Skip to content

Commit a33cff2

Browse files
authored
Fix F701 to F707 errors in tests (#19125)
## Summary Per @ntBre in #19111, it would be a good idea to make the tests no longer have these syntax errors, so this PR updates the tests and snapshots. `B031` gave me a lot of trouble since the ending test of declaring a function named `groupby` makes it so that inside other functions, it's unclear which `groupby` is referred to since it depends on when the function is called. To fix it I made each function have it's own `from itertools import groupby` so there's no more ambiguity.
1 parent f48a34f commit a33cff2

File tree

11 files changed

+513
-499
lines changed

11 files changed

+513
-499
lines changed
Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,30 @@
1-
try:
2-
pass
3-
except Exception:
4-
continue
1+
for _ in []:
2+
try:
3+
pass
4+
except Exception:
5+
continue
56

6-
try:
7-
pass
8-
except:
9-
continue
7+
try:
8+
pass
9+
except:
10+
continue
1011

11-
try:
12-
pass
13-
except (Exception,):
14-
continue
12+
try:
13+
pass
14+
except (Exception,):
15+
continue
1516

16-
try:
17-
pass
18-
except (Exception, ValueError):
19-
continue
17+
try:
18+
pass
19+
except (Exception, ValueError):
20+
continue
2021

21-
try:
22-
pass
23-
except ValueError:
24-
continue
22+
try:
23+
pass
24+
except ValueError:
25+
continue
2526

26-
try:
27-
pass
28-
except (ValueError,):
29-
continue
27+
try:
28+
pass
29+
except (ValueError,):
30+
continue

crates/ruff_linter/resources/test/fixtures/flake8_bugbear/B031.py

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -185,38 +185,45 @@ def collect_shop_items(shopper, items):
185185
collect_shop_items(shopper, section_items)
186186

187187
# Shouldn't trigger the warning when there is a return statement.
188-
for _section, section_items in groupby(items, key=lambda p: p[1]):
189-
if _section == "greens":
188+
def foo():
189+
for _section, section_items in groupby(items, key=lambda p: p[1]):
190+
if _section == "greens":
191+
collect_shop_items(shopper, section_items)
192+
return
193+
elif _section == "frozen items":
194+
return section_items
190195
collect_shop_items(shopper, section_items)
191-
return
192-
elif _section == "frozen items":
193-
return section_items
194-
collect_shop_items(shopper, section_items)
195196

196197
# Should trigger the warning for duplicate access, even if is a return statement after.
197-
for _section, section_items in groupby(items, key=lambda p: p[1]):
198-
if _section == "greens":
199-
collect_shop_items(shopper, section_items)
200-
collect_shop_items(shopper, section_items)
201-
return
198+
def foo():
199+
from itertools import groupby
200+
for _section, section_items in groupby(items, key=lambda p: p[1]):
201+
if _section == "greens":
202+
collect_shop_items(shopper, section_items)
203+
collect_shop_items(shopper, section_items)
204+
return
202205

203206
# Should trigger the warning for duplicate access, even if is a return in another branch.
204-
for _section, section_items in groupby(items, key=lambda p: p[1]):
205-
if _section == "greens":
206-
collect_shop_items(shopper, section_items)
207-
return
208-
elif _section == "frozen items":
209-
collect_shop_items(shopper, section_items)
210-
collect_shop_items(shopper, section_items)
207+
def foo():
208+
from itertools import groupby
209+
for _section, section_items in groupby(items, key=lambda p: p[1]):
210+
if _section == "greens":
211+
collect_shop_items(shopper, section_items)
212+
return
213+
elif _section == "frozen items":
214+
collect_shop_items(shopper, section_items)
215+
collect_shop_items(shopper, section_items)
211216

212217
# Should trigger, since only one branch has a return statement.
213-
for _section, section_items in groupby(items, key=lambda p: p[1]):
214-
if _section == "greens":
215-
collect_shop_items(shopper, section_items)
216-
return
217-
elif _section == "frozen items":
218-
collect_shop_items(shopper, section_items)
219-
collect_shop_items(shopper, section_items) # B031
218+
def foo():
219+
from itertools import groupby
220+
for _section, section_items in groupby(items, key=lambda p: p[1]):
221+
if _section == "greens":
222+
collect_shop_items(shopper, section_items)
223+
return
224+
elif _section == "frozen items":
225+
collect_shop_items(shopper, section_items)
226+
collect_shop_items(shopper, section_items) # B031
220227

221228
# Let's redefine the `groupby` function to make sure we pick up the correct one.
222229
# NOTE: This should always be at the end of the file.

crates/ruff_linter/resources/test/fixtures/flake8_pie/PIE804.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@
2626
abc(a=1, **{'a': c}, **{'b': c}) # PIE804
2727

2828
# Some values need to be parenthesized.
29-
abc(foo=1, **{'bar': (bar := 1)}) # PIE804
30-
abc(foo=1, **{'bar': (yield 1)}) # PIE804
29+
def foo():
30+
abc(foo=1, **{'bar': (bar := 1)}) # PIE804
31+
abc(foo=1, **{'bar': (yield 1)}) # PIE804
3132

3233
# https://github.com/astral-sh/ruff/issues/18036
3334
# The autofix for this is unsafe due to the comments inside the dictionary.

crates/ruff_linter/resources/test/fixtures/flake8_simplify/SIM115.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@
2727
close_files = stack.pop_all().close
2828

2929
# OK
30-
with contextlib.AsyncExitStack() as exit_stack:
31-
f = await exit_stack.enter_async_context(open("filename"))
30+
async def foo():
31+
with contextlib.AsyncExitStack() as exit_stack:
32+
f = await exit_stack.enter_async_context(open("filename"))
3233

3334
# OK (false negative)
3435
with contextlib.ExitStack():
@@ -275,9 +276,10 @@ def setUpClass(cls):
275276
cls.enterClassContext(open("filename"))
276277

277278
# OK
278-
class ExampleAsyncTests(IsolatedAsyncioTestCase):
279-
async def test_something(self):
280-
await self.enterAsyncContext(open("filename"))
279+
async def foo():
280+
class ExampleAsyncTests(IsolatedAsyncioTestCase):
281+
async def test_something(self):
282+
await self.enterAsyncContext(open("filename"))
281283

282284
# OK
283285
class ExampleTests(TestCase):
Lines changed: 89 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,98 +1,99 @@
1-
# Errors
2-
a = "hello"
1+
def foo():
2+
# Errors
3+
a = "hello"
34

4-
# SIM116
5-
if a == "foo":
6-
return "bar"
7-
elif a == "bar":
8-
return "baz"
9-
elif a == "boo":
10-
return "ooh"
11-
else:
12-
return 42
5+
# SIM116
6+
if a == "foo":
7+
return "bar"
8+
elif a == "bar":
9+
return "baz"
10+
elif a == "boo":
11+
return "ooh"
12+
else:
13+
return 42
1314

14-
# SIM116
15-
if a == 1:
16-
return (1, 2, 3)
17-
elif a == 2:
18-
return (4, 5, 6)
19-
elif a == 3:
20-
return (7, 8, 9)
21-
else:
22-
return (10, 11, 12)
15+
# SIM116
16+
if a == 1:
17+
return (1, 2, 3)
18+
elif a == 2:
19+
return (4, 5, 6)
20+
elif a == 3:
21+
return (7, 8, 9)
22+
else:
23+
return (10, 11, 12)
2324

24-
# SIM116
25-
if a == 1:
26-
return (1, 2, 3)
27-
elif a == 2:
28-
return (4, 5, 6)
29-
elif a == 3:
30-
return (7, 8, 9)
25+
# SIM116
26+
if a == 1:
27+
return (1, 2, 3)
28+
elif a == 2:
29+
return (4, 5, 6)
30+
elif a == 3:
31+
return (7, 8, 9)
3132

32-
# SIM116
33-
if a == "hello 'sir'":
34-
return (1, 2, 3)
35-
elif a == 'goodbye "mam"':
36-
return (4, 5, 6)
37-
elif a == """Fairwell 'mister'""":
38-
return (7, 8, 9)
39-
else:
40-
return (10, 11, 12)
33+
# SIM116
34+
if a == "hello 'sir'":
35+
return (1, 2, 3)
36+
elif a == 'goodbye "mam"':
37+
return (4, 5, 6)
38+
elif a == """Fairwell 'mister'""":
39+
return (7, 8, 9)
40+
else:
41+
return (10, 11, 12)
4142

42-
# SIM116
43-
if a == b"one":
44-
return 1
45-
elif a == b"two":
46-
return 2
47-
elif a == b"three":
48-
return 3
43+
# SIM116
44+
if a == b"one":
45+
return 1
46+
elif a == b"two":
47+
return 2
48+
elif a == b"three":
49+
return 3
4950

50-
# SIM116
51-
if a == "hello 'sir'":
52-
return ("hello'", 'hi"', 3)
53-
elif a == 'goodbye "mam"':
54-
return (4, 5, 6)
55-
elif a == """Fairwell 'mister'""":
56-
return (7, 8, 9)
57-
else:
58-
return (10, 11, 12)
51+
# SIM116
52+
if a == "hello 'sir'":
53+
return ("hello'", 'hi"', 3)
54+
elif a == 'goodbye "mam"':
55+
return (4, 5, 6)
56+
elif a == """Fairwell 'mister'""":
57+
return (7, 8, 9)
58+
else:
59+
return (10, 11, 12)
5960

60-
# OK
61-
if a == "foo":
62-
return "bar"
63-
elif a == "bar":
64-
return baz()
65-
elif a == "boo":
66-
return "ooh"
67-
else:
68-
return 42
61+
# OK
62+
if a == "foo":
63+
return "bar"
64+
elif a == "bar":
65+
return baz()
66+
elif a == "boo":
67+
return "ooh"
68+
else:
69+
return 42
6970

70-
# OK
71-
if a == b"one":
72-
return 1
73-
elif b == b"two":
74-
return 2
75-
elif a == b"three":
76-
return 3
71+
# OK
72+
if a == b"one":
73+
return 1
74+
elif b == b"two":
75+
return 2
76+
elif a == b"three":
77+
return 3
7778

78-
# SIM116
79-
if func_name == "create":
80-
return "A"
81-
elif func_name == "modify":
82-
return "M"
83-
elif func_name == "remove":
84-
return "D"
85-
elif func_name == "move":
86-
return "MV"
79+
# SIM116
80+
if func_name == "create":
81+
return "A"
82+
elif func_name == "modify":
83+
return "M"
84+
elif func_name == "remove":
85+
return "D"
86+
elif func_name == "move":
87+
return "MV"
8788

88-
# OK
89-
def no_return_in_else(platform):
90-
if platform == "linux":
91-
return "auditwheel repair -w {dest_dir} {wheel}"
92-
elif platform == "macos":
93-
return "delocate-wheel --require-archs {delocate_archs} -w {dest_dir} -v {wheel}"
94-
elif platform == "windows":
95-
return ""
96-
else:
97-
msg = f"Unknown platform: {platform!r}"
98-
raise ValueError(msg)
89+
# OK
90+
def no_return_in_else(platform):
91+
if platform == "linux":
92+
return "auditwheel repair -w {dest_dir} {wheel}"
93+
elif platform == "macos":
94+
return "delocate-wheel --require-archs {delocate_archs} -w {dest_dir} -v {wheel}"
95+
elif platform == "windows":
96+
return ""
97+
else:
98+
msg = f"Unknown platform: {platform!r}"
99+
raise ValueError(msg)

crates/ruff_linter/resources/test/fixtures/pycodestyle/E27.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,5 @@ def f():
8181
# https://github.com/astral-sh/ruff/issues/12094
8282
pass;
8383

84-
yield, x
84+
def foo():
85+
yield, x

0 commit comments

Comments
 (0)