summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordoranid <ddandd@gmail.com>2021-08-01 22:04:46 +0300
committerGitHub <noreply@github.com>2021-08-01 21:04:46 +0200
commite17862654fc0585e63f5ab6785eab23787e91e9b (patch)
tree30f37de849659b816358975fb577394fc1affc34
parentf236e36729265602143ace2d969b44e53e4d57c1 (diff)
downloadastroid-git-e17862654fc0585e63f5ab6785eab23787e91e9b.tar.gz
Don't calculate slots when MRO parsing fails. (#1089)
* 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>
-rw-r--r--ChangeLog4
-rw-r--r--astroid/scoped_nodes.py16
-rw-r--r--tests/unittest_scoped_nodes.py11
3 files changed, 28 insertions, 3 deletions
diff --git a/ChangeLog b/ChangeLog
index e65a4c41..ca355f1b 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -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
diff --git a/astroid/scoped_nodes.py b/astroid/scoped_nodes.py
index c7c28779..910d3035 100644
--- a/astroid/scoped_nodes.py
+++ b/astroid/scoped_nodes.py
@@ -42,6 +42,7 @@ Lambda, GeneratorExp, DictComp and SetComp to some extent).
import builtins
import io
import itertools
+import typing
from typing import List, Optional
from astroid import bases
@@ -2906,9 +2907,11 @@ class ClassDef(mixins.FilterStmtsMixin, LocalsDictNodeNG, node_classes.Statement
: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:
@@ -2923,7 +2926,14 @@ class ClassDef(mixins.FilterStmtsMixin, LocalsDictNodeNG, node_classes.Statement
"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
diff --git a/tests/unittest_scoped_nodes.py b/tests/unittest_scoped_nodes.py
index 8fd890d1..56e1ed29 100644
--- a/tests/unittest_scoped_nodes.py
+++ b/tests/unittest_scoped_nodes.py
@@ -2230,5 +2230,16 @@ def test_ancestor_with_generic():
]
+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()