Fix F701 to F707 errors in tests (#19125)

## Summary

Per @ntBre in https://github.com/astral-sh/ruff/pull/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.
This commit is contained in:
GiGaGon 2025-07-04 11:43:18 -07:00 committed by GitHub
parent f48a34fbab
commit a33cff2b12
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 528 additions and 514 deletions

View file

@ -1,29 +1,30 @@
try: for _ in []:
pass try:
except Exception: pass
continue except Exception:
continue
try: try:
pass pass
except: except:
continue continue
try: try:
pass pass
except (Exception,): except (Exception,):
continue continue
try: try:
pass pass
except (Exception, ValueError): except (Exception, ValueError):
continue continue
try: try:
pass pass
except ValueError: except ValueError:
continue continue
try: try:
pass pass
except (ValueError,): except (ValueError,):
continue continue

View file

@ -185,38 +185,45 @@ for _section, section_items in groupby(items, key=lambda p: p[1]):
collect_shop_items(shopper, section_items) collect_shop_items(shopper, section_items)
# Shouldn't trigger the warning when there is a return statement. # Shouldn't trigger the warning when there is a return statement.
for _section, section_items in groupby(items, key=lambda p: p[1]): def foo():
if _section == "greens": for _section, section_items in groupby(items, key=lambda p: p[1]):
if _section == "greens":
collect_shop_items(shopper, section_items)
return
elif _section == "frozen items":
return section_items
collect_shop_items(shopper, section_items) collect_shop_items(shopper, section_items)
return
elif _section == "frozen items":
return section_items
collect_shop_items(shopper, section_items)
# Should trigger the warning for duplicate access, even if is a return statement after. # Should trigger the warning for duplicate access, even if is a return statement after.
for _section, section_items in groupby(items, key=lambda p: p[1]): def foo():
if _section == "greens": from itertools import groupby
collect_shop_items(shopper, section_items) for _section, section_items in groupby(items, key=lambda p: p[1]):
collect_shop_items(shopper, section_items) if _section == "greens":
return collect_shop_items(shopper, section_items)
collect_shop_items(shopper, section_items)
return
# Should trigger the warning for duplicate access, even if is a return in another branch. # Should trigger the warning for duplicate access, even if is a return in another branch.
for _section, section_items in groupby(items, key=lambda p: p[1]): def foo():
if _section == "greens": from itertools import groupby
collect_shop_items(shopper, section_items) for _section, section_items in groupby(items, key=lambda p: p[1]):
return if _section == "greens":
elif _section == "frozen items": collect_shop_items(shopper, section_items)
collect_shop_items(shopper, section_items) return
collect_shop_items(shopper, section_items) elif _section == "frozen items":
collect_shop_items(shopper, section_items)
collect_shop_items(shopper, section_items)
# Should trigger, since only one branch has a return statement. # Should trigger, since only one branch has a return statement.
for _section, section_items in groupby(items, key=lambda p: p[1]): def foo():
if _section == "greens": from itertools import groupby
collect_shop_items(shopper, section_items) for _section, section_items in groupby(items, key=lambda p: p[1]):
return if _section == "greens":
elif _section == "frozen items": collect_shop_items(shopper, section_items)
collect_shop_items(shopper, section_items) return
collect_shop_items(shopper, section_items) # B031 elif _section == "frozen items":
collect_shop_items(shopper, section_items)
collect_shop_items(shopper, section_items) # B031
# Let's redefine the `groupby` function to make sure we pick up the correct one. # Let's redefine the `groupby` function to make sure we pick up the correct one.
# NOTE: This should always be at the end of the file. # NOTE: This should always be at the end of the file.

View file

@ -26,8 +26,9 @@ abc(**{'a': b}, **{'a': c}) # PIE804
abc(a=1, **{'a': c}, **{'b': c}) # PIE804 abc(a=1, **{'a': c}, **{'b': c}) # PIE804
# Some values need to be parenthesized. # Some values need to be parenthesized.
abc(foo=1, **{'bar': (bar := 1)}) # PIE804 def foo():
abc(foo=1, **{'bar': (yield 1)}) # PIE804 abc(foo=1, **{'bar': (bar := 1)}) # PIE804
abc(foo=1, **{'bar': (yield 1)}) # PIE804
# https://github.com/astral-sh/ruff/issues/18036 # https://github.com/astral-sh/ruff/issues/18036
# The autofix for this is unsafe due to the comments inside the dictionary. # The autofix for this is unsafe due to the comments inside the dictionary.

View file

@ -27,8 +27,9 @@ with contextlib.ExitStack() as stack:
close_files = stack.pop_all().close close_files = stack.pop_all().close
# OK # OK
with contextlib.AsyncExitStack() as exit_stack: async def foo():
f = await exit_stack.enter_async_context(open("filename")) with contextlib.AsyncExitStack() as exit_stack:
f = await exit_stack.enter_async_context(open("filename"))
# OK (false negative) # OK (false negative)
with contextlib.ExitStack(): with contextlib.ExitStack():
@ -275,9 +276,10 @@ class ExampleClassTests(TestCase):
cls.enterClassContext(open("filename")) cls.enterClassContext(open("filename"))
# OK # OK
class ExampleAsyncTests(IsolatedAsyncioTestCase): async def foo():
async def test_something(self): class ExampleAsyncTests(IsolatedAsyncioTestCase):
await self.enterAsyncContext(open("filename")) async def test_something(self):
await self.enterAsyncContext(open("filename"))
# OK # OK
class ExampleTests(TestCase): class ExampleTests(TestCase):

View file

@ -1,98 +1,99 @@
# Errors def foo():
a = "hello" # Errors
a = "hello"
# SIM116 # SIM116
if a == "foo": if a == "foo":
return "bar" return "bar"
elif a == "bar": elif a == "bar":
return "baz" return "baz"
elif a == "boo": elif a == "boo":
return "ooh" return "ooh"
else:
return 42
# SIM116
if a == 1:
return (1, 2, 3)
elif a == 2:
return (4, 5, 6)
elif a == 3:
return (7, 8, 9)
else:
return (10, 11, 12)
# SIM116
if a == 1:
return (1, 2, 3)
elif a == 2:
return (4, 5, 6)
elif a == 3:
return (7, 8, 9)
# SIM116
if a == "hello 'sir'":
return (1, 2, 3)
elif a == 'goodbye "mam"':
return (4, 5, 6)
elif a == """Fairwell 'mister'""":
return (7, 8, 9)
else:
return (10, 11, 12)
# SIM116
if a == b"one":
return 1
elif a == b"two":
return 2
elif a == b"three":
return 3
# SIM116
if a == "hello 'sir'":
return ("hello'", 'hi"', 3)
elif a == 'goodbye "mam"':
return (4, 5, 6)
elif a == """Fairwell 'mister'""":
return (7, 8, 9)
else:
return (10, 11, 12)
# OK
if a == "foo":
return "bar"
elif a == "bar":
return baz()
elif a == "boo":
return "ooh"
else:
return 42
# OK
if a == b"one":
return 1
elif b == b"two":
return 2
elif a == b"three":
return 3
# SIM116
if func_name == "create":
return "A"
elif func_name == "modify":
return "M"
elif func_name == "remove":
return "D"
elif func_name == "move":
return "MV"
# OK
def no_return_in_else(platform):
if platform == "linux":
return "auditwheel repair -w {dest_dir} {wheel}"
elif platform == "macos":
return "delocate-wheel --require-archs {delocate_archs} -w {dest_dir} -v {wheel}"
elif platform == "windows":
return ""
else: else:
msg = f"Unknown platform: {platform!r}" return 42
raise ValueError(msg)
# SIM116
if a == 1:
return (1, 2, 3)
elif a == 2:
return (4, 5, 6)
elif a == 3:
return (7, 8, 9)
else:
return (10, 11, 12)
# SIM116
if a == 1:
return (1, 2, 3)
elif a == 2:
return (4, 5, 6)
elif a == 3:
return (7, 8, 9)
# SIM116
if a == "hello 'sir'":
return (1, 2, 3)
elif a == 'goodbye "mam"':
return (4, 5, 6)
elif a == """Fairwell 'mister'""":
return (7, 8, 9)
else:
return (10, 11, 12)
# SIM116
if a == b"one":
return 1
elif a == b"two":
return 2
elif a == b"three":
return 3
# SIM116
if a == "hello 'sir'":
return ("hello'", 'hi"', 3)
elif a == 'goodbye "mam"':
return (4, 5, 6)
elif a == """Fairwell 'mister'""":
return (7, 8, 9)
else:
return (10, 11, 12)
# OK
if a == "foo":
return "bar"
elif a == "bar":
return baz()
elif a == "boo":
return "ooh"
else:
return 42
# OK
if a == b"one":
return 1
elif b == b"two":
return 2
elif a == b"three":
return 3
# SIM116
if func_name == "create":
return "A"
elif func_name == "modify":
return "M"
elif func_name == "remove":
return "D"
elif func_name == "move":
return "MV"
# OK
def no_return_in_else(platform):
if platform == "linux":
return "auditwheel repair -w {dest_dir} {wheel}"
elif platform == "macos":
return "delocate-wheel --require-archs {delocate_archs} -w {dest_dir} -v {wheel}"
elif platform == "windows":
return ""
else:
msg = f"Unknown platform: {platform!r}"
raise ValueError(msg)

View file

@ -81,4 +81,5 @@ match(foo):
# https://github.com/astral-sh/ruff/issues/12094 # https://github.com/astral-sh/ruff/issues/12094
pass; pass;
yield, x def foo():
yield, x

View file

@ -1,46 +1,46 @@
--- ---
source: crates/ruff_linter/src/rules/flake8_bandit/mod.rs source: crates/ruff_linter/src/rules/flake8_bandit/mod.rs
--- ---
S112.py:3:1: S112 `try`-`except`-`continue` detected, consider logging the exception S112.py:4:5: S112 `try`-`except`-`continue` detected, consider logging the exception
| |
1 | try: 2 | try:
2 | pass 3 | pass
3 | / except Exception: 4 | / except Exception:
4 | | continue 5 | | continue
| |____________^ S112 | |________________^ S112
5 | 6 |
6 | try: 7 | try:
| |
S112.py:8:1: S112 `try`-`except`-`continue` detected, consider logging the exception S112.py:9:5: S112 `try`-`except`-`continue` detected, consider logging the exception
| |
6 | try: 7 | try:
7 | pass 8 | pass
8 | / except: 9 | / except:
9 | | continue 10 | | continue
| |____________^ S112 | |________________^ S112
10 | 11 |
11 | try: 12 | try:
| |
S112.py:13:1: S112 `try`-`except`-`continue` detected, consider logging the exception S112.py:14:5: S112 `try`-`except`-`continue` detected, consider logging the exception
| |
11 | try: 12 | try:
12 | pass 13 | pass
13 | / except (Exception,): 14 | / except (Exception,):
14 | | continue 15 | | continue
| |____________^ S112 | |________________^ S112
15 | 16 |
16 | try: 17 | try:
| |
S112.py:18:1: S112 `try`-`except`-`continue` detected, consider logging the exception S112.py:19:5: S112 `try`-`except`-`continue` detected, consider logging the exception
| |
16 | try: 17 | try:
17 | pass 18 | pass
18 | / except (Exception, ValueError): 19 | / except (Exception, ValueError):
19 | | continue 20 | | continue
| |____________^ S112 | |________________^ S112
20 | 21 |
21 | try: 22 | try:
| |

View file

@ -195,31 +195,31 @@ B031.py:144:33: B031 Using the generator returned from `itertools.groupby()` mor
146 | for group in groupby(items, key=lambda p: p[1]): 146 | for group in groupby(items, key=lambda p: p[1]):
| |
B031.py:200:37: B031 Using the generator returned from `itertools.groupby()` more than once will do nothing on the second usage B031.py:203:41: B031 Using the generator returned from `itertools.groupby()` more than once will do nothing on the second usage
| |
198 | if _section == "greens": 201 | if _section == "greens":
199 | collect_shop_items(shopper, section_items) 202 | collect_shop_items(shopper, section_items)
200 | collect_shop_items(shopper, section_items) 203 | collect_shop_items(shopper, section_items)
| ^^^^^^^^^^^^^ B031 | ^^^^^^^^^^^^^ B031
201 | return 204 | return
| |
B031.py:210:37: B031 Using the generator returned from `itertools.groupby()` more than once will do nothing on the second usage B031.py:215:41: B031 Using the generator returned from `itertools.groupby()` more than once will do nothing on the second usage
| |
208 | elif _section == "frozen items": 213 | elif _section == "frozen items":
209 | collect_shop_items(shopper, section_items) 214 | collect_shop_items(shopper, section_items)
210 | collect_shop_items(shopper, section_items) 215 | collect_shop_items(shopper, section_items)
| ^^^^^^^^^^^^^ B031 | ^^^^^^^^^^^^^ B031
211 | 216 |
212 | # Should trigger, since only one branch has a return statement. 217 | # Should trigger, since only one branch has a return statement.
| |
B031.py:219:33: B031 Using the generator returned from `itertools.groupby()` more than once will do nothing on the second usage B031.py:226:37: B031 Using the generator returned from `itertools.groupby()` more than once will do nothing on the second usage
| |
217 | elif _section == "frozen items": 224 | elif _section == "frozen items":
218 | collect_shop_items(shopper, section_items) 225 | collect_shop_items(shopper, section_items)
219 | collect_shop_items(shopper, section_items) # B031 226 | collect_shop_items(shopper, section_items) # B031
| ^^^^^^^^^^^^^ B031 | ^^^^^^^^^^^^^ B031
220 | 227 |
221 | # Let's redefine the `groupby` function to make sure we pick up the correct one. 228 | # Let's redefine the `groupby` function to make sure we pick up the correct one.
| |

View file

@ -190,72 +190,73 @@ PIE804.py:26:22: PIE804 [*] Unnecessary `dict` kwargs
26 |+abc(a=1, **{'a': c}, b=c) # PIE804 26 |+abc(a=1, **{'a': c}, b=c) # PIE804
27 27 | 27 27 |
28 28 | # Some values need to be parenthesized. 28 28 | # Some values need to be parenthesized.
29 29 | abc(foo=1, **{'bar': (bar := 1)}) # PIE804 29 29 | def foo():
PIE804.py:29:12: PIE804 [*] Unnecessary `dict` kwargs PIE804.py:30:16: PIE804 [*] Unnecessary `dict` kwargs
| |
28 | # Some values need to be parenthesized. 28 | # Some values need to be parenthesized.
29 | abc(foo=1, **{'bar': (bar := 1)}) # PIE804 29 | def foo():
| ^^^^^^^^^^^^^^^^^^^^^ PIE804 30 | abc(foo=1, **{'bar': (bar := 1)}) # PIE804
30 | abc(foo=1, **{'bar': (yield 1)}) # PIE804 | ^^^^^^^^^^^^^^^^^^^^^ PIE804
| 31 | abc(foo=1, **{'bar': (yield 1)}) # PIE804
= help: Remove unnecessary kwargs
Safe fix
26 26 | abc(a=1, **{'a': c}, **{'b': c}) # PIE804
27 27 |
28 28 | # Some values need to be parenthesized.
29 |-abc(foo=1, **{'bar': (bar := 1)}) # PIE804
29 |+abc(foo=1, bar=(bar := 1)) # PIE804
30 30 | abc(foo=1, **{'bar': (yield 1)}) # PIE804
31 31 |
32 32 | # https://github.com/astral-sh/ruff/issues/18036
PIE804.py:30:12: PIE804 [*] Unnecessary `dict` kwargs
|
28 | # Some values need to be parenthesized.
29 | abc(foo=1, **{'bar': (bar := 1)}) # PIE804
30 | abc(foo=1, **{'bar': (yield 1)}) # PIE804
| ^^^^^^^^^^^^^^^^^^^^ PIE804
31 |
32 | # https://github.com/astral-sh/ruff/issues/18036
| |
= help: Remove unnecessary kwargs = help: Remove unnecessary kwargs
Safe fix Safe fix
27 27 | 27 27 |
28 28 | # Some values need to be parenthesized. 28 28 | # Some values need to be parenthesized.
29 29 | abc(foo=1, **{'bar': (bar := 1)}) # PIE804 29 29 | def foo():
30 |-abc(foo=1, **{'bar': (yield 1)}) # PIE804 30 |- abc(foo=1, **{'bar': (bar := 1)}) # PIE804
30 |+abc(foo=1, bar=(yield 1)) # PIE804 30 |+ abc(foo=1, bar=(bar := 1)) # PIE804
31 31 | 31 31 | abc(foo=1, **{'bar': (yield 1)}) # PIE804
32 32 | # https://github.com/astral-sh/ruff/issues/18036 32 32 |
33 33 | # The autofix for this is unsafe due to the comments inside the dictionary. 33 33 | # https://github.com/astral-sh/ruff/issues/18036
PIE804.py:35:5: PIE804 [*] Unnecessary `dict` kwargs PIE804.py:31:16: PIE804 [*] Unnecessary `dict` kwargs
| |
33 | # The autofix for this is unsafe due to the comments inside the dictionary. 29 | def foo():
34 | foo( 30 | abc(foo=1, **{'bar': (bar := 1)}) # PIE804
35 | / **{ 31 | abc(foo=1, **{'bar': (yield 1)}) # PIE804
36 | | # Comment 1 | ^^^^^^^^^^^^^^^^^^^^ PIE804
37 | | "x": 1.0, 32 |
38 | | # Comment 2 33 | # https://github.com/astral-sh/ruff/issues/18036
39 | | "y": 2.0, |
40 | | } = help: Remove unnecessary kwargs
Safe fix
28 28 | # Some values need to be parenthesized.
29 29 | def foo():
30 30 | abc(foo=1, **{'bar': (bar := 1)}) # PIE804
31 |- abc(foo=1, **{'bar': (yield 1)}) # PIE804
31 |+ abc(foo=1, bar=(yield 1)) # PIE804
32 32 |
33 33 | # https://github.com/astral-sh/ruff/issues/18036
34 34 | # The autofix for this is unsafe due to the comments inside the dictionary.
PIE804.py:36:5: PIE804 [*] Unnecessary `dict` kwargs
|
34 | # The autofix for this is unsafe due to the comments inside the dictionary.
35 | foo(
36 | / **{
37 | | # Comment 1
38 | | "x": 1.0,
39 | | # Comment 2
40 | | "y": 2.0,
41 | | }
| |_____^ PIE804 | |_____^ PIE804
41 | ) 42 | )
| |
= help: Remove unnecessary kwargs = help: Remove unnecessary kwargs
Unsafe fix Unsafe fix
32 32 | # https://github.com/astral-sh/ruff/issues/18036 33 33 | # https://github.com/astral-sh/ruff/issues/18036
33 33 | # The autofix for this is unsafe due to the comments inside the dictionary. 34 34 | # The autofix for this is unsafe due to the comments inside the dictionary.
34 34 | foo( 35 35 | foo(
35 |- **{ 36 |- **{
36 |- # Comment 1 37 |- # Comment 1
37 |- "x": 1.0, 38 |- "x": 1.0,
38 |- # Comment 2 39 |- # Comment 2
39 |- "y": 2.0, 40 |- "y": 2.0,
40 |- } 41 |- }
35 |+ x=1.0, y=2.0 36 |+ x=1.0, y=2.0
41 36 | ) 42 37 | )

View file

@ -50,285 +50,285 @@ SIM115.py:12:5: SIM115 Use a context manager for opening files
14 | f.close() 14 | f.close()
| |
SIM115.py:39:9: SIM115 Use a context manager for opening files SIM115.py:40:9: SIM115 Use a context manager for opening files
| |
37 | # SIM115 38 | # SIM115
38 | with contextlib.ExitStack(): 39 | with contextlib.ExitStack():
39 | f = open("filename") 40 | f = open("filename")
| ^^^^ SIM115 | ^^^^ SIM115
40 | 41 |
41 | # OK 42 | # OK
|
SIM115.py:80:5: SIM115 Use a context manager for opening files
|
78 | import fileinput
79 |
80 | f = tempfile.NamedTemporaryFile()
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ SIM115
81 | f = tempfile.TemporaryFile()
82 | f = tempfile.SpooledTemporaryFile()
| |
SIM115.py:81:5: SIM115 Use a context manager for opening files SIM115.py:81:5: SIM115 Use a context manager for opening files
| |
80 | f = tempfile.NamedTemporaryFile() 79 | import fileinput
81 | f = tempfile.TemporaryFile() 80 |
| ^^^^^^^^^^^^^^^^^^^^^^ SIM115 81 | f = tempfile.NamedTemporaryFile()
82 | f = tempfile.SpooledTemporaryFile() | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ SIM115
83 | f = tarfile.open("foo.tar") 82 | f = tempfile.TemporaryFile()
83 | f = tempfile.SpooledTemporaryFile()
| |
SIM115.py:82:5: SIM115 Use a context manager for opening files SIM115.py:82:5: SIM115 Use a context manager for opening files
| |
80 | f = tempfile.NamedTemporaryFile() 81 | f = tempfile.NamedTemporaryFile()
81 | f = tempfile.TemporaryFile() 82 | f = tempfile.TemporaryFile()
82 | f = tempfile.SpooledTemporaryFile() | ^^^^^^^^^^^^^^^^^^^^^^ SIM115
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ SIM115 83 | f = tempfile.SpooledTemporaryFile()
83 | f = tarfile.open("foo.tar") 84 | f = tarfile.open("foo.tar")
84 | f = TarFile("foo.tar").open()
| |
SIM115.py:83:5: SIM115 Use a context manager for opening files SIM115.py:83:5: SIM115 Use a context manager for opening files
| |
81 | f = tempfile.TemporaryFile() 81 | f = tempfile.NamedTemporaryFile()
82 | f = tempfile.SpooledTemporaryFile() 82 | f = tempfile.TemporaryFile()
83 | f = tarfile.open("foo.tar") 83 | f = tempfile.SpooledTemporaryFile()
| ^^^^^^^^^^^^ SIM115 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ SIM115
84 | f = TarFile("foo.tar").open() 84 | f = tarfile.open("foo.tar")
85 | f = tarfile.TarFile("foo.tar").open() 85 | f = TarFile("foo.tar").open()
| |
SIM115.py:84:5: SIM115 Use a context manager for opening files SIM115.py:84:5: SIM115 Use a context manager for opening files
| |
82 | f = tempfile.SpooledTemporaryFile() 82 | f = tempfile.TemporaryFile()
83 | f = tarfile.open("foo.tar") 83 | f = tempfile.SpooledTemporaryFile()
84 | f = TarFile("foo.tar").open() 84 | f = tarfile.open("foo.tar")
| ^^^^^^^^^^^^^^^^^^^^^^^ SIM115 | ^^^^^^^^^^^^ SIM115
85 | f = tarfile.TarFile("foo.tar").open() 85 | f = TarFile("foo.tar").open()
86 | f = tarfile.TarFile().open() 86 | f = tarfile.TarFile("foo.tar").open()
| |
SIM115.py:85:5: SIM115 Use a context manager for opening files SIM115.py:85:5: SIM115 Use a context manager for opening files
| |
83 | f = tarfile.open("foo.tar") 83 | f = tempfile.SpooledTemporaryFile()
84 | f = TarFile("foo.tar").open() 84 | f = tarfile.open("foo.tar")
85 | f = tarfile.TarFile("foo.tar").open() 85 | f = TarFile("foo.tar").open()
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ SIM115 | ^^^^^^^^^^^^^^^^^^^^^^^ SIM115
86 | f = tarfile.TarFile().open() 86 | f = tarfile.TarFile("foo.tar").open()
87 | f = zipfile.ZipFile("foo.zip").open("foo.txt") 87 | f = tarfile.TarFile().open()
| |
SIM115.py:86:5: SIM115 Use a context manager for opening files SIM115.py:86:5: SIM115 Use a context manager for opening files
| |
84 | f = TarFile("foo.tar").open() 84 | f = tarfile.open("foo.tar")
85 | f = tarfile.TarFile("foo.tar").open() 85 | f = TarFile("foo.tar").open()
86 | f = tarfile.TarFile().open() 86 | f = tarfile.TarFile("foo.tar").open()
| ^^^^^^^^^^^^^^^^^^^^^^ SIM115 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ SIM115
87 | f = zipfile.ZipFile("foo.zip").open("foo.txt") 87 | f = tarfile.TarFile().open()
88 | f = io.open("foo.txt") 88 | f = zipfile.ZipFile("foo.zip").open("foo.txt")
| |
SIM115.py:87:5: SIM115 Use a context manager for opening files SIM115.py:87:5: SIM115 Use a context manager for opening files
| |
85 | f = tarfile.TarFile("foo.tar").open() 85 | f = TarFile("foo.tar").open()
86 | f = tarfile.TarFile().open() 86 | f = tarfile.TarFile("foo.tar").open()
87 | f = zipfile.ZipFile("foo.zip").open("foo.txt") 87 | f = tarfile.TarFile().open()
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ SIM115 | ^^^^^^^^^^^^^^^^^^^^^^ SIM115
88 | f = io.open("foo.txt") 88 | f = zipfile.ZipFile("foo.zip").open("foo.txt")
89 | f = io.open_code("foo.txt") 89 | f = io.open("foo.txt")
| |
SIM115.py:88:5: SIM115 Use a context manager for opening files SIM115.py:88:5: SIM115 Use a context manager for opening files
| |
86 | f = tarfile.TarFile().open() 86 | f = tarfile.TarFile("foo.tar").open()
87 | f = zipfile.ZipFile("foo.zip").open("foo.txt") 87 | f = tarfile.TarFile().open()
88 | f = io.open("foo.txt") 88 | f = zipfile.ZipFile("foo.zip").open("foo.txt")
| ^^^^^^^ SIM115 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ SIM115
89 | f = io.open_code("foo.txt") 89 | f = io.open("foo.txt")
90 | f = codecs.open("foo.txt") 90 | f = io.open_code("foo.txt")
| |
SIM115.py:89:5: SIM115 Use a context manager for opening files SIM115.py:89:5: SIM115 Use a context manager for opening files
| |
87 | f = zipfile.ZipFile("foo.zip").open("foo.txt") 87 | f = tarfile.TarFile().open()
88 | f = io.open("foo.txt") 88 | f = zipfile.ZipFile("foo.zip").open("foo.txt")
89 | f = io.open_code("foo.txt") 89 | f = io.open("foo.txt")
| ^^^^^^^^^^^^ SIM115 | ^^^^^^^ SIM115
90 | f = codecs.open("foo.txt") 90 | f = io.open_code("foo.txt")
91 | f = bz2.open("foo.txt") 91 | f = codecs.open("foo.txt")
| |
SIM115.py:90:5: SIM115 Use a context manager for opening files SIM115.py:90:5: SIM115 Use a context manager for opening files
| |
88 | f = io.open("foo.txt") 88 | f = zipfile.ZipFile("foo.zip").open("foo.txt")
89 | f = io.open_code("foo.txt") 89 | f = io.open("foo.txt")
90 | f = codecs.open("foo.txt") 90 | f = io.open_code("foo.txt")
| ^^^^^^^^^^^ SIM115 | ^^^^^^^^^^^^ SIM115
91 | f = bz2.open("foo.txt") 91 | f = codecs.open("foo.txt")
92 | f = gzip.open("foo.txt") 92 | f = bz2.open("foo.txt")
| |
SIM115.py:91:5: SIM115 Use a context manager for opening files SIM115.py:91:5: SIM115 Use a context manager for opening files
| |
89 | f = io.open_code("foo.txt") 89 | f = io.open("foo.txt")
90 | f = codecs.open("foo.txt") 90 | f = io.open_code("foo.txt")
91 | f = bz2.open("foo.txt") 91 | f = codecs.open("foo.txt")
| ^^^^^^^^ SIM115 | ^^^^^^^^^^^ SIM115
92 | f = gzip.open("foo.txt") 92 | f = bz2.open("foo.txt")
93 | f = dbm.open("foo.db") 93 | f = gzip.open("foo.txt")
| |
SIM115.py:92:5: SIM115 Use a context manager for opening files SIM115.py:92:5: SIM115 Use a context manager for opening files
| |
90 | f = codecs.open("foo.txt") 90 | f = io.open_code("foo.txt")
91 | f = bz2.open("foo.txt") 91 | f = codecs.open("foo.txt")
92 | f = gzip.open("foo.txt") 92 | f = bz2.open("foo.txt")
| ^^^^^^^^^ SIM115 | ^^^^^^^^ SIM115
93 | f = dbm.open("foo.db") 93 | f = gzip.open("foo.txt")
94 | f = dbm.gnu.open("foo.db") 94 | f = dbm.open("foo.db")
| |
SIM115.py:93:5: SIM115 Use a context manager for opening files SIM115.py:93:5: SIM115 Use a context manager for opening files
| |
91 | f = bz2.open("foo.txt") 91 | f = codecs.open("foo.txt")
92 | f = gzip.open("foo.txt") 92 | f = bz2.open("foo.txt")
93 | f = dbm.open("foo.db") 93 | f = gzip.open("foo.txt")
| ^^^^^^^^ SIM115 | ^^^^^^^^^ SIM115
94 | f = dbm.gnu.open("foo.db") 94 | f = dbm.open("foo.db")
95 | f = dbm.ndbm.open("foo.db") 95 | f = dbm.gnu.open("foo.db")
| |
SIM115.py:94:5: SIM115 Use a context manager for opening files SIM115.py:94:5: SIM115 Use a context manager for opening files
| |
92 | f = gzip.open("foo.txt") 92 | f = bz2.open("foo.txt")
93 | f = dbm.open("foo.db") 93 | f = gzip.open("foo.txt")
94 | f = dbm.gnu.open("foo.db") 94 | f = dbm.open("foo.db")
| ^^^^^^^^^^^^ SIM115 | ^^^^^^^^ SIM115
95 | f = dbm.ndbm.open("foo.db") 95 | f = dbm.gnu.open("foo.db")
96 | f = dbm.dumb.open("foo.db") 96 | f = dbm.ndbm.open("foo.db")
| |
SIM115.py:95:5: SIM115 Use a context manager for opening files SIM115.py:95:5: SIM115 Use a context manager for opening files
| |
93 | f = dbm.open("foo.db") 93 | f = gzip.open("foo.txt")
94 | f = dbm.gnu.open("foo.db") 94 | f = dbm.open("foo.db")
95 | f = dbm.ndbm.open("foo.db") 95 | f = dbm.gnu.open("foo.db")
| ^^^^^^^^^^^^^ SIM115 | ^^^^^^^^^^^^ SIM115
96 | f = dbm.dumb.open("foo.db") 96 | f = dbm.ndbm.open("foo.db")
97 | f = lzma.open("foo.xz") 97 | f = dbm.dumb.open("foo.db")
| |
SIM115.py:96:5: SIM115 Use a context manager for opening files SIM115.py:96:5: SIM115 Use a context manager for opening files
| |
94 | f = dbm.gnu.open("foo.db") 94 | f = dbm.open("foo.db")
95 | f = dbm.ndbm.open("foo.db") 95 | f = dbm.gnu.open("foo.db")
96 | f = dbm.dumb.open("foo.db") 96 | f = dbm.ndbm.open("foo.db")
| ^^^^^^^^^^^^^ SIM115 | ^^^^^^^^^^^^^ SIM115
97 | f = lzma.open("foo.xz") 97 | f = dbm.dumb.open("foo.db")
98 | f = lzma.LZMAFile("foo.xz") 98 | f = lzma.open("foo.xz")
| |
SIM115.py:97:5: SIM115 Use a context manager for opening files SIM115.py:97:5: SIM115 Use a context manager for opening files
| |
95 | f = dbm.ndbm.open("foo.db") 95 | f = dbm.gnu.open("foo.db")
96 | f = dbm.dumb.open("foo.db") 96 | f = dbm.ndbm.open("foo.db")
97 | f = lzma.open("foo.xz") 97 | f = dbm.dumb.open("foo.db")
| ^^^^^^^^^ SIM115 | ^^^^^^^^^^^^^ SIM115
98 | f = lzma.LZMAFile("foo.xz") 98 | f = lzma.open("foo.xz")
99 | f = shelve.open("foo.db") 99 | f = lzma.LZMAFile("foo.xz")
| |
SIM115.py:98:5: SIM115 Use a context manager for opening files SIM115.py:98:5: SIM115 Use a context manager for opening files
| |
96 | f = dbm.dumb.open("foo.db") 96 | f = dbm.ndbm.open("foo.db")
97 | f = lzma.open("foo.xz") 97 | f = dbm.dumb.open("foo.db")
98 | f = lzma.LZMAFile("foo.xz") 98 | f = lzma.open("foo.xz")
| ^^^^^^^^^^^^^ SIM115 | ^^^^^^^^^ SIM115
99 | f = shelve.open("foo.db") 99 | f = lzma.LZMAFile("foo.xz")
100 | f = tokenize.open("foo.py") 100 | f = shelve.open("foo.db")
| |
SIM115.py:99:5: SIM115 Use a context manager for opening files SIM115.py:99:5: SIM115 Use a context manager for opening files
| |
97 | f = lzma.open("foo.xz") 97 | f = dbm.dumb.open("foo.db")
98 | f = lzma.LZMAFile("foo.xz") 98 | f = lzma.open("foo.xz")
99 | f = shelve.open("foo.db") 99 | f = lzma.LZMAFile("foo.xz")
| ^^^^^^^^^^^ SIM115 | ^^^^^^^^^^^^^ SIM115
100 | f = tokenize.open("foo.py") 100 | f = shelve.open("foo.db")
101 | f = wave.open("foo.wav") 101 | f = tokenize.open("foo.py")
| |
SIM115.py:100:5: SIM115 Use a context manager for opening files SIM115.py:100:5: SIM115 Use a context manager for opening files
| |
98 | f = lzma.LZMAFile("foo.xz") 98 | f = lzma.open("foo.xz")
99 | f = shelve.open("foo.db") 99 | f = lzma.LZMAFile("foo.xz")
100 | f = tokenize.open("foo.py") 100 | f = shelve.open("foo.db")
| ^^^^^^^^^^^^^ SIM115 | ^^^^^^^^^^^ SIM115
101 | f = wave.open("foo.wav") 101 | f = tokenize.open("foo.py")
102 | f = tarfile.TarFile.taropen("foo.tar") 102 | f = wave.open("foo.wav")
| |
SIM115.py:101:5: SIM115 Use a context manager for opening files SIM115.py:101:5: SIM115 Use a context manager for opening files
| |
99 | f = shelve.open("foo.db") 99 | f = lzma.LZMAFile("foo.xz")
100 | f = tokenize.open("foo.py") 100 | f = shelve.open("foo.db")
101 | f = wave.open("foo.wav") 101 | f = tokenize.open("foo.py")
| ^^^^^^^^^ SIM115 | ^^^^^^^^^^^^^ SIM115
102 | f = tarfile.TarFile.taropen("foo.tar") 102 | f = wave.open("foo.wav")
103 | f = fileinput.input("foo.txt") 103 | f = tarfile.TarFile.taropen("foo.tar")
| |
SIM115.py:102:5: SIM115 Use a context manager for opening files SIM115.py:102:5: SIM115 Use a context manager for opening files
| |
100 | f = tokenize.open("foo.py") 100 | f = shelve.open("foo.db")
101 | f = wave.open("foo.wav") 101 | f = tokenize.open("foo.py")
102 | f = tarfile.TarFile.taropen("foo.tar") 102 | f = wave.open("foo.wav")
| ^^^^^^^^^^^^^^^^^^^^^^^ SIM115 | ^^^^^^^^^ SIM115
103 | f = fileinput.input("foo.txt") 103 | f = tarfile.TarFile.taropen("foo.tar")
104 | f = fileinput.FileInput("foo.txt") 104 | f = fileinput.input("foo.txt")
| |
SIM115.py:103:5: SIM115 Use a context manager for opening files SIM115.py:103:5: SIM115 Use a context manager for opening files
| |
101 | f = wave.open("foo.wav") 101 | f = tokenize.open("foo.py")
102 | f = tarfile.TarFile.taropen("foo.tar") 102 | f = wave.open("foo.wav")
103 | f = fileinput.input("foo.txt") 103 | f = tarfile.TarFile.taropen("foo.tar")
| ^^^^^^^^^^^^^^^ SIM115 | ^^^^^^^^^^^^^^^^^^^^^^^ SIM115
104 | f = fileinput.FileInput("foo.txt") 104 | f = fileinput.input("foo.txt")
105 | f = fileinput.FileInput("foo.txt")
| |
SIM115.py:104:5: SIM115 Use a context manager for opening files SIM115.py:104:5: SIM115 Use a context manager for opening files
| |
102 | f = tarfile.TarFile.taropen("foo.tar") 102 | f = wave.open("foo.wav")
103 | f = fileinput.input("foo.txt") 103 | f = tarfile.TarFile.taropen("foo.tar")
104 | f = fileinput.FileInput("foo.txt") 104 | f = fileinput.input("foo.txt")
| ^^^^^^^^^^^^^^^ SIM115
105 | f = fileinput.FileInput("foo.txt")
|
SIM115.py:105:5: SIM115 Use a context manager for opening files
|
103 | f = tarfile.TarFile.taropen("foo.tar")
104 | f = fileinput.input("foo.txt")
105 | f = fileinput.FileInput("foo.txt")
| ^^^^^^^^^^^^^^^^^^^ SIM115 | ^^^^^^^^^^^^^^^^^^^ SIM115
105 | 106 |
106 | with contextlib.suppress(Exception): 107 | with contextlib.suppress(Exception):
| |
SIM115.py:240:9: SIM115 Use a context manager for opening files SIM115.py:241:9: SIM115 Use a context manager for opening files
| |
238 | def aliased(): 239 | def aliased():
239 | from shelve import open as open_shelf 240 | from shelve import open as open_shelf
240 | x = open_shelf("foo.dbm") 241 | x = open_shelf("foo.dbm")
| ^^^^^^^^^^ SIM115 | ^^^^^^^^^^ SIM115
241 | x.close() 242 | x.close()
| |
SIM115.py:244:9: SIM115 Use a context manager for opening files SIM115.py:245:9: SIM115 Use a context manager for opening files
| |
243 | from tarfile import TarFile as TF 244 | from tarfile import TarFile as TF
244 | f = TF("foo").open() 245 | f = TF("foo").open()
| ^^^^^^^^^^^^^^ SIM115 | ^^^^^^^^^^^^^^ SIM115
245 | f.close() 246 | f.close()
| |
SIM115.py:257:5: SIM115 Use a context manager for opening files SIM115.py:258:5: SIM115 Use a context manager for opening files
| |
256 | # SIM115 257 | # SIM115
257 | f = dbm.sqlite3.open("foo.db") 258 | f = dbm.sqlite3.open("foo.db")
| ^^^^^^^^^^^^^^^^ SIM115 | ^^^^^^^^^^^^^^^^ SIM115
258 | f.close() 259 | f.close()
| |

View file

@ -1,110 +1,110 @@
--- ---
source: crates/ruff_linter/src/rules/flake8_simplify/mod.rs source: crates/ruff_linter/src/rules/flake8_simplify/mod.rs
--- ---
SIM116.py:5:1: SIM116 Use a dictionary instead of consecutive `if` statements SIM116.py:6:5: SIM116 Use a dictionary instead of consecutive `if` statements
| |
4 | # SIM116 5 | # SIM116
5 | / if a == "foo": 6 | / if a == "foo":
6 | | return "bar" 7 | | return "bar"
7 | | elif a == "bar": 8 | | elif a == "bar":
8 | | return "baz" 9 | | return "baz"
9 | | elif a == "boo": 10 | | elif a == "boo":
10 | | return "ooh" 11 | | return "ooh"
11 | | else: 12 | | else:
12 | | return 42 13 | | return 42
| |_____________^ SIM116 | |_________________^ SIM116
13 | 14 |
14 | # SIM116 15 | # SIM116
| |
SIM116.py:15:1: SIM116 Use a dictionary instead of consecutive `if` statements SIM116.py:16:5: SIM116 Use a dictionary instead of consecutive `if` statements
| |
14 | # SIM116 15 | # SIM116
15 | / if a == 1: 16 | / if a == 1:
16 | | return (1, 2, 3) 17 | | return (1, 2, 3)
17 | | elif a == 2: 18 | | elif a == 2:
18 | | return (4, 5, 6) 19 | | return (4, 5, 6)
19 | | elif a == 3: 20 | | elif a == 3:
20 | | return (7, 8, 9) 21 | | return (7, 8, 9)
21 | | else: 22 | | else:
22 | | return (10, 11, 12) 23 | | return (10, 11, 12)
| |_______________________^ SIM116 | |___________________________^ SIM116
23 | 24 |
24 | # SIM116 25 | # SIM116
| |
SIM116.py:25:1: SIM116 Use a dictionary instead of consecutive `if` statements SIM116.py:26:5: SIM116 Use a dictionary instead of consecutive `if` statements
| |
24 | # SIM116 25 | # SIM116
25 | / if a == 1: 26 | / if a == 1:
26 | | return (1, 2, 3) 27 | | return (1, 2, 3)
27 | | elif a == 2: 28 | | elif a == 2:
28 | | return (4, 5, 6) 29 | | return (4, 5, 6)
29 | | elif a == 3: 30 | | elif a == 3:
30 | | return (7, 8, 9) 31 | | return (7, 8, 9)
| |____________________^ SIM116 | |________________________^ SIM116
31 | 32 |
32 | # SIM116 33 | # SIM116
| |
SIM116.py:33:1: SIM116 Use a dictionary instead of consecutive `if` statements SIM116.py:34:5: SIM116 Use a dictionary instead of consecutive `if` statements
| |
32 | # SIM116 33 | # SIM116
33 | / if a == "hello 'sir'": 34 | / if a == "hello 'sir'":
34 | | return (1, 2, 3) 35 | | return (1, 2, 3)
35 | | elif a == 'goodbye "mam"': 36 | | elif a == 'goodbye "mam"':
36 | | return (4, 5, 6) 37 | | return (4, 5, 6)
37 | | elif a == """Fairwell 'mister'""": 38 | | elif a == """Fairwell 'mister'""":
38 | | return (7, 8, 9) 39 | | return (7, 8, 9)
39 | | else: 40 | | else:
40 | | return (10, 11, 12) 41 | | return (10, 11, 12)
| |_______________________^ SIM116 | |___________________________^ SIM116
41 | 42 |
42 | # SIM116 43 | # SIM116
| |
SIM116.py:43:1: SIM116 Use a dictionary instead of consecutive `if` statements SIM116.py:44:5: SIM116 Use a dictionary instead of consecutive `if` statements
| |
42 | # SIM116 43 | # SIM116
43 | / if a == b"one": 44 | / if a == b"one":
44 | | return 1 45 | | return 1
45 | | elif a == b"two": 46 | | elif a == b"two":
46 | | return 2 47 | | return 2
47 | | elif a == b"three": 48 | | elif a == b"three":
48 | | return 3 49 | | return 3
| |____________^ SIM116 | |________________^ SIM116
49 | 50 |
50 | # SIM116 51 | # SIM116
| |
SIM116.py:51:1: SIM116 Use a dictionary instead of consecutive `if` statements SIM116.py:52:5: SIM116 Use a dictionary instead of consecutive `if` statements
| |
50 | # SIM116 51 | # SIM116
51 | / if a == "hello 'sir'": 52 | / if a == "hello 'sir'":
52 | | return ("hello'", 'hi"', 3) 53 | | return ("hello'", 'hi"', 3)
53 | | elif a == 'goodbye "mam"': 54 | | elif a == 'goodbye "mam"':
54 | | return (4, 5, 6) 55 | | return (4, 5, 6)
55 | | elif a == """Fairwell 'mister'""": 56 | | elif a == """Fairwell 'mister'""":
56 | | return (7, 8, 9) 57 | | return (7, 8, 9)
57 | | else: 58 | | else:
58 | | return (10, 11, 12) 59 | | return (10, 11, 12)
| |_______________________^ SIM116 | |___________________________^ SIM116
59 | 60 |
60 | # OK 61 | # OK
| |
SIM116.py:79:1: SIM116 Use a dictionary instead of consecutive `if` statements SIM116.py:80:5: SIM116 Use a dictionary instead of consecutive `if` statements
| |
78 | # SIM116 79 | # SIM116
79 | / if func_name == "create": 80 | / if func_name == "create":
80 | | return "A" 81 | | return "A"
81 | | elif func_name == "modify": 82 | | elif func_name == "modify":
82 | | return "M" 83 | | return "M"
83 | | elif func_name == "remove": 84 | | elif func_name == "remove":
84 | | return "D" 85 | | return "D"
85 | | elif func_name == "move": 86 | | elif func_name == "move":
86 | | return "MV" 87 | | return "MV"
| |_______________^ SIM116 | |___________________^ SIM116
87 | 88 |
88 | # OK 89 | # OK
| |