Skip to content

Commit 6cad6f9

Browse files
author
Guido van Rossum
committed
Address code review
1 parent 07123b3 commit 6cad6f9

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

mypy/semanal.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -903,7 +903,7 @@ def analyze_base_classes(self, defn: ClassDef) -> None:
903903
base_types = [] # type: List[Instance]
904904
info = defn.info
905905

906-
base_type_exprs = self.get_base_type_exprs(defn)
906+
base_type_exprs = self.check_with_metaclass(defn)
907907
for base_expr in base_type_exprs:
908908
try:
909909
base = self.expr_to_analyzed_type(base_expr)
@@ -955,9 +955,9 @@ def analyze_base_classes(self, defn: ClassDef) -> None:
955955
if defn.info.is_enum and defn.type_vars:
956956
self.fail("Enum class cannot be generic", defn)
957957

958-
def get_base_type_exprs(self, defn: ClassDef) -> List[Expression]:
959-
# Special-case six.with_metaclass(M, B1, B2, ...)
960-
# TODO: factor out in helper method
958+
def check_with_metaclass(self, defn: ClassDef) -> List[Expression]:
959+
# Special-case six.with_metaclass(M, B1, B2, ...).
960+
# May update defn.metaclass.
961961
base_type_exprs = defn.base_type_exprs
962962
if defn.metaclass is None and len(base_type_exprs) == 1:
963963
base_expr = base_type_exprs[0]

test-data/unit/check-classes.test

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3158,6 +3158,24 @@ class M(type):
31583158
class A(with_metaclass(M)): pass
31593159
reveal_type(type(A).x) # E: Revealed type is 'builtins.int'
31603160

3161+
[case testSixWithMetaclassImportFrom]
3162+
import six
3163+
from metadefs import M
3164+
class A(six.with_metaclass(M)): pass
3165+
reveal_type(type(A).x) # E: Revealed type is 'builtins.int'
3166+
[file metadefs.py]
3167+
class M(type):
3168+
x = 5
3169+
3170+
[case testSixWithMetaclassImport]
3171+
import six
3172+
import metadefs
3173+
class A(six.with_metaclass(metadefs.M)): pass
3174+
reveal_type(type(A).x) # E: Revealed type is 'builtins.int'
3175+
[file metadefs.py]
3176+
class M(type):
3177+
x = 5
3178+
31613179
[case testSixWithMetaclassAndBase]
31623180
import six
31633181
class M(type):
@@ -3180,10 +3198,12 @@ C2().baz() # E: "C2" has no attribute "baz"
31803198
import six
31813199
class M(type): pass
31823200
class A(object): pass
3201+
def f() -> type: return M
31833202
class C1(six.with_metaclass(M), object): pass # E: Invalid base class
31843203
class C2(C1, six.with_metaclass(M)): pass # E: Invalid base class
31853204
class C3(six.with_metaclass(A)): pass # E: Metaclasses not inheriting from 'type' are not supported
31863205
class C4(six.with_metaclass(M), metaclass=M): pass # E: Invalid base class
3206+
class C5(six.with_metaclass(f())): pass # E: Dynamic metaclass not supported for 'C5'
31873207

31883208
[case testSixWithMetaclassErrors_python2-skip]
31893209
# No error here yet

0 commit comments

Comments
 (0)