From ea55023b58d6e84d94b017be6be195610062b8d2 Mon Sep 17 00:00:00 2001 From: Jacob Walls Date: Fri, 16 Aug 2024 09:57:46 -0400 Subject: [PATCH 1/2] Fix a crash in `undefined-loop-variable` with `enumerate()` (#9876) (cherry picked from commit cb6db062ffcecc0928358d26c9c094da8aa0efaa) --- doc/whatsnew/fragments/9875.bugfix | 3 +++ pylint/checkers/variables.py | 2 +- tests/functional/r/regression/regression_9875_enumerate.py | 7 +++++++ .../functional/r/regression/regression_9875_enumerate.txt | 2 ++ 4 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 doc/whatsnew/fragments/9875.bugfix create mode 100644 tests/functional/r/regression/regression_9875_enumerate.py create mode 100644 tests/functional/r/regression/regression_9875_enumerate.txt diff --git a/doc/whatsnew/fragments/9875.bugfix b/doc/whatsnew/fragments/9875.bugfix new file mode 100644 index 0000000000..ee3da47204 --- /dev/null +++ b/doc/whatsnew/fragments/9875.bugfix @@ -0,0 +1,3 @@ +Fix a crash in ``undefined-loop-variable`` when providing the ``iterable`` argument to ``enumerate()``. + +Closes #9875 diff --git a/pylint/checkers/variables.py b/pylint/checkers/variables.py index 495051f315..0491b3b61e 100644 --- a/pylint/checkers/variables.py +++ b/pylint/checkers/variables.py @@ -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) diff --git a/tests/functional/r/regression/regression_9875_enumerate.py b/tests/functional/r/regression/regression_9875_enumerate.py new file mode 100644 index 0000000000..1eca3f7811 --- /dev/null +++ b/tests/functional/r/regression/regression_9875_enumerate.py @@ -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=}') diff --git a/tests/functional/r/regression/regression_9875_enumerate.txt b/tests/functional/r/regression/regression_9875_enumerate.txt new file mode 100644 index 0000000000..dad9a0f0aa --- /dev/null +++ b/tests/functional/r/regression/regression_9875_enumerate.txt @@ -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 From 83da6ea557470c171f18814c5895a5e2d532ab6c Mon Sep 17 00:00:00 2001 From: Jacob Walls Date: Fri, 16 Aug 2024 10:10:51 -0400 Subject: [PATCH 2/2] Move test file --- .../r/{regression => regression_02}/regression_9875_enumerate.py | 0 .../r/{regression => regression_02}/regression_9875_enumerate.txt | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename tests/functional/r/{regression => regression_02}/regression_9875_enumerate.py (100%) rename tests/functional/r/{regression => regression_02}/regression_9875_enumerate.txt (100%) diff --git a/tests/functional/r/regression/regression_9875_enumerate.py b/tests/functional/r/regression_02/regression_9875_enumerate.py similarity index 100% rename from tests/functional/r/regression/regression_9875_enumerate.py rename to tests/functional/r/regression_02/regression_9875_enumerate.py diff --git a/tests/functional/r/regression/regression_9875_enumerate.txt b/tests/functional/r/regression_02/regression_9875_enumerate.txt similarity index 100% rename from tests/functional/r/regression/regression_9875_enumerate.txt rename to tests/functional/r/regression_02/regression_9875_enumerate.txt