Skip to content

Commit

Permalink
Fix bug in detecting unused-variable when iterating on variable. (#…
Browse files Browse the repository at this point in the history
…7537)


Closes #3044
  • Loading branch information
clavedeluna committed Sep 28, 2022
1 parent 99e05cc commit 51a5850
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
3 changes: 3 additions & 0 deletions doc/whatsnew/fragments/3044.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix bug in detecting ``unused-variable`` when iterating on variable.

Closes #3044
2 changes: 1 addition & 1 deletion pylint/checkers/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -2388,7 +2388,7 @@ def _check_is_unused(
self._check_unused_arguments(name, node, stmt, argnames, nonlocal_names)
else:
if stmt.parent and isinstance(
stmt.parent, (nodes.Assign, nodes.AnnAssign, nodes.Tuple)
stmt.parent, (nodes.Assign, nodes.AnnAssign, nodes.Tuple, nodes.For)
):
if name in nonlocal_names:
return
Expand Down
13 changes: 13 additions & 0 deletions tests/functional/u/unused/unused_variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,3 +186,16 @@ def sibling_except_handlers():
pass
except ValueError as e:
print(e)

def func6():
a = 1

def nonlocal_writer():
nonlocal a

for a in range(10):
pass

nonlocal_writer()

assert a == 9, a

0 comments on commit 51a5850

Please sign in to comment.