From a1a9a50fe306e3217fd12cefaf036b57b9a0b1c3 Mon Sep 17 00:00:00 2001 From: Jacob Walls Date: Sun, 3 Apr 2022 00:33:59 -0400 Subject: [PATCH 1/3] Fix handling of "for x in x" homonyms --- ChangeLog | 5 +++++ pylint/checkers/variables.py | 7 +++++-- tests/functional/u/undefined/undefined_variable_py38.py | 2 +- tests/functional/u/undefined/undefined_variable_py38.txt | 1 - tests/functional/u/unused/unused_variable.py | 7 +++++++ 5 files changed, 18 insertions(+), 4 deletions(-) diff --git a/ChangeLog b/ChangeLog index 8417475d86..2718a3d7c2 100644 --- a/ChangeLog +++ b/ChangeLog @@ -66,6 +66,11 @@ Release date: TBA Closes #6069 +* Fix false positive for ``unused-variable`` (in unreleased development) for + patterns like ``[_ for same_name in same_name]``. + + Closes #6136 + * Narrow the scope of the ``unnecessary-ellipsis`` checker to: * functions & classes which contain both a docstring and an ellipsis. * A body which contains an ellipsis ``nodes.Expr`` node & at least one other statement. diff --git a/pylint/checkers/variables.py b/pylint/checkers/variables.py index 9113408a71..e1c5e7770d 100644 --- a/pylint/checkers/variables.py +++ b/pylint/checkers/variables.py @@ -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) diff --git a/tests/functional/u/undefined/undefined_variable_py38.py b/tests/functional/u/undefined/undefined_variable_py38.py index 01e0201a81..93ba1eb2ba 100644 --- a/tests/functional/u/undefined/undefined_variable_py38.py +++ b/tests/functional/u/undefined/undefined_variable_py38.py @@ -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)] diff --git a/tests/functional/u/undefined/undefined_variable_py38.txt b/tests/functional/u/undefined/undefined_variable_py38.txt index 4cce67ecc6..e912cbbf98 100644 --- a/tests/functional/u/undefined/undefined_variable_py38.txt +++ b/tests/functional/u/undefined/undefined_variable_py38.txt @@ -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 diff --git a/tests/functional/u/unused/unused_variable.py b/tests/functional/u/unused/unused_variable.py index d5afd8f2bb..d037a386ee 100644 --- a/tests/functional/u/unused/unused_variable.py +++ b/tests/functional/u/unused/unused_variable.py @@ -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] From 84110e5017508676afed584346e7a95495c0400f Mon Sep 17 00:00:00 2001 From: Jacob Walls Date: Sun, 3 Apr 2022 08:14:27 -0400 Subject: [PATCH 2/3] Update ChangeLog Co-authored-by: Pierre Sassoulas --- ChangeLog | 5 ----- 1 file changed, 5 deletions(-) diff --git a/ChangeLog b/ChangeLog index 2718a3d7c2..52f4df3b19 100644 --- a/ChangeLog +++ b/ChangeLog @@ -65,12 +65,7 @@ Release date: TBA subscripts in comprehensions. Closes #6069 - -* Fix false positive for ``unused-variable`` (in unreleased development) for - patterns like ``[_ for same_name in same_name]``. - Closes #6136 - * Narrow the scope of the ``unnecessary-ellipsis`` checker to: * functions & classes which contain both a docstring and an ellipsis. * A body which contains an ellipsis ``nodes.Expr`` node & at least one other statement. From de066f1d72436139235b27b43a26cd53c75d1000 Mon Sep 17 00:00:00 2001 From: Pierre Sassoulas Date: Sun, 3 Apr 2022 15:18:10 +0200 Subject: [PATCH 3/3] Update ChangeLog --- ChangeLog | 1 + 1 file changed, 1 insertion(+) diff --git a/ChangeLog b/ChangeLog index 52f4df3b19..463b9e43d1 100644 --- a/ChangeLog +++ b/ChangeLog @@ -66,6 +66,7 @@ Release date: TBA Closes #6069 Closes #6136 + * Narrow the scope of the ``unnecessary-ellipsis`` checker to: * functions & classes which contain both a docstring and an ellipsis. * A body which contains an ellipsis ``nodes.Expr`` node & at least one other statement.