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

Prevent returning an empty list for ClassDef.slots() #1861

Merged
merged 3 commits into from Nov 3, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions ChangeLog
Expand Up @@ -16,6 +16,10 @@ Release date: TBA

Refs PyCQA/pylint#2567

* Prevent returning an empty list for ``ClassDef.slots()`` when the mro list contains one class & it is not ``object``.

Refs PyCQA/pylint#5099

* Add ``_value2member_map_`` member to the ``enum`` brain.

Refs PyCQA/pylint#3941
Expand Down
2 changes: 1 addition & 1 deletion astroid/nodes/scoped_nodes/scoped_nodes.py
Expand Up @@ -2968,7 +2968,7 @@ def grouped_slots(
mro: list[ClassDef],
) -> Iterator[node_classes.NodeNG | None]:
# Not interested in object, since it can't have slots.
for cls in mro[:-1]:
for cls in [cls for cls in mro if cls.qname() != "builtins.object"]:
mbyrnepr2 marked this conversation as resolved.
Show resolved Hide resolved
try:
cls_slots = cls._slots()
except NotImplementedError:
Expand Down
20 changes: 20 additions & 0 deletions tests/unittest_brain.py
Expand Up @@ -1820,6 +1820,26 @@ def __init__(self, value):
assert isinstance(slots[0], nodes.Const)
assert slots[0].value == "value"

def test_collections_generic_alias_slots(self):
"""Test slots for a class which is a subclass of a generic alias type."""
node = builder.extract_node(
"""
import collections
import typing
Type = typing.TypeVar('Type')
class A(collections.abc.AsyncIterator[Type]):
__slots__ = ('_value',)
def __init__(self, value: collections.abc.AsyncIterator[Type]):
self._value = value
"""
)
inferred = next(node.infer())
assert isinstance(inferred, nodes.ClassDef)
slots = inferred.slots()
assert len(slots) == 1
assert isinstance(slots[0], nodes.Const)
assert slots[0].value == "_value"

def test_has_dunder_args(self) -> None:
ast_node = builder.extract_node(
"""
Expand Down