Skip to content
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

Merged
merged 14 commits into from
Jan 4, 2018

Conversation

hippo91
Copy link
Contributor

@hippo91 hippo91 commented Dec 3, 2017

This PR is an answer to #1731.

The unused-argument message is emitted from the leave_functiondef method of VariablesChecker 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 the VariablesChecker instance is not correct in this case. The origin of the problem is located inside the visit_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 of consumed which name is key. But this one corresponds to the variable key of the dict comprehension ( for key, value in ...). Due to this confusion the name key corresponding to the node key.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 of NamesConsumer 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.

…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.
…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.
@PCManticore PCManticore added the Needs review 🔍 Needs to be reviewed by one or multiple more persons label Dec 22, 2017
Copy link
Contributor

@PCManticore PCManticore left a 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.

@@ -336,6 +337,64 @@ def _assigned_locally(name_node):

}


NamesConsumerAtomic = collections.namedtuple("NamesConsumerAtomic",
Copy link
Contributor

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 PCManticore added reviewed-waiting-updates and removed Needs review 🔍 Needs to be reviewed by one or multiple more persons labels Jan 3, 2018
@hippo91 hippo91 merged commit 36cfe0c into pylint-dev:master Jan 4, 2018
@hippo91
Copy link
Contributor Author

hippo91 commented Jan 4, 2018

@PCManticore i merged it onto master. Should i backport it to 1.8?

@PCManticore
Copy link
Contributor

Sure :)

@hippo91
Copy link
Contributor Author

hippo91 commented Jan 4, 2018

@PCManticore do all bug corrections be backported to 1.8?

hippo91 added a commit to hippo91/pylint that referenced this pull request Jan 4, 2018
hippo91 added a commit that referenced this pull request Jan 4, 2018
@hippo91 hippo91 mentioned this pull request Feb 25, 2018
@hippo91 hippo91 deleted the bug_1731 branch June 20, 2020 11:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants