Skip to content

Commit

Permalink
Prevent returning an empty list for ClassDef.slots() when the mro…
Browse files Browse the repository at this point in the history
… list contains one class & it is not ``object``.

Refs pylint-dev/pylint#5099
  • Loading branch information
mbyrnepr2 committed Nov 2, 2022
1 parent 5c1ed28 commit 966ca80
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
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"]:
try:
cls_slots = cls._slots()
except NotImplementedError:
Expand Down
21 changes: 21 additions & 0 deletions tests/unittest_brain.py
Expand Up @@ -1820,6 +1820,27 @@ 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

0 comments on commit 966ca80

Please sign in to comment.