Skip to content

Commit 5f01da9

Browse files
Revert #893: Avoid setting a Call as a base for metaclasses from six.with_metaclass() (#1622)
1 parent 7db01c1 commit 5f01da9

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

ChangeLog

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ Release date: TBA
88

99
* ``astroid`` now requires Python 3.7.2 to run.
1010

11+
* Avoid setting a Call as a base for classes created using ``six.with_metaclass()``.
12+
13+
Refs PyCQA/pylint#5935
14+
1115
* Fix detection of builtins on ``PyPy`` 3.9.
1216

1317
* Fix ``re`` brain on Python ``3.11``. The flags now come from ``re._compile``.

astroid/brain/brain_six.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ def transform_six_with_metaclass(node):
219219
"""
220220
call = node.bases[0]
221221
node._metaclass = call.args[0]
222+
node.bases = call.args[1:]
222223
return node
223224

224225

tests/unittest_brain.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -569,13 +569,29 @@ class B(six.with_metaclass(A, C)):
569569
inferred = next(ast_node.infer())
570570
self.assertIsInstance(inferred, nodes.ClassDef)
571571
self.assertEqual(inferred.name, "B")
572-
self.assertIsInstance(inferred.bases[0], nodes.Call)
572+
self.assertIsInstance(inferred.bases[0], nodes.Name)
573+
self.assertEqual(inferred.bases[0].name, "C")
573574
ancestors = tuple(inferred.ancestors())
574575
self.assertIsInstance(ancestors[0], nodes.ClassDef)
575576
self.assertEqual(ancestors[0].name, "C")
576577
self.assertIsInstance(ancestors[1], nodes.ClassDef)
577578
self.assertEqual(ancestors[1].name, "object")
578579

580+
@staticmethod
581+
def test_six_with_metaclass_enum_ancestor() -> None:
582+
code = """
583+
import six
584+
from enum import Enum, EnumMeta
585+
586+
class FooMeta(EnumMeta):
587+
pass
588+
589+
class Foo(six.with_metaclass(FooMeta, Enum)): #@
590+
bar = 1
591+
"""
592+
klass = astroid.extract_node(code)
593+
assert list(klass.ancestors())[-1].name == "Enum"
594+
579595
def test_six_with_metaclass_with_additional_transform(self) -> None:
580596
def transform_class(cls: Any) -> ClassDef:
581597
if cls.name == "A":

0 commit comments

Comments
 (0)