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 bug in detecting unused-variable when iterating on variable. #7537

Merged
merged 2 commits into from
Sep 28, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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