-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Add suggested fix for TRY201
#6008
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ad72d0c
Make `TRY201` always autofixable
dhruvmanila 7ca46c1
Generate an empty `raise` stmt for the fix
dhruvmanila 956fa4e
Simplify edit, make it suggested
dhruvmanila ffe8f13
Update snapshots
dhruvmanila 7f60ef0
Simplify fix as unused variable will be removed automatically
dhruvmanila 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
This file contains 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
49 changes: 46 additions & 3 deletions
49
...rules/tryceratops/snapshots/ruff__rules__tryceratops__tests__verbose-raise_TRY201.py.snap
This file contains 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 |
---|---|---|
@@ -1,27 +1,70 @@ | ||
--- | ||
source: crates/ruff/src/rules/tryceratops/mod.rs | ||
--- | ||
TRY201.py:20:15: TRY201 Use `raise` without specifying exception name | ||
TRY201.py:20:15: TRY201 [*] Use `raise` without specifying exception name | ||
| | ||
18 | except MyException as e: | ||
19 | logger.exception("process failed") | ||
20 | raise e | ||
| ^ TRY201 | ||
| | ||
= help: Remove exception name | ||
|
||
TRY201.py:63:19: TRY201 Use `raise` without specifying exception name | ||
ℹ Fix | ||
15 15 | def bad(): | ||
16 16 | try: | ||
17 17 | process() | ||
18 |- except MyException as e: | ||
18 |+ except MyException: | ||
19 19 | logger.exception("process failed") | ||
20 |- raise e | ||
20 |+ raise | ||
21 21 | | ||
22 22 | | ||
23 23 | def good(): | ||
|
||
TRY201.py:63:19: TRY201 [*] Use `raise` without specifying exception name | ||
| | ||
61 | logger.exception("process failed") | ||
62 | if True: | ||
63 | raise e | ||
| ^ TRY201 | ||
| | ||
= help: Remove exception name | ||
|
||
ℹ Fix | ||
57 57 | def bad_that_needs_recursion(): | ||
58 58 | try: | ||
59 59 | process() | ||
60 |- except MyException as e: | ||
60 |+ except MyException: | ||
61 61 | logger.exception("process failed") | ||
62 62 | if True: | ||
63 |- raise e | ||
63 |+ raise | ||
64 64 | | ||
65 65 | | ||
66 66 | def bad_that_needs_recursion_2(): | ||
|
||
TRY201.py:74:23: TRY201 Use `raise` without specifying exception name | ||
TRY201.py:74:23: TRY201 [*] Use `raise` without specifying exception name | ||
| | ||
73 | def foo(): | ||
74 | raise e | ||
| ^ TRY201 | ||
| | ||
= help: Remove exception name | ||
|
||
ℹ Fix | ||
66 66 | def bad_that_needs_recursion_2(): | ||
67 67 | try: | ||
68 68 | process() | ||
69 |- except MyException as e: | ||
69 |+ except MyException: | ||
70 70 | logger.exception("process failed") | ||
71 71 | if True: | ||
72 72 | | ||
73 73 | def foo(): | ||
74 |- raise e | ||
74 |+ raise | ||
|
||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this be simplified to:
Edit::range_replacement("raise".to_string(), raise.range())
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it should be "suggested" because this rule can have some false positives. I assume it gets this wrong:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh yes, I had that case in mind and it does gets it wrong. I was wondering if our semantic model can resolve such references (I can't find anything on that). I'll make this a suggested fix.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we change to
Edit::range_replacement("raise".to_string(), raise.range())
too, or does that not work?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh we need to delete
as e
too, I see. Does this rule verify thate
isn't used in the exception body? Like:In that case, we can change
raise e
toraise
, but we can't removeas e
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, we don't really check that case. I don't think we can use the semantic model to verify such instances so I'll probably have to check either using a
StatementVisitor
orany_over_stmt
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this fix should only remove
e
inraise e
. If the variable is unused, it'll get marked in the next pass as an unused variable and removed via the unused variable autofix.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh right, that makes sense. I'll just add a test case for it then and remove the first edit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good. Eventually we can improve this rule to handle it properly in the first place.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, this is interesting: Ruff is detecting that the variable
e
is unused in the last test case which seems incorrect: