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 crash when using enumerate in a ternary expression #7132

Merged
merged 1 commit into from Jul 5, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions doc/whatsnew/2/2.14/full.rst
Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions pylint/checkers/variables.py
Expand Up @@ -2237,9 +2237,12 @@ def _loopvar_name(self, node: astroid.Name) -> None:
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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coincidentally, this could be done with a ternary πŸ˜„

But we never use those!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like Daniel wouldn't be a fan of this checker : #4871 πŸ˜‰

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:
Expand Down
7 changes: 7 additions & 0 deletions tests/functional/u/undefined/undefined_loop_variable.py
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion 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