Skip to content

Commit 50ad118

Browse files
Fix handling of "for x in x" homonyms (#6154)
Co-authored-by: Pierre Sassoulas <[email protected]>
1 parent 30302f2 commit 50ad118

File tree

5 files changed

+14
-4
lines changed

5 files changed

+14
-4
lines changed

ChangeLog

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ Release date: TBA
6565
subscripts in comprehensions.
6666

6767
Closes #6069
68+
Closes #6136
6869

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

pylint/checkers/variables.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2305,11 +2305,14 @@ def _check_is_unused(
23052305
if global_names and _import_name_is_global(stmt, global_names):
23062306
return
23072307

2308+
# Ignore names in comprehension targets
2309+
if name in comprehension_target_names:
2310+
return
2311+
23082312
argnames = node.argnames()
23092313
# Care about functions with unknown argument (builtins)
23102314
if name in argnames:
2311-
if name not in comprehension_target_names:
2312-
self._check_unused_arguments(name, node, stmt, argnames, nonlocal_names)
2315+
self._check_unused_arguments(name, node, stmt, argnames, nonlocal_names)
23132316
else:
23142317
if stmt.parent and isinstance(
23152318
stmt.parent, (nodes.Assign, nodes.AnnAssign, nodes.Tuple)

tests/functional/u/undefined/undefined_variable_py38.py

Lines changed: 1 addition & 1 deletion
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

Lines changed: 0 additions & 1 deletion
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

Lines changed: 7 additions & 0 deletions
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)