Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix handling of "for x in x" homonyms #6154

Merged
merged 3 commits into from Apr 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions ChangeLog
Expand Up @@ -65,6 +65,7 @@ Release date: TBA
subscripts in comprehensions.

Closes #6069
Closes #6136
Pierre-Sassoulas marked this conversation as resolved.
Show resolved Hide resolved

* 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 @@ -2305,11 +2305,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
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This message was added in #5711 without much conviction. It probably shouldn't be here at all. Removing this is a better fix for the issue in #5326, which is a ticket I opened to say hey we started emitting a message here!

_ = [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]