-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Fix #5326: Fix false positive for unused-variable
for a comprehension variable matching a type annotation
#5651
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
Fix #5326: Fix false positive for unused-variable
for a comprehension variable matching a type annotation
#5651
Conversation
β¦ matching an outer scope type annotation Refs pylint-dev#5326
for more information, see https://pre-commit.ci
Pull Request Test Coverage Report for Build 1672085133
π - Coveralls |
Knowing you you have probably already investigated this, but it was impossible to fix this without creating the false negative? |
Ha. So my thought there was that it was wrong to make $ cat test.py
'''Should raise undefined-variable'''
if (var := var):
print(var)
$ pylint test.py
-------------------------------------------------------------------
Your code has been rated at 10.00/10 (previous run: 5.00/10, +5.00) I'm assuming that's the false negative you meant, since it was "created" instead of "left alone." (But "created" only insofar as we were getting lucky π² .) If you meant the false negative I started to describe in #5326 (comment), then to keep things sane I'll just break that out into a new issue. I think they can be addressed separately. |
unused-variable
for a comprehension variable matching a type annotationunused-variable
for a comprehension variable matching a type annotation
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.
Thanks a lot @jacobtylerwalls, very elegant fix !
@@ -1803,7 +1803,7 @@ def _is_only_type_assignment(node: nodes.Name, defstmt: nodes.Statement) -> bool | |||
# Local refs are ordered, so we break. | |||
# print(var) | |||
# var = 1 # <- irrelevant | |||
if defstmt_frame == node_frame and not ref_node.lineno < node.lineno: | |||
if defstmt_frame == node_frame and ref_node.lineno > node.lineno: |
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 like the small optimization to save a "not" on top of the clever fix !
Type of Changes
Description
Fixes the regression described in #5326 where
undefined-variable
is emitted when a comprehension variable matches a type annotation.One test result is edited to show that after these changes we will need better detection of walrus operators.
Fixes #5326