summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJacob Walls <jacobtylerwalls@gmail.com>2022-07-05 10:49:47 -0400
committerGitHub <noreply@github.com>2022-07-05 16:49:47 +0200
commit14b3363ffda4014b0f390ef4b2008843b2db8287 (patch)
tree32fb6df34775b13d0dcdcb966dc40161b18909f4
parentc1ffa59508841c87e4720c72b2d40fc0ae795622 (diff)
downloadpylint-git-14b3363ffda4014b0f390ef4b2008843b2db8287.tar.gz
Fix crash when using `enumerate` in a ternary expression (#7132)
-rw-r--r--doc/whatsnew/2/2.14/full.rst5
-rw-r--r--pylint/checkers/variables.py7
-rw-r--r--tests/functional/u/undefined/undefined_loop_variable.py7
-rw-r--r--tests/functional/u/undefined/undefined_loop_variable.txt2
4 files changed, 18 insertions, 3 deletions
diff --git a/doc/whatsnew/2/2.14/full.rst b/doc/whatsnew/2/2.14/full.rst
index d4b94eec1..3d9d25eac 100644
--- a/doc/whatsnew/2/2.14/full.rst
+++ b/doc/whatsnew/2/2.14/full.rst
@@ -6,6 +6,11 @@ What's New in Pylint 2.14.5?
Release date: TBA
+* Fixed a crash in the ``undefined-loop-variable`` check when ``enumerate()`` is used
+ in a ternary expression.
+
+ Closes #7131
+
* Fixed handling of ``--`` as separator between positional arguments and flags.
Closes #7003
diff --git a/pylint/checkers/variables.py b/pylint/checkers/variables.py
index b945bfbd8..4dc469495 100644
--- a/pylint/checkers/variables.py
+++ b/pylint/checkers/variables.py
@@ -2237,9 +2237,12 @@ class VariablesChecker(BaseChecker):
if (
isinstance(inferred, astroid.Instance)
and inferred.qname() == "builtins.enumerate"
- and assign.iter.args
):
- inferred = next(assign.iter.args[0].infer())
+ likely_call = assign.iter
+ if isinstance(assign.iter, nodes.IfExp):
+ likely_call = assign.iter.body
+ if isinstance(likely_call, nodes.Call):
+ inferred = next(likely_call.args[0].infer())
except astroid.InferenceError:
self.add_message("undefined-loop-variable", args=node.name, node=node)
else:
diff --git a/tests/functional/u/undefined/undefined_loop_variable.py b/tests/functional/u/undefined/undefined_loop_variable.py
index a4249a39f..c18d22755 100644
--- a/tests/functional/u/undefined/undefined_loop_variable.py
+++ b/tests/functional/u/undefined/undefined_loop_variable.py
@@ -148,6 +148,13 @@ def use_enumerate():
print(i, num)
+def use_enumerate_in_ternary_expression():
+ """https://github.com/PyCQA/pylint/issues/7131"""
+ for i, num in enumerate(range(3)) if __revision__ else enumerate(range(4)):
+ pass
+ print(i, num)
+
+
def find_even_number(container):
"""https://github.com/PyCQA/pylint/pull/6923#discussion_r895134495"""
for something in container:
diff --git a/tests/functional/u/undefined/undefined_loop_variable.txt b/tests/functional/u/undefined/undefined_loop_variable.txt
index ef9031c57..9cee5085d 100644
--- a/tests/functional/u/undefined/undefined_loop_variable.txt
+++ b/tests/functional/u/undefined/undefined_loop_variable.txt
@@ -1,4 +1,4 @@
undefined-loop-variable:6:11:6:14:do_stuff:Using possibly undefined loop variable 'var':UNDEFINED
undefined-loop-variable:25:7:25:11::Using possibly undefined loop variable 'var1':UNDEFINED
undefined-loop-variable:75:11:75:14:do_stuff_with_redefined_range:Using possibly undefined loop variable 'var':UNDEFINED
-undefined-loop-variable:156:11:156:20:find_even_number:Using possibly undefined loop variable 'something':UNDEFINED
+undefined-loop-variable:163:11:163:20:find_even_number:Using possibly undefined loop variable 'something':UNDEFINED