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 a crash when a subclass extends __slots__ #9817

Merged
merged 1 commit into from
Jul 20, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions doc/whatsnew/fragments/9814.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix a crash when a subclass extends ``__slots__``.

Closes #9814
20 changes: 16 additions & 4 deletions pylint/checkers/classes/class_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -1537,7 +1537,11 @@ def _has_valid_slots(self, node: nodes.ClassDef) -> bool:
if "__slots__" not in node.locals:
return False

for slots in node.ilookup("__slots__"):
try:
inferred_slots = tuple(node.ilookup("__slots__"))
except astroid.InferenceError:
return False
for slots in inferred_slots:
# check if __slots__ is a valid type
if isinstance(slots, util.UninferableBase):
return False
Expand All @@ -1555,7 +1559,11 @@ def _check_slots(self, node: nodes.ClassDef) -> None:
if "__slots__" not in node.locals:
return

for slots in node.ilookup("__slots__"):
try:
inferred_slots = tuple(node.ilookup("__slots__"))
except astroid.InferenceError:
return
for slots in inferred_slots:
# check if __slots__ is a valid type
if isinstance(slots, util.UninferableBase):
continue
Expand Down Expand Up @@ -1586,8 +1594,12 @@ def _check_slots(self, node: nodes.ClassDef) -> None:

def _get_classdef_slots_names(self, node: nodes.ClassDef) -> list[str]:

slots_names = []
for slots in node.ilookup("__slots__"):
slots_names: list[str] = []
try:
inferred_slots = tuple(node.ilookup("__slots__"))
except astroid.InferenceError: # pragma: no cover
Copy link
Member Author

@jacobtylerwalls jacobtylerwalls Jul 20, 2024

Choose a reason for hiding this comment

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

This is no-covered because the calls to this function are (currently) guarded by _has_valid_slots(), which does the same test, but just adding safety because we shouldn't be failing to catch InferenceError from calls to the inference system.

return slots_names
for slots in inferred_slots:
if isinstance(slots, nodes.Dict):
values = [item[0] for item in slots.items]
else:
Expand Down
14 changes: 14 additions & 0 deletions tests/functional/s/slots_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,3 +187,17 @@ class ClassWithEmptySlotsAndAnnotation:
__slots__ = ()

a: int


# https://github.com/pylint-dev/pylint/issues/9814
class SlotsManipulationTest:
__slots__ = ["a", "b", "c"]


class TestChild(SlotsManipulationTest):
__slots__ += ["d", "e", "f"] # pylint: disable=undefined-variable


t = TestChild()

print(t.__slots__)
Loading