Skip to content

Commit

Permalink
Fix handling of "for x in x" homonyms (#6154)
Browse files Browse the repository at this point in the history
Co-authored-by: Pierre Sassoulas <pierre.sassoulas@gmail.com>
  • Loading branch information
jacobtylerwalls and Pierre-Sassoulas committed Apr 4, 2022
1 parent 22b5dc1 commit 4213b3c
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 4 deletions.
1 change: 1 addition & 0 deletions ChangeLog
Expand Up @@ -25,6 +25,7 @@ Release date: TBA
subscripts in comprehensions.

Closes #6069
Closes #6136

* Narrow the scope of the ``unnecessary-ellipsis`` checker to:
* functions & classes which contain both a docstring and an ellipsis.
Expand Down
7 changes: 5 additions & 2 deletions pylint/checkers/variables.py
Expand Up @@ -2300,11 +2300,14 @@ def _check_is_unused(
if global_names and _import_name_is_global(stmt, global_names):
return

# Ignore names in comprehension targets
if name in comprehension_target_names:
return

argnames = node.argnames()
# Care about functions with unknown argument (builtins)
if name in argnames:
if name not in comprehension_target_names:
self._check_unused_arguments(name, node, stmt, argnames, nonlocal_names)
self._check_unused_arguments(name, node, stmt, argnames, nonlocal_names)
else:
if stmt.parent and isinstance(
stmt.parent, (nodes.Assign, nodes.AnnAssign, nodes.Tuple)
Expand Down
2 changes: 1 addition & 1 deletion tests/functional/u/undefined/undefined_variable_py38.py
Expand Up @@ -123,7 +123,7 @@ def type_annotation_used_after_comprehension():

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


Expand Down
1 change: 0 additions & 1 deletion tests/functional/u/undefined/undefined_variable_py38.txt
Expand Up @@ -2,7 +2,6 @@ undefined-variable:42:6:42:16::Undefined variable 'no_default':UNDEFINED
undefined-variable:50:6:50:22::Undefined variable 'again_no_default':UNDEFINED
undefined-variable:76:6:76:19::Undefined variable 'else_assign_1':INFERENCE
undefined-variable:99:6:99:19::Undefined variable 'else_assign_2':INFERENCE
unused-variable:126:4:126:10:type_annotation_unused_after_comprehension:Unused variable 'my_int':UNDEFINED
used-before-assignment:134:10:134:16:type_annotation_used_improperly_after_comprehension:Using variable 'my_int' before assignment:HIGH
used-before-assignment:141:10:141:16:type_annotation_used_improperly_after_comprehension_2:Using variable 'my_int' before assignment:HIGH
used-before-assignment:171:12:171:16:expression_in_ternary_operator_inside_container_wrong_position:Using variable 'val3' before assignment:HIGH
7 changes: 7 additions & 0 deletions tests/functional/u/unused/unused_variable.py
Expand Up @@ -172,3 +172,10 @@ def main(lst):
print(e) # [undefined-loop-variable]

main([])


def func5():
"""No unused-variable for a container if iterated in comprehension"""
x = []
# Test case requires homonym between "for x" and "in x"
assert [True for x in x]

0 comments on commit 4213b3c

Please sign in to comment.