Fix scoping for function annotations, decorators and base classes #3713
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
Description
This is a follow up to PR #3497 Fix class scope issues in undefined-variable checker. The earlier PR fixed false positives in which function annotations and decorators were being incorrectly reported as unable to access the class scope due to being incorrectly treated as within the function. This PR fixes false negatives that were caused by still searching the function scope.
Issue #1082, while reported as a false positive for
used-before-assignment
is really due to a false negative forundefined-variable
. Issue #3461 was partially addressed by PR #3497, and this PR fixes the false negative case where we were not reporting type hints that reference the function parameterself
(there are still lots of other open type hint issues, but this fixes the specific case reported). Issue #3434 was due to skipping only a single scope level when checking a class's base class list, when there could in fact be an arbitrary number of nested list comprehensions in between. This logic has been moved to a per-scope check similar to the way function annotations and decorators are now checked.To implement this fix, it was necessary to keep track of the frame/scope
node
inNamesConsumer
, as this may be different to the immediate frame/scope of the variable being checked.Type of Changes
Related Issue
Close #1082, #3434, #3461