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

[Backport maintenance/3.2.x] [undefined-variable] Fix a crash for undefined lineno in annotations #9706

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions doc/whatsnew/fragments/8866.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fixed a crash when the lineno of a variable used as an annotation wasn't available for ``undefined-variable``.

Closes #8866
15 changes: 8 additions & 7 deletions pylint/checkers/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -2295,13 +2295,14 @@ def _is_variable_violation(
# using a name defined earlier in the class containing the function.
if node is frame.returns and defframe.parent_of(frame.returns):
annotation_return = True
if (
frame.returns.name in defframe.locals
and defframe.locals[node.name][0].lineno < frame.lineno
):
# Detect class assignments with a name defined earlier in the
# class. In this case, no warning should be raised.
maybe_before_assign = False
if frame.returns.name in defframe.locals:
definition = defframe.locals[node.name][0]
if definition.lineno is None or definition.lineno < frame.lineno:
# Detect class assignments with a name defined earlier in the
# class. In this case, no warning should be raised.
maybe_before_assign = False
else:
maybe_before_assign = True
else:
maybe_before_assign = True
if isinstance(node.parent, nodes.Arguments):
Expand Down
4 changes: 4 additions & 0 deletions tests/functional/u/undefined/undefined_variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,3 +380,7 @@ def y(self) -> RepeatedReturnAnnotations: # [undefined-variable]
pass
def z(self) -> RepeatedReturnAnnotations: # [undefined-variable]
pass

class A:
def say_hello(self) -> __module__: # [undefined-variable]
...
1 change: 1 addition & 0 deletions tests/functional/u/undefined/undefined_variable.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,4 @@ undefined-variable:365:10:365:20:global_var_mixed_assignment:Undefined variable
undefined-variable:377:19:377:44:RepeatedReturnAnnotations.x:Undefined variable 'RepeatedReturnAnnotations':UNDEFINED
undefined-variable:379:19:379:44:RepeatedReturnAnnotations.y:Undefined variable 'RepeatedReturnAnnotations':UNDEFINED
undefined-variable:381:19:381:44:RepeatedReturnAnnotations.z:Undefined variable 'RepeatedReturnAnnotations':UNDEFINED
undefined-variable:385:27:385:37:A.say_hello:Undefined variable '__module__':UNDEFINED
Loading