Skip to content

Commit dc560d4

Browse files
hippo91Pierre-Sassoulas
authored andcommitted
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.
1 parent 0d8ed3f commit dc560d4

2 files changed

Lines changed: 28 additions & 1 deletion

File tree

astroid/brain/brain_six.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,6 @@ def transform_six_with_metaclass(node):
221221
"""
222222
call = node.bases[0]
223223
node._metaclass = call.args[0]
224-
node.bases = call.args[1:]
225224

226225

227226
register_module_extender(MANAGER, "six", six_moves_transform)

tests/unittest_inference.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3793,6 +3793,34 @@ class B(six.with_metaclass(A)):
37933793
self.assertIsInstance(inferred, nodes.ClassDef)
37943794
self.assertEqual(inferred.name, "B")
37953795

3796+
def test_With_metaclass_subclasses_inheritance(self):
3797+
ast_node = extract_node(
3798+
"""
3799+
class A(type):
3800+
def test(cls):
3801+
return cls
3802+
3803+
class C:
3804+
pass
3805+
3806+
import six
3807+
class B(six.with_metaclass(A, C)):
3808+
pass
3809+
3810+
B #@
3811+
"""
3812+
)
3813+
inferred = next(ast_node.infer())
3814+
self.assertIsInstance(inferred, nodes.ClassDef)
3815+
self.assertEqual(inferred.name, "B")
3816+
bases = inferred.bases
3817+
self.assertIsInstance(bases[0], nodes.Call)
3818+
ancestors = tuple(inferred.ancestors())
3819+
self.assertIsInstance(ancestors[0], nodes.ClassDef)
3820+
self.assertEqual(ancestors[0].name, "C")
3821+
self.assertIsInstance(ancestors[1], nodes.ClassDef)
3822+
self.assertEqual(ancestors[1].name, "object")
3823+
37963824
def test_With_metaclass_with_partial_imported_name(self):
37973825
ast_node = extract_node(
37983826
"""

0 commit comments

Comments
 (0)