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

[Backport maintenance/3.2.x] Fix a crash in undefined-loop-variable with enumerate() #9877

Merged
merged 2 commits into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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/9875.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix a crash in ``undefined-loop-variable`` when providing the ``iterable`` argument to ``enumerate()``.

Closes #9875
2 changes: 1 addition & 1 deletion pylint/checkers/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -2683,7 +2683,7 @@ def _loopvar_name(self, node: astroid.Name) -> None:
likely_call = assign.iter
if isinstance(assign.iter, nodes.IfExp):
likely_call = assign.iter.body
if isinstance(likely_call, nodes.Call):
if isinstance(likely_call, nodes.Call) and likely_call.args:
inferred = next(likely_call.args[0].infer())
except astroid.InferenceError:
self.add_message("undefined-loop-variable", args=node.name, node=node)
Expand Down
7 changes: 7 additions & 0 deletions tests/functional/r/regression/regression_9875_enumerate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"""https://github.com/pylint-dev/pylint/issues/9875"""
# value = 0
for idx, value in enumerate(iterable=[1, 2, 3]):
print(f'{idx=} {value=}')
# +1: [undefined-loop-variable, undefined-loop-variable]
for idx, value in enumerate(iterable=[value-1, value-2*1]):
print(f'{idx=} {value=}')
2 changes: 2 additions & 0 deletions tests/functional/r/regression/regression_9875_enumerate.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
undefined-loop-variable:6:38:6:43::Using possibly undefined loop variable 'value':UNDEFINED
undefined-loop-variable:6:47:6:52::Using possibly undefined loop variable 'value':UNDEFINED
Loading