Skip to content

AstroidError crash on extremely specific enumerate to dictionary comprehension. #10510

@grantthchen

Description

@grantthchen

Bug description

Very new to submitting bugs, and this is really easily circumvented, but the scary error message prompted me to post this in case it's something of use to Pylint developers.

When parsing the following a.py:

xs = [1, 2, 3]
testdict = {j: i for i, j in enumerate(xs, -len(xs))}

Pylint crashes with AstroidError.

The following code works without issues:

xs = [1, 2, 3]
testdict = {j: i for i, j in enumerate([1, 2, 3], -len(xs))}
testdict = {j: i for i, j in enumerate(xs, len(xs))}
s = -len(xs)
testdict = {j: i for i, j in enumerate(xs, s)}
testdict = {j: i for i, j in list(enumerate(xs, -len(xs)))}
testdict = {j: i for i, j in dict(enumerate(xs, -len(xs))).items()}

Configuration

Command used

pylint a.py

Pylint output

pylint crashed with a ``AstroidError`` and with the following stacktrace:
Traceback (most recent call last):
  File "/home/redacted/.vscode-server/extensions/ms-python.pylint-2024.2.0/bundled/libs/pylint/lint/pylinter.py", line 789, in _lint_file
    check_astroid_module(module)
  File "/home/redacted/.vscode-server/extensions/ms-python.pylint-2024.2.0/bundled/libs/pylint/lint/pylinter.py", line 1018, in check_astroid_module
    retval = self._check_astroid_module(
  File "/home/redacted/.vscode-server/extensions/ms-python.pylint-2024.2.0/bundled/libs/pylint/lint/pylinter.py", line 1070, in _check_astroid_module
    walker.walk(node)
  File "/home/redacted/.vscode-server/extensions/ms-python.pylint-2024.2.0/bundled/libs/pylint/utils/ast_walker.py", line 94, in walk
    self.walk(child)
  File "/home/redacted/.vscode-server/extensions/ms-python.pylint-2024.2.0/bundled/libs/pylint/utils/ast_walker.py", line 94, in walk
    self.walk(child)
  File "/home/redacted/.vscode-server/extensions/ms-python.pylint-2024.2.0/bundled/libs/pylint/utils/ast_walker.py", line 94, in walk
    self.walk(child)
  File "/home/redacted/.vscode-server/extensions/ms-python.pylint-2024.2.0/bundled/libs/pylint/utils/ast_walker.py", line 91, in walk
    callback(astroid)
  File "/home/redacted/.vscode-server/extensions/ms-python.pylint-2024.2.0/bundled/libs/pylint/checkers/refactoring/refactoring_checker.py", line 1814, in visit_comprehension
    self._check_unnecessary_list_index_lookup(node)
  File "/home/redacted/.vscode-server/extensions/ms-python.pylint-2024.2.0/bundled/libs/pylint/checkers/refactoring/refactoring_checker.py", line 2312, in _check_unnecessary_list_index_lookup
    has_start_arg, confidence = self._enumerate_with_start(node)
  File "/home/redacted/.vscode-server/extensions/ms-python.pylint-2024.2.0/bundled/libs/pylint/checkers/refactoring/refactoring_checker.py", line 2437, in _enumerate_with_start
    start_val, confidence = self._get_start_value(start_arg)
  File "/home/redacted/.vscode-server/extensions/ms-python.pylint-2024.2.0/bundled/libs/pylint/checkers/refactoring/refactoring_checker.py", line 2463, in _get_start_value
    return node.operand.value, HIGH
AttributeError: 'Call' object has no attribute 'value'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/redacted/.vscode-server/extensions/ms-python.pylint-2024.2.0/bundled/libs/pylint/lint/pylinter.py", line 753, in _lint_files
    self._lint_file(fileitem, module, check_astroid_module)
  File "/home/redacted/.vscode-server/extensions/ms-python.pylint-2024.2.0/bundled/libs/pylint/lint/pylinter.py", line 791, in _lint_file
    raise astroid.AstroidError from e
astroid.exceptions.AstroidError

Expected behavior

No crash.

Pylint version

pylint 3.2.7
astroid 3.2.4
Python 3.8.17 (default, Jul  5 2023, 21:04:15) 
[GCC 11.2.0]

OS / Environment

linux (Linux)

Additional dependencies

Metadata

Metadata

Assignees

No one assigned

    Labels

    Crash 💥A bug that makes pylint crashNeeds PRThis issue is accepted, sufficiently specified and now needs an implementation

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions