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 when providing type annotation for class __slots__ without assigning a value #7280

Closed
jphells opened this issue Aug 9, 2022 · 0 comments · Fixed by #7285
Closed

Crash when providing type annotation for class __slots__ without assigning a value #7280

jphells opened this issue Aug 9, 2022 · 0 comments · Fixed by #7285
Labels
Crash 💥 A bug that makes pylint crash Needs PR This issue is accepted, sufficiently specified and now needs an implementation
Milestone

Comments

@jphells
Copy link

jphells commented Aug 9, 2022

Bug description

Pylint crashes fatally when linting a file with the following example code:

example.py

# pylint: disable=missing-docstring

class Foo:
    __slots__: str

pylint crashed with a AstroidError and with the following stacktrace:

Traceback (most recent call last):
  File "c:\Users\User\example\venv\lib\site-packages\astroid\nodes\scoped_nodes\scoped_nodes.py", line 2663, in igetattr
    attributes = self.getattr(name, context, class_context=class_context)
  File "c:\Users\User\example\venv\lib\site-packages\astroid\nodes\scoped_nodes\scoped_nodes.py", line 2603, in getattr
    raise AttributeInferenceError(
astroid.exceptions.AttributeInferenceError: '__slots__' not found on <ClassDef.Foo l.2 at 0x13232ba3220>.

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

Traceback (most recent call last):
  File "c:\Users\User\example\venv\lib\site-packages\pylint\lint\pylinter.py", line 727, in _check_file
    check_astroid_module(ast_node)
  File "c:\Users\User\example\venv\lib\site-packages\pylint\lint\pylinter.py", line 926, in check_astroid_module
    retval = self._check_astroid_module(
  File "c:\Users\User\example\venv\lib\site-packages\pylint\lint\pylinter.py", line 976, in _check_astroid_module
    walker.walk(node)
  File "c:\Users\User\example\venv\lib\site-packages\pylint\utils\ast_walker.py", line 93, in walk
    self.walk(child)
  File "c:\Users\User\example\venv\lib\site-packages\pylint\utils\ast_walker.py", line 90, in walk
    callback(astroid)
  File "c:\Users\User\example\venv\lib\site-packages\pylint\checkers\classes\class_checker.py", line 838, in visit_classdef
    self._check_slots(node)
  File "c:\Users\User\example\venv\lib\site-packages\pylint\checkers\classes\class_checker.py", line 1369, in _check_slots
    for slots in node.igetattr("__slots__"):
  File "c:\Users\User\example\venv\lib\site-packages\astroid\nodes\scoped_nodes\scoped_nodes.py", line 2714, in igetattr
    raise InferenceError(
astroid.exceptions.InferenceError: '__slots__' not found on <ClassDef.Foo l.2 at 0x13232ba3220>.

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

Traceback (most recent call last):
  File "c:\Users\User\example\venv\lib\site-packages\pylint\lint\pylinter.py", line 684, in _check_files
    self._check_file(get_ast, check_astroid_module, file)
  File "c:\Users\User\example\venv\lib\site-packages\pylint\lint\pylinter.py", line 729, in _check_file
    raise astroid.AstroidError from e
astroid.exceptions.AstroidError

A hotfix to my code is simply to assign a value to __slots__ in the class definition.

Expecting pylint not to crash and an empty output or warning/error telling that I am using a defined but unassigned __slots__.

Configuration

No response

Command used

pylint example.py

Pylint output

In the editor:

c:\Users\User\example\example.py: Fatal error while checking 'c:\Users\User\example\example.py'. Please open an issue in our bug tracker so we address this. There is a pre-filled template that you can use in 'C:\Users\User\AppData\Local\pylint\pylint\Cache\pylint-crash-2022-08-09-13.txt'.

pylint-crash-2022-08-09-13.txt (and when running pylint example.py):

Traceback (most recent call last):
  File "c:\Users\User\example\venv\lib\site-packages\astroid\nodes\scoped_nodes\scoped_nodes.py", line 2663, in igetattr
    attributes = self.getattr(name, context, class_context=class_context)
  File "c:\Users\User\example\venv\lib\site-packages\astroid\nodes\scoped_nodes\scoped_nodes.py", line 2603, in getattr
    raise AttributeInferenceError(
astroid.exceptions.AttributeInferenceError: '__slots__' not found on <ClassDef.Foo l.2 at 0x13232ba3220>.

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

Traceback (most recent call last):
  File "c:\Users\User\example\venv\lib\site-packages\pylint\lint\pylinter.py", line 727, in _check_file
    check_astroid_module(ast_node)
  File "c:\Users\User\example\venv\lib\site-packages\pylint\lint\pylinter.py", line 926, in check_astroid_module
    retval = self._check_astroid_module(
  File "c:\Users\User\example\venv\lib\site-packages\pylint\lint\pylinter.py", line 976, in _check_astroid_module
    walker.walk(node)
  File "c:\Users\User\example\venv\lib\site-packages\pylint\utils\ast_walker.py", line 93, in walk
    self.walk(child)
  File "c:\Users\User\example\venv\lib\site-packages\pylint\utils\ast_walker.py", line 90, in walk
    callback(astroid)
  File "c:\Users\User\example\venv\lib\site-packages\pylint\checkers\classes\class_checker.py", line 838, in visit_classdef
    self._check_slots(node)
  File "c:\Users\User\example\venv\lib\site-packages\pylint\checkers\classes\class_checker.py", line 1369, in _check_slots
    for slots in node.igetattr("__slots__"):
  File "c:\Users\User\example\venv\lib\site-packages\astroid\nodes\scoped_nodes\scoped_nodes.py", line 2714, in igetattr
    raise InferenceError(
astroid.exceptions.InferenceError: '__slots__' not found on <ClassDef.Foo l.2 at 0x13232ba3220>.

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

Traceback (most recent call last):
  File "c:\Users\User\example\venv\lib\site-packages\pylint\lint\pylinter.py", line 684, in _check_files
    self._check_file(get_ast, check_astroid_module, file)
  File "c:\Users\User\example\venv\lib\site-packages\pylint\lint\pylinter.py", line 729, in _check_file
    raise astroid.AstroidError from e
astroid.exceptions.AstroidError

Expected behavior

Expected: pylint does not crash and either empty output, or some kind of warning or error that I'm defining but not assigning to a __slots__ attribute.

Pylint version

pylint 2.14.5
astroid 2.11.7
Python 3.10.4 (tags/v3.10.4:9d38120, Mar 23 2022, 23:13:41) [MSC v.1929 64 bit (AMD64)]

OS / Environment

No response

Additional dependencies

astroid==2.11.7
colorama==0.4.5
dill==0.3.5.1
isort==5.10.1
lazy-object-proxy==1.7.1
mccabe==0.7.0
platformdirs==2.5.2
pylint==2.14.5
tomli==2.0.1
tomlkit==0.11.2
wrapt==1.14.1

@jphells jphells added the Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling label Aug 9, 2022
@jacobtylerwalls jacobtylerwalls 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 Aug 9, 2022
@jacobtylerwalls jacobtylerwalls added this to the 2.15.0 milestone Aug 9, 2022
@jacobtylerwalls jacobtylerwalls changed the title Pylint 2.14.5 on python 3.10.4 crashes fatally after defining a class __slots__ without assigning a value to it Crash when providing type annotation for class __slots__ without assigning a value Aug 9, 2022
mbyrnepr2 added a commit to mbyrnepr2/pylint that referenced this issue Aug 10, 2022
jacobtylerwalls pushed a commit that referenced this issue Aug 11, 2022
#7285)

* Fix crash when a type-annotated `__slots__` with no value is declared.

Closes #7280
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
Development

Successfully merging a pull request may close this issue.

2 participants