From 7f1d5134d89aab9b3e49030676637debfd731dbb Mon Sep 17 00:00:00 2001 From: Tim Martin Date: Wed, 7 Oct 2020 18:44:48 +0100 Subject: [PATCH 1/2] Fix incorrect MRO being calculated for scoped multiple inheritance If a class inherits from two bases and the classes are expressed as a non-trivial expression such as qualified with a module name, classes after the first could not be inferred due to reusing the context object in _inferred_bases --- ChangeLog | 4 ++++ astroid/scoped_nodes.py | 2 +- tests/unittest_scoped_nodes.py | 24 ++++++++++++++++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index f42649356..86c6deee8 100644 --- a/ChangeLog +++ b/ChangeLog @@ -53,6 +53,10 @@ Release Date: TBA * Fixed exception-chaining error messages. +* Fix failure to infer base class type with multiple inheritance and qualified names + + Fixes #843 + What's New in astroid 2.4.3? ============================ diff --git a/astroid/scoped_nodes.py b/astroid/scoped_nodes.py index 5c9419668..a44e6282e 100644 --- a/astroid/scoped_nodes.py +++ b/astroid/scoped_nodes.py @@ -2858,7 +2858,7 @@ def _inferred_bases(self, context=None): for stmt in self.bases: try: - baseobj = next(stmt.infer(context=context)) + baseobj = next(stmt.infer(context=context.clone())) except exceptions.InferenceError: continue if isinstance(baseobj, bases.Instance): diff --git a/tests/unittest_scoped_nodes.py b/tests/unittest_scoped_nodes.py index c4597fa68..2606fd47a 100644 --- a/tests/unittest_scoped_nodes.py +++ b/tests/unittest_scoped_nodes.py @@ -1417,6 +1417,30 @@ def __init__(self): ], ) + def test_mro_with_attribute_classes(self): + cls = builder.extract_node( + """ + class A: + pass + class B: + pass + scope = object() + scope.A = A + scope.B = B + class C(scope.A, scope.B): + pass + """ + ) + self.assertEqualMro( + cls, + [ + "C", + "A", + "B", + "object" + ] + ) + def test_generator_from_infer_call_result_parent(self): func = builder.extract_node( """ From c89dc0102bfe7a6e72b21e213742bdb982b438cc Mon Sep 17 00:00:00 2001 From: Tim Martin Date: Fri, 16 Oct 2020 10:36:30 +0100 Subject: [PATCH 2/2] Fix formatting regression --- tests/unittest_scoped_nodes.py | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/tests/unittest_scoped_nodes.py b/tests/unittest_scoped_nodes.py index 2606fd47a..aef1a671f 100644 --- a/tests/unittest_scoped_nodes.py +++ b/tests/unittest_scoped_nodes.py @@ -1431,15 +1431,7 @@ class C(scope.A, scope.B): pass """ ) - self.assertEqualMro( - cls, - [ - "C", - "A", - "B", - "object" - ] - ) + self.assertEqualMro(cls, ["C", "A", "B", "object"]) def test_generator_from_infer_call_result_parent(self): func = builder.extract_node(