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

"AttributeError: 'ClassDef' object has no attribute 'value'" in _check_redefined_slots #6100

Closed
kainjow opened this issue Apr 1, 2022 · 6 comments · Fixed by #6112
Closed
Labels
Bug 🪲 Crash 💥 A bug that makes pylint crash Regression
Milestone

Comments

@kainjow
Copy link

kainjow commented Apr 1, 2022

Bug description

I'm seeing this exception when a __slot__ is set to a list of enum values, but was able to reproduce minimally with just using a type as the list:

class MyClass:
    __slots__ = [str]

Configuration

No response

Command used

pylint test.py

Pylint output

Exception on node <ClassDef.MyClass l.1 at 0x7fe9152603d0> in file 'test.py'
Traceback (most recent call last):
  File "/Users/me/.virtualenvs/myvirtualenv/lib/python3.8/site-packages/pylint/utils/ast_walker.py", line 73, in walk
    callback(astroid)
  File "/Users/me/.virtualenvs/myvirtualenv/lib/python3.8/site-packages/pylint/checkers/classes/class_checker.py", line 804, in visit_classdef
    self._check_slots(node)
  File "/Users/me/.virtualenvs/myvirtualenv/lib/python3.8/site-packages/pylint/checkers/classes/class_checker.py", line 1369, in _check_slots
    self._check_redefined_slots(node, slots, values)
  File "/Users/me/.virtualenvs/myvirtualenv/lib/python3.8/site-packages/pylint/checkers/classes/class_checker.py", line 1385, in _check_redefined_slots
    slots_names.append(inferred_slot.value)
AttributeError: 'ClassDef' object has no attribute 'value'
************* Module box.sync.core.test
F0001(fatal):  1,0: : Fatal error while checking 'test.py'. Please open an issue in our bug tracker so we address this. There is a pre-filled template that you can use in '/Users/me/Library/Caches/pylint/pylint-crash-2022-04-01-11.txt'.

Expected behavior

pylint not crash.

Pylint version

pylint 2.13.4
astroid 2.11.2

OS / Environment

Python 3.8.8
macOS 11.6.4

Additional dependencies

No response

@kainjow kainjow added the Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling label Apr 1, 2022
@kainjow kainjow changed the title "AttributeError: 'ClassDef' object has no attribute 'value'" with __slots__ "AttributeError: 'ClassDef' object has no attribute 'value'" in _check_redefined_slots Apr 1, 2022
@jacobtylerwalls
Copy link
Member

Thanks for the report. Regression in 56129b7. /CC @mbyrnepr2

@jacobtylerwalls jacobtylerwalls added Bug 🪲 Regression Crash 💥 A bug that makes pylint crash and removed Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling labels Apr 1, 2022
@jacobtylerwalls jacobtylerwalls added this to the 2.13.5 milestone Apr 1, 2022
@mbyrnepr2
Copy link
Member

Thanks for the headsup. By the way, this example is invalid Python:

$ python example.py
Traceback (most recent call last):
  File "example.py", line 1, in <module>
    class MyClass:
TypeError: __slots__ items must be strings, not 'type'

But we probably want to avoid the crash either way.

@kainjow
Copy link
Author

kainjow commented Apr 1, 2022

I’m on mobile now but I can update the example using enums which I think mapped to strings. Will check later.

@kainjow
Copy link
Author

kainjow commented Apr 1, 2022

Enum example:

from enum import Enum

class MyEnum(Enum):
    VALUE = 'value'

class MyClass:
    __slots__ = [MyEnum.VALUE]

@mbyrnepr2
Copy link
Member

Enum example:

from enum import Enum

class MyEnum(Enum):
    VALUE = 'value'

class MyClass:
    __slots__ = [MyEnum.VALUE]

Thanks for this example. This one is also invalid Python but I agree that we could tidy things up to prevent the crash report which happens when running Pylint on it.

Both of the examples provided emit the invalid-slots-object error so some of the heavy-lifting is already there to work on.
I'm happy to put together a MR for this.

python example2.py

Traceback (most recent call last):
  File "example2.py", line 6, in <module>
    class MyClass:
TypeError: __slots__ items must be strings, not 'MyEnum'

@kainjow
Copy link
Author

kainjow commented Apr 1, 2022

Thanks. So I realized that enum example doesn't yet match the code base because the issues come from code that uses a custom Enum (added pre-Python 3). So in those cases, the value is a string, not an enum value.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug 🪲 Crash 💥 A bug that makes pylint crash Regression
Projects
None yet
3 participants