Skip to content

Commit

Permalink
[undefined-variable] Fix a crash for undefined lineno in annotations (#…
Browse files Browse the repository at this point in the history
…9705) (#9706)

(cherry picked from commit f082174)

Co-authored-by: Pierre Sassoulas <[email protected]>
  • Loading branch information
github-actions[bot] and Pierre-Sassoulas authored Jun 6, 2024
1 parent 918d216 commit 6b66ca6
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 7 deletions.
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

0 comments on commit 6b66ca6

Please sign in to comment.