Skip to content

Commit ea55023

Browse files
Fix a crash in undefined-loop-variable with enumerate() (#9876)
(cherry picked from commit cb6db06)
1 parent f1925f4 commit ea55023

File tree

4 files changed

+13
-1
lines changed

4 files changed

+13
-1
lines changed

doc/whatsnew/fragments/9875.bugfix

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix a crash in ``undefined-loop-variable`` when providing the ``iterable`` argument to ``enumerate()``.
2+
3+
Closes #9875

pylint/checkers/variables.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2683,7 +2683,7 @@ def _loopvar_name(self, node: astroid.Name) -> None:
26832683
likely_call = assign.iter
26842684
if isinstance(assign.iter, nodes.IfExp):
26852685
likely_call = assign.iter.body
2686-
if isinstance(likely_call, nodes.Call):
2686+
if isinstance(likely_call, nodes.Call) and likely_call.args:
26872687
inferred = next(likely_call.args[0].infer())
26882688
except astroid.InferenceError:
26892689
self.add_message("undefined-loop-variable", args=node.name, node=node)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
"""https://github.com/pylint-dev/pylint/issues/9875"""
2+
# value = 0
3+
for idx, value in enumerate(iterable=[1, 2, 3]):
4+
print(f'{idx=} {value=}')
5+
# +1: [undefined-loop-variable, undefined-loop-variable]
6+
for idx, value in enumerate(iterable=[value-1, value-2*1]):
7+
print(f'{idx=} {value=}')
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
undefined-loop-variable:6:38:6:43::Using possibly undefined loop variable 'value':UNDEFINED
2+
undefined-loop-variable:6:47:6:52::Using possibly undefined loop variable 'value':UNDEFINED

0 commit comments

Comments
 (0)