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

Fix class scope issues in undefined-variable checker #511 #1976 #3494 #3497

Merged

Conversation

anjsimmo
Copy link
Contributor

Steps

  • Add yourself to CONTRIBUTORS if you are a new contributor.
  • Add a ChangeLog entry describing what your PR does.
  • If it's a new feature or an important bug fix, add a What's New entry in doc/whatsnew/<current release.rst>.
  • Write a good description on what the PR does.

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 that lambda arg=revattr: revattr * 42 uses revattr as both an argument, as well as an undefined-variable in the body. An additional line was also added to test the scope issues were addressed for generator expressions.

Type of Changes

Type
🐛 Bug fix

Related Issue

Closes #511
Closes #1976
Closes #3494

@coveralls
Copy link

coveralls commented Apr 20, 2020

Coverage Status

Coverage decreased (-0.01%) to 90.442% when pulling 34b0abd on anjsimmo:fix-undefined-variable-scoping into 67d0572 on PyCQA:master.

@PCManticore
Copy link
Contributor

Hey @anjsimmo This is a fantastic patch, thanks a lot for sending it in! I don't have any comments since it was clear and straightforward. Thanks for squashing 3 bugs all at once.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants