Skip to content

Commit

Permalink
Don't calculate slots when MRO parsing fails. (#1089)
Browse files Browse the repository at this point in the history
* Don't calculate slots when MRO parsing fails.

* Added changelog entry.

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Pierre Sassoulas <pierre.sassoulas@gmail.com>
  • Loading branch information
3 people committed Aug 1, 2021
1 parent f236e36 commit e178626
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
4 changes: 4 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ Release date: 2021-07-19

* Added ``If.is_sys_guard`` and ``If.is_typing_guard`` helper methods

* Fix handling of classes with duplicated bases with the same name

Closes PyCQA/astroid#1088

* Fix a bad inferenece type for yield values inside of a derived class.

Closes PyCQA/astroid#1090
Expand Down
16 changes: 13 additions & 3 deletions astroid/scoped_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import builtins
import io
import itertools
import typing
from typing import List, Optional

from astroid import bases
Expand Down Expand Up @@ -2906,9 +2907,11 @@ def slots(self):
:rtype: list(str) or None
"""

def grouped_slots():
def grouped_slots(
mro: List["ClassDef"],
) -> typing.Iterator[Optional[node_classes.NodeNG]]:
# Not interested in object, since it can't have slots.
for cls in self.mro()[:-1]:
for cls in mro[:-1]:
try:
cls_slots = cls._slots()
except NotImplementedError:
Expand All @@ -2923,7 +2926,14 @@ def grouped_slots():
"The concept of slots is undefined for old-style classes."
)

slots = list(grouped_slots())
try:
mro = self.mro()
except MroError as e:
raise NotImplementedError(
"Cannot get slots while parsing mro fails."
) from e

slots = list(grouped_slots(mro))
if not all(slot is not None for slot in slots):
return None

Expand Down
11 changes: 11 additions & 0 deletions tests/unittest_scoped_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2230,5 +2230,16 @@ class C(B[str]): pass
]


def test_slots_duplicate_bases_issue_1089():
astroid = builder.parse(
"""
class First(object, object): #@
pass
"""
)
with pytest.raises(NotImplementedError):
astroid["First"].slots()


if __name__ == "__main__":
unittest.main()

0 comments on commit e178626

Please sign in to comment.