-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Fixes ''unused-argument`` false-positives with overshadowed variable in dictionary comprehension #1757
Conversation
…ume, consumed and scope_type informatios. Use of this class inside some visit/leave methods but more interestingly inside visit_name method, trying to make it a little bit clearer. Add of a method named _check_name_identical_to_args in order to deal with a name used inside function args and inside a comprehension. Deletion of _next_to_consume static method replaced by NamesConsumer.get_next_to_consume.
…NamesConsumer.get_next_to_consume signature
…r_function_scope. Adding docstring inside _has_homonym_in_upper_function_scope. Restricting the match in _has_homonym_in_upper_function_scope to function scope.
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.
@hippo91 this looks super straightforward! Thank you for tackling this issue. Left just a naming comment, please rebase it and let's get this merged.
pylint/checkers/variables.py
Outdated
@@ -336,6 +337,64 @@ def _assigned_locally(name_node): | |||
|
|||
} | |||
|
|||
|
|||
NamesConsumerAtomic = collections.namedtuple("NamesConsumerAtomic", |
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.
Maybe we can rename it to ScopeConsumer since it is used mostly to hold names belonging to a scope?
@PCManticore i merged it onto master. Should i backport it to 1.8? |
Sure :) |
@PCManticore do all bug corrections be backported to 1.8? |
This PR is an answer to #1731.
The
unused-argument
message is emitted from theleave_functiondef
method ofVariablesChecker
class.In the example given by @bokhi the wrong emission is due to the fact that the
not_consumed
variable holds a reference to the concerned argument whereas it should not.The
_to_consume
variable of theVariablesChecker
instance is not correct in this case. The origin of the problem is located inside thevisit_name
method.On line 1102, there is a test to know if the name has already been consumed. When checking for the name
key
corresponding to the node inside the dict comprehension (key.items()
) the answer is that it has been consumed because there is a member ofconsumed
which name iskey
. But this one corresponds to the variablekey
of the dict comprehension (for key, value in ...
). Due to this confusion the namekey
corresponding to the nodekey.items()
is not marked as consumed and thus leads to the emission of the false-positive message.I just added a test to avoid this confusion.
This test is inside the
_has_homonym_in_upper_function_scope
method which returns True if there is another member with the same name in an upper function scope.As i got some problems to understand this method i decided to introduce a new class named
NamesConsumer
. The_to_consume
variable is now a list ofNamesConsumer
instances instead of being a list of tuples with two first members being dictionaries and the third member a string.It allows also to have a better printing in debug sessions thanks to the
__repr__
method.I hope the method to be a little bit clearer with this change.
@PCManticore, has i do not yet fully understand this complex method could you please review this PR carefully (i'm sure you always do!). I just ensured that there are no more false positive and that i didn't break the unit tests.