summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorhippo91 <guillaume.peillex@gmail.com>2021-02-11 18:42:24 +0100
committerPierre Sassoulas <pierre.sassoulas@gmail.com>2021-02-12 08:26:52 +0100
commitdc560d470b3351cfc886f62688ee86dcbbb1a3cd (patch)
treec39b5ca86fda6699afcb9c2df31a6f2464be0e51
parent0d8ed3f88deef0b2e85b5310b82cc3998220dc3c (diff)
downloadastroid-git-dc560d470b3351cfc886f62688ee86dcbbb1a3cd.tar.gz
The node.bases has not to be tweaked otherwise leads to false positive unused-import due to the fact that six.with_metaclass is not consumed. Adds a unittest to check that bases attribute holds a call node and that ancestors attributes returns the correct class hierarchy.
-rw-r--r--astroid/brain/brain_six.py1
-rw-r--r--tests/unittest_inference.py28
2 files changed, 28 insertions, 1 deletions
diff --git a/astroid/brain/brain_six.py b/astroid/brain/brain_six.py
index a998213f..ed46d872 100644
--- a/astroid/brain/brain_six.py
+++ b/astroid/brain/brain_six.py
@@ -221,7 +221,6 @@ def transform_six_with_metaclass(node):
"""
call = node.bases[0]
node._metaclass = call.args[0]
- node.bases = call.args[1:]
register_module_extender(MANAGER, "six", six_moves_transform)
diff --git a/tests/unittest_inference.py b/tests/unittest_inference.py
index c107eadb..10dd49d7 100644
--- a/tests/unittest_inference.py
+++ b/tests/unittest_inference.py
@@ -3793,6 +3793,34 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase):
self.assertIsInstance(inferred, nodes.ClassDef)
self.assertEqual(inferred.name, "B")
+ def test_With_metaclass_subclasses_inheritance(self):
+ ast_node = extract_node(
+ """
+ class A(type):
+ def test(cls):
+ return cls
+
+ class C:
+ pass
+
+ import six
+ class B(six.with_metaclass(A, C)):
+ pass
+
+ B #@
+ """
+ )
+ inferred = next(ast_node.infer())
+ self.assertIsInstance(inferred, nodes.ClassDef)
+ self.assertEqual(inferred.name, "B")
+ bases = inferred.bases
+ self.assertIsInstance(bases[0], nodes.Call)
+ ancestors = tuple(inferred.ancestors())
+ self.assertIsInstance(ancestors[0], nodes.ClassDef)
+ self.assertEqual(ancestors[0].name, "C")
+ self.assertIsInstance(ancestors[1], nodes.ClassDef)
+ self.assertEqual(ancestors[1].name, "object")
+
def test_With_metaclass_with_partial_imported_name(self):
ast_node = extract_node(
"""