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

Crash on enumerate(x, int(y)) #9078

Closed
Ranfir opened this issue Sep 28, 2023 · 1 comment · Fixed by #9704
Closed

Crash on enumerate(x, int(y)) #9078

Ranfir opened this issue Sep 28, 2023 · 1 comment · Fixed by #9704
Labels
Crash 💥 A bug that makes pylint crash Needs PR This issue is accepted, sufficiently specified and now needs an implementation
Milestone

Comments

@Ranfir
Copy link

Ranfir commented Sep 28, 2023

Bug description

When parsing the following file:

for _, _ in enumerate(x, int(y)):
    ...

pylint crashed with a AstroidError and with the following stacktrace:

Traceback (most recent call last):
  File "/tmp/pylint_crash/.venv/lib/python3.11/site-packages/pylint/lint/pylinter.py", line 811, in _lint_file
    check_astroid_module(module)
  File "/tmp/pylint_crash/.venv/lib/python3.11/site-packages/pylint/lint/pylinter.py", line 1085, in check_astroid_module
    retval = self._check_astroid_module(
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/tmp/pylint_crash/.venv/lib/python3.11/site-packages/pylint/lint/pylinter.py", line 1135, in _check_astroid_module
    walker.walk(node)
  File "/tmp/pylint_crash/.venv/lib/python3.11/site-packages/pylint/utils/ast_walker.py", line 94, in walk
    self.walk(child)
  File "/tmp/pylint_crash/.venv/lib/python3.11/site-packages/pylint/utils/ast_walker.py", line 91, in walk
    callback(astroid)
  File "/tmp/pylint_crash/.venv/lib/python3.11/site-packages/pylint/checkers/refactoring/refactoring_checker.py", line 700, in visit_for
    self._check_unnecessary_list_index_lookup(node)
  File "/tmp/pylint_crash/.venv/lib/python3.11/site-packages/pylint/checkers/refactoring/refactoring_checker.py", line 2229, in _check_unnecessary_list_index_lookup
    has_start_arg, confidence = self._enumerate_with_start(node)
                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/tmp/pylint_crash/.venv/lib/python3.11/site-packages/pylint/checkers/refactoring/refactoring_checker.py", line 2347, in _enumerate_with_start
    start_val, confidence = self._get_start_value(start_arg)
                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/tmp/pylint_crash/.venv/lib/python3.11/site-packages/pylint/checkers/refactoring/refactoring_checker.py", line 2368, in _get_start_value
    start_val = inferred.value if inferred else None
                ^^^^^^^^^^^^^^
  File "/tmp/pylint_crash/.venv/lib/python3.11/site-packages/astroid/bases.py", line 140, in __getattr__
    return getattr(self._proxied, name)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'ClassDef' object has no attribute 'value'

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

Traceback (most recent call last):
  File "/tmp/pylint_crash/.venv/lib/python3.11/site-packages/pylint/lint/pylinter.py", line 775, in _lint_files
    self._lint_file(fileitem, module, check_astroid_module)
  File "/tmp/pylint_crash/.venv/lib/python3.11/site-packages/pylint/lint/pylinter.py", line 813, in _lint_file
    raise astroid.AstroidError from e
astroid.exceptions.AstroidError

Command used

.venv/bin/pylint scratch2.py

Pylint output

************* Module scratch2
scratch2.py:1:0: C0114: Missing module docstring (missing-module-docstring)
Exception on node <For l.1 at 0x7f1d71faa450> in file '/tmp/pylint_crash/scratch2.py'
Traceback (most recent call last):
  File "/tmp/pylint_crash/.venv/lib/python3.11/site-packages/pylint/utils/ast_walker.py", line 91, in walk
    callback(astroid)
  File "/tmp/pylint_crash/.venv/lib/python3.11/site-packages/pylint/checkers/refactoring/refactoring_checker.py", line 700, in visit_for
    self._check_unnecessary_list_index_lookup(node)
  File "/tmp/pylint_crash/.venv/lib/python3.11/site-packages/pylint/checkers/refactoring/refactoring_checker.py", line 2229, in _check_unnecessary_list_index_lookup
    has_start_arg, confidence = self._enumerate_with_start(node)
                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/tmp/pylint_crash/.venv/lib/python3.11/site-packages/pylint/checkers/refactoring/refactoring_checker.py", line 2347, in _enumerate_with_start
    start_val, confidence = self._get_start_value(start_arg)
                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/tmp/pylint_crash/.venv/lib/python3.11/site-packages/pylint/checkers/refactoring/refactoring_checker.py", line 2368, in _get_start_value
    start_val = inferred.value if inferred else None
                ^^^^^^^^^^^^^^
  File "/tmp/pylint_crash/.venv/lib/python3.11/site-packages/astroid/bases.py", line 140, in __getattr__
    return getattr(self._proxied, name)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'ClassDef' object has no attribute 'value'
scratch2.py:1:0: F0002: scratch2.py: Fatal error while checking 'scratch2.py'. Please open an issue in our bug tracker so we address this.

Expected behavior

no crash

Pylint version

pylint 2.17.6
astroid 2.15.8
Python 3.11.5 (main, Sep 14 2023, 11:48:30) [GCC 9.4.0]

OS / Environment

Ubuntu 20.04.6 LTS

Additional dependencies

astroid==2.15.8
dill==0.3.7
isort==5.12.0
lazy-object-proxy==1.9.0
mccabe==0.7.0
platformdirs==3.10.0
pylint==2.17.6
tomlkit==0.12.1
wrapt==1.15.0
@Ranfir Ranfir added the Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling label Sep 28, 2023
@Pierre-Sassoulas Pierre-Sassoulas added Crash 💥 A bug that makes pylint crash Needs PR This issue is accepted, sufficiently specified and now needs an implementation and removed Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling labels Sep 28, 2023
@Pierre-Sassoulas Pierre-Sassoulas changed the title crash on enumerate(x, int(y)) Crash on enumerate(x, int(y)) Sep 28, 2023
@nickdrozd
Copy link
Contributor

This is a nice one. y is undefined, so int(y) is inferred to be an int. But that seems to be the class int rather than an instance (or some kind of mix-up like that). And the class has no value field, hence the error.

Issues like this and #9074 would probably be caught by proper Astroid typing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Crash 💥 A bug that makes pylint crash Needs PR This issue is accepted, sufficiently specified and now needs an implementation
Projects
None yet
3 participants