Skip to content

Commit b376395

Browse files
Fix false positive for unused-variable for a comprehension variable matching a type annotation (#5651)
* Close #5326
1 parent f3ece3f commit b376395

File tree

5 files changed

+30
-4
lines changed

5 files changed

+30
-4
lines changed

ChangeLog

+5
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,11 @@ Release date: TBA
9999

100100
Closes #5461
101101

102+
* Fix false positive for ``unused-variable`` for a comprehension variable matching
103+
an outer scope type annotation.
104+
105+
Closes #5326
106+
102107
* Some files in ``pylint.testutils`` were deprecated. In the future imports should be done from the
103108
``pylint.testutils.functional`` namespace directly.
104109

doc/whatsnew/2.13.rst

+5
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,11 @@ Other Changes
131131

132132
Closes #5461
133133

134+
* Fix false positive for ``unused-variable`` for a comprehension variable matching
135+
an outer scope type annotation.
136+
137+
Closes #5326
138+
134139
* Require Python ``3.6.2`` to run pylint.
135140

136141
Closes #5065

pylint/checkers/variables.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1849,7 +1849,7 @@ def _is_only_type_assignment(node: nodes.Name, defstmt: nodes.Statement) -> bool
18491849
# Local refs are ordered, so we break.
18501850
# print(var)
18511851
# var = 1 # <- irrelevant
1852-
if defstmt_frame == node_frame and not ref_node.lineno < node.lineno:
1852+
if defstmt_frame == node_frame and ref_node.lineno > node.lineno:
18531853
break
18541854

18551855
# If the parent of the local reference is anything but an AnnAssign

tests/functional/u/undefined/undefined_variable_py38.py

+19-2
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ def typing_and_assignment_expression():
1111
print(var)
1212

1313

14-
def typing_and_self_referncing_assignment_expression():
14+
def typing_and_self_referencing_assignment_expression():
1515
"""The variable gets assigned in an assignment expression that references itself"""
1616
var: int
17-
if (var := var ** 2): # [undefined-variable]
17+
if (var := var ** 2): # false negative--walrus operator!
1818
print(var)
1919

2020

@@ -108,3 +108,20 @@ def no_parameters_in_function_default() -> None:
108108
if (x_0 := thing.this_value) < (x_1 := thing.that_value)
109109
else x_1,
110110
)
111+
112+
113+
# Tests for type annotation reused in comprehension
114+
115+
def type_annotation_used_after_comprehension():
116+
"""https://github.com/PyCQA/pylint/issues/5326#issuecomment-982635371"""
117+
my_int: int
118+
ints = [my_int + 1 for my_int in range(5)]
119+
120+
for my_int in ints:
121+
print(my_int)
122+
123+
124+
def type_annotation_unused_after_comprehension():
125+
"""https://github.com/PyCQA/pylint/issues/5326"""
126+
my_int: int
127+
_ = [print(kwarg1=my_int, kwarg2=my_int) for my_int in range(10)]

tests/functional/u/undefined/undefined_variable_py38.txt

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
undefined-variable:17:15:17:18:typing_and_self_referncing_assignment_expression:Undefined variable 'var':UNDEFINED
21
undefined-variable:42:6:42:16::Undefined variable 'no_default':UNDEFINED
32
undefined-variable:50:6:50:22::Undefined variable 'again_no_default':UNDEFINED
43
undefined-variable:76:6:76:19::Undefined variable 'else_assign_1':UNDEFINED

0 commit comments

Comments
 (0)