Skip to content

Commit 4213b3c

Browse files
Fix handling of "for x in x" homonyms (#6154)
Co-authored-by: Pierre Sassoulas <[email protected]>
1 parent 22b5dc1 commit 4213b3c

File tree

5 files changed

+14
-4
lines changed

5 files changed

+14
-4
lines changed

ChangeLog

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Release date: TBA
2525
subscripts in comprehensions.
2626

2727
Closes #6069
28+
Closes #6136
2829

2930
* Narrow the scope of the ``unnecessary-ellipsis`` checker to:
3031
* functions & classes which contain both a docstring and an ellipsis.

pylint/checkers/variables.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -2300,11 +2300,14 @@ def _check_is_unused(
23002300
if global_names and _import_name_is_global(stmt, global_names):
23012301
return
23022302

2303+
# Ignore names in comprehension targets
2304+
if name in comprehension_target_names:
2305+
return
2306+
23032307
argnames = node.argnames()
23042308
# Care about functions with unknown argument (builtins)
23052309
if name in argnames:
2306-
if name not in comprehension_target_names:
2307-
self._check_unused_arguments(name, node, stmt, argnames, nonlocal_names)
2310+
self._check_unused_arguments(name, node, stmt, argnames, nonlocal_names)
23082311
else:
23092312
if stmt.parent and isinstance(
23102313
stmt.parent, (nodes.Assign, nodes.AnnAssign, nodes.Tuple)

tests/functional/u/undefined/undefined_variable_py38.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ def type_annotation_used_after_comprehension():
123123

124124
def type_annotation_unused_after_comprehension():
125125
"""https://github.com/PyCQA/pylint/issues/5326"""
126-
my_int: int # [unused-variable]
126+
my_int: int
127127
_ = [print(sep=my_int, end=my_int) for my_int in range(10)]
128128

129129

tests/functional/u/undefined/undefined_variable_py38.txt

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ undefined-variable:42:6:42:16::Undefined variable 'no_default':UNDEFINED
22
undefined-variable:50:6:50:22::Undefined variable 'again_no_default':UNDEFINED
33
undefined-variable:76:6:76:19::Undefined variable 'else_assign_1':INFERENCE
44
undefined-variable:99:6:99:19::Undefined variable 'else_assign_2':INFERENCE
5-
unused-variable:126:4:126:10:type_annotation_unused_after_comprehension:Unused variable 'my_int':UNDEFINED
65
used-before-assignment:134:10:134:16:type_annotation_used_improperly_after_comprehension:Using variable 'my_int' before assignment:HIGH
76
used-before-assignment:141:10:141:16:type_annotation_used_improperly_after_comprehension_2:Using variable 'my_int' before assignment:HIGH
87
used-before-assignment:171:12:171:16:expression_in_ternary_operator_inside_container_wrong_position:Using variable 'val3' before assignment:HIGH

tests/functional/u/unused/unused_variable.py

+7
Original file line numberDiff line numberDiff line change
@@ -172,3 +172,10 @@ def main(lst):
172172
print(e) # [undefined-loop-variable]
173173

174174
main([])
175+
176+
177+
def func5():
178+
"""No unused-variable for a container if iterated in comprehension"""
179+
x = []
180+
# Test case requires homonym between "for x" and "in x"
181+
assert [True for x in x]

0 commit comments

Comments
 (0)