Fix class scope issues in undefined-variable checker #511 #1976 #3494 #3497
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.
Steps
doc/whatsnew/<current release.rst>
.Description
The
_ignore_class_scope
method within the variable checker determines whether to search attributes in the class scope. This in turn uses_defined_in_function_definition
to distinguish function arguments and annotations from the contents of the function (which form their own scope). However,_defined_in_function_definition
was previously missing checks to search decorators (#511) and return type annotations (#1976), leading to false positives. These two checks were an easy fix.While implementing the solution, it became apparent that Pylint wasn't checking whether variables inside list/set/dict comprehensions and generator expressions could actually access class scope. This led to false negatives where Python would raise a
NameError
, but Pylint wasn't providing any warning (#3494). A new function_in_lambda_or_comprehension_body
was added that begins with a variable node, then searches up its parent chain, checking for lambda expressions and comprehensions along the way that would cause class attributes to go out of scope. Only the lambda arguments, and comprehension iters are allowed to pass through.tests/functional/c/class_scope.py
was changed to note thatlambda arg=revattr: revattr * 42
usesrevattr
as both an argument, as well as anundefined-variable
in the body. An additional line was also added to test the scope issues were addressed for generator expressions.Type of Changes
Related Issue
Closes #511
Closes #1976
Closes #3494