-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Fix false negative for used-before-assignment (ExceptHandler) #4791
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
Merged
Changes from all commits
Commits
Show all changes
3 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# pylint: disable=missing-docstring,invalid-name | ||
def main1(): | ||
try: | ||
raise ValueError | ||
except ValueError as e: # [unused-variable] | ||
pass | ||
|
||
print(e) # [used-before-assignment] | ||
|
||
|
||
def main2(): | ||
try: | ||
raise ValueError | ||
except ValueError as e: | ||
print(e) | ||
|
||
|
||
def main3(): | ||
try: | ||
raise ValueError | ||
except ValueError as e: # [unused-variable] | ||
pass | ||
|
||
e = 10 | ||
print(e) | ||
|
||
|
||
def main4(): | ||
try: | ||
raise ValueError | ||
except ValueError as e: # [unused-variable] | ||
pass | ||
|
||
try: | ||
raise ValueError | ||
except ValueError as e: | ||
pass | ||
|
||
try: | ||
raise ValueError | ||
except ValueError as e: | ||
pass | ||
|
||
print(e) # [used-before-assignment] |
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,5 @@ | ||
unused-variable:5:4:main1:Unused variable 'e' | ||
used-before-assignment:8:10:main1:Using variable 'e' before assignment | ||
unused-variable:21:4:main3:Unused variable 'e' | ||
unused-variable:31:4:main4:Unused variable 'e' | ||
used-before-assignment:44:10:main4:Using variable 'e' before assignment |
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.
I think it also closes #4391?
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.
Unfortunately it doesn't---the changes don't handle shadowing:
You can also see that there are tests in
tests/functional/u/unused/unused_variable.py:108-158
that still have TODOs in them.Both occurrences of the
as error
are part of the same consumer in "to_consume
", and they're both removed when theerror
astroid.Name
node in the inner block is visited. I can think of a couple of different ways of fixing this, but none that are particularly easy/elegant. I can point out that we get a similar issue with loop variable shadowing:Pylint currently reports a
redefined-outer-name
error for the inneri
loop variable, but does not reportunused-variable
for the outeri
loop variable. Theredefined-outer-name
check is handled in a separatevisit_for
method; I could do something similar invisit_excepthandler
if you want.P.S. I'll reply to your general review comment later, need to grab lunch now!
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.
Thank you for your very clear answer !