Skip to content

Commit

Permalink
Prevent returning an empty list for ClassDef.slots() (#1861)
Browse files Browse the repository at this point in the history
Prevent returning an empty list for `ClassDef.slots()` when the mro list contains one class & it is not `object`. Refs pylint-dev/pylint#5099

Co-authored-by: Marc Mueller <30130371+cdce8p@users.noreply.github.com>
  • Loading branch information
mbyrnepr2 and cdce8p committed Nov 3, 2022
1 parent 5c1ed28 commit 2d34699
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
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
6 changes: 4 additions & 2 deletions astroid/nodes/scoped_nodes/scoped_nodes.py
Expand Up @@ -2967,8 +2967,10 @@ def slots(self):
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 mro:
# Not interested in object, since it can't have slots.
if cls.qname() == "builtins.object":
continue
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

0 comments on commit 2d34699

Please sign in to comment.