Skip to content

Commit

Permalink
The node.bases has not to be tweaked otherwise leads to false positiv…
Browse files Browse the repository at this point in the history
…e 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.
  • Loading branch information
hippo91 authored and Pierre-Sassoulas committed Feb 12, 2021
1 parent 0d8ed3f commit dc560d4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
1 change: 0 additions & 1 deletion astroid/brain/brain_six.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
28 changes: 28 additions & 0 deletions tests/unittest_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -3793,6 +3793,34 @@ class B(six.with_metaclass(A)):
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(
"""
Expand Down

0 comments on commit dc560d4

Please sign in to comment.