Skip to content

Commit 6b66ca6

Browse files
[undefined-variable] Fix a crash for undefined lineno in annotations (#9705) (#9706)
(cherry picked from commit f082174) Co-authored-by: Pierre Sassoulas <[email protected]>
1 parent 918d216 commit 6b66ca6

File tree

4 files changed

+16
-7
lines changed

4 files changed

+16
-7
lines changed

doc/whatsnew/fragments/8866.bugfix

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fixed a crash when the lineno of a variable used as an annotation wasn't available for ``undefined-variable``.
2+
3+
Closes #8866

pylint/checkers/variables.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2295,13 +2295,14 @@ def _is_variable_violation(
22952295
# using a name defined earlier in the class containing the function.
22962296
if node is frame.returns and defframe.parent_of(frame.returns):
22972297
annotation_return = True
2298-
if (
2299-
frame.returns.name in defframe.locals
2300-
and defframe.locals[node.name][0].lineno < frame.lineno
2301-
):
2302-
# Detect class assignments with a name defined earlier in the
2303-
# class. In this case, no warning should be raised.
2304-
maybe_before_assign = False
2298+
if frame.returns.name in defframe.locals:
2299+
definition = defframe.locals[node.name][0]
2300+
if definition.lineno is None or definition.lineno < frame.lineno:
2301+
# Detect class assignments with a name defined earlier in the
2302+
# class. In this case, no warning should be raised.
2303+
maybe_before_assign = False
2304+
else:
2305+
maybe_before_assign = True
23052306
else:
23062307
maybe_before_assign = True
23072308
if isinstance(node.parent, nodes.Arguments):

tests/functional/u/undefined/undefined_variable.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,3 +380,7 @@ def y(self) -> RepeatedReturnAnnotations: # [undefined-variable]
380380
pass
381381
def z(self) -> RepeatedReturnAnnotations: # [undefined-variable]
382382
pass
383+
384+
class A:
385+
def say_hello(self) -> __module__: # [undefined-variable]
386+
...

tests/functional/u/undefined/undefined_variable.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,4 @@ undefined-variable:365:10:365:20:global_var_mixed_assignment:Undefined variable
3737
undefined-variable:377:19:377:44:RepeatedReturnAnnotations.x:Undefined variable 'RepeatedReturnAnnotations':UNDEFINED
3838
undefined-variable:379:19:379:44:RepeatedReturnAnnotations.y:Undefined variable 'RepeatedReturnAnnotations':UNDEFINED
3939
undefined-variable:381:19:381:44:RepeatedReturnAnnotations.z:Undefined variable 'RepeatedReturnAnnotations':UNDEFINED
40+
undefined-variable:385:27:385:37:A.say_hello:Undefined variable '__module__':UNDEFINED

0 commit comments

Comments
 (0)