-
Notifications
You must be signed in to change notification settings - Fork 2k
[ruff] New rule useless-finally (RUF072)
#24165
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
MichaReiser
merged 2 commits into
astral-sh:main
from
seroperson:i19158-useless-finally
Mar 25, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
172 changes: 172 additions & 0 deletions
172
crates/ruff_linter/resources/test/fixtures/ruff/RUF072.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,172 @@ | ||
| # Errors | ||
|
|
||
| # try/except/finally with pass | ||
| try: | ||
| foo() | ||
| except Exception: | ||
| bar() | ||
| finally: | ||
| pass | ||
|
|
||
| # try/except/finally with ellipsis | ||
| try: | ||
| foo() | ||
| except Exception: | ||
| bar() | ||
| finally: | ||
| ... | ||
|
|
||
| # try/except/else/finally with pass | ||
| try: | ||
| foo() | ||
| except Exception: | ||
| bar() | ||
| else: | ||
| baz() | ||
| finally: | ||
| pass | ||
|
|
||
| # bare try/finally with pass | ||
| try: | ||
| foo() | ||
| finally: | ||
| pass | ||
|
|
||
| # bare try/finally with ellipsis | ||
| try: | ||
| foo() | ||
| finally: | ||
| ... | ||
|
|
||
| # bare try/finally with multi-line body | ||
| try: | ||
| foo() | ||
| bar() | ||
| baz() | ||
| finally: | ||
| pass | ||
|
|
||
| # Nested try with useless finally | ||
| try: | ||
| try: | ||
| foo() | ||
| finally: | ||
| pass | ||
| except Exception: | ||
| bar() | ||
|
|
||
| # finally with two pass statements | ||
| try: | ||
| foo() | ||
| except Exception: | ||
| bar() | ||
| finally: | ||
| pass | ||
| pass | ||
|
|
||
| # bare try/finally with pass and ellipsis | ||
| try: | ||
| foo() | ||
| finally: | ||
| pass | ||
| ... | ||
|
|
||
| # OK | ||
|
|
||
| # finally with real code | ||
| try: | ||
| foo() | ||
| finally: | ||
| cleanup() | ||
|
|
||
| # finally with pass and other statements | ||
| try: | ||
| foo() | ||
| finally: | ||
| pass | ||
| cleanup() | ||
|
|
||
| # No finally at all | ||
| try: | ||
| foo() | ||
| except Exception: | ||
| bar() | ||
|
|
||
| # Comments — diagnostic but no fix | ||
|
|
||
| # Comment on `finally:` line | ||
| try: | ||
| foo() | ||
| except Exception: | ||
| bar() | ||
| finally: # comment | ||
| pass | ||
|
|
||
| # Comment on `pass` line | ||
| try: | ||
| foo() | ||
| except Exception: | ||
| bar() | ||
| finally: | ||
| pass # comment | ||
|
|
||
| # Preceding own-line comment | ||
| try: | ||
| foo() | ||
| except Exception: | ||
| bar() | ||
| # comment | ||
| finally: | ||
| pass | ||
|
|
||
| # Trailing own-line comment | ||
| try: | ||
| foo() | ||
| except Exception: | ||
| bar() | ||
| finally: | ||
| pass | ||
| # comment | ||
|
|
||
| # Own-line comment before `pass` in the finally body | ||
| try: | ||
| foo() | ||
| except Exception: | ||
| bar() | ||
| finally: | ||
| # comment | ||
| pass | ||
|
|
||
| # Own-line comment extra-indented before `pass` | ||
| try: | ||
| foo() | ||
| except Exception: | ||
| bar() | ||
| finally: | ||
| # comment | ||
| pass | ||
|
|
||
| # Trailing comment indented one level (belongs to finally body) | ||
| try: | ||
| foo() | ||
| except Exception: | ||
| bar() | ||
| finally: | ||
| pass | ||
| # indented comment | ||
|
|
||
| # Trailing comment dedented one level (not part of finally, but | ||
| # immediately adjacent — suppresses fix conservatively) | ||
| try: | ||
| foo() | ||
| except Exception: | ||
| bar() | ||
| finally: | ||
| pass | ||
| # dedented comment | ||
|
|
||
| # Comment on bare try/finally | ||
| try: | ||
| foo() | ||
| finally: # comment | ||
| pass |
30 changes: 30 additions & 0 deletions
30
crates/ruff_linter/resources/test/fixtures/ruff/RUF072_RUF047.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| # RUF072 removes the empty `finally`, RUF047 removes the empty `else` | ||
| # Both fixes apply independently on the same try statement | ||
| try: | ||
| foo() | ||
| except Exception: | ||
| bar() | ||
| else: | ||
| pass | ||
| finally: | ||
| pass | ||
|
|
||
| # All non-body clauses are no-ops | ||
| try: | ||
| foo() | ||
| except Exception: | ||
| pass | ||
| else: | ||
| pass | ||
| finally: | ||
| pass | ||
|
|
||
| # Only the `finally` is empty; `else` has real code | ||
| try: | ||
| foo() | ||
| except Exception: | ||
| bar() | ||
| else: | ||
| baz() | ||
| finally: | ||
| pass | ||
10 changes: 10 additions & 0 deletions
10
crates/ruff_linter/resources/test/fixtures/ruff/RUF072_RUF047_SIM105.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| # All three non-body clauses are no-ops — after all rules converge, | ||
| # only `contextlib.suppress(Exception): foo()` remains | ||
| try: | ||
| foo() | ||
| except Exception: | ||
| pass | ||
| else: | ||
| pass | ||
| finally: | ||
| pass |
10 changes: 10 additions & 0 deletions
10
crates/ruff_linter/resources/test/fixtures/ruff/RUF072_SIM105.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| # SIM105 cannot fire while `finally` is non-empty | ||
| # RUF072 removes the empty `finally` first, then SIM105 rewrites | ||
| # the `try/except: pass` to `contextlib.suppress()` on the next pass | ||
| try: | ||
| foo() | ||
| except Exception: | ||
| pass | ||
| finally: | ||
| pass | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.