File tree 2 files changed +24
-4
lines changed 2 files changed +24
-4
lines changed Original file line number Diff line number Diff line change @@ -939,7 +939,7 @@ def analyze_base_classes(self, defn: ClassDef) -> None:
939
939
base_types = [] # type: List[Instance]
940
940
info = defn .info
941
941
942
- base_type_exprs = self .get_base_type_exprs (defn )
942
+ base_type_exprs = self .check_with_metaclass (defn )
943
943
for base_expr in base_type_exprs :
944
944
try :
945
945
base = self .expr_to_analyzed_type (base_expr )
@@ -991,9 +991,9 @@ def analyze_base_classes(self, defn: ClassDef) -> None:
991
991
if defn .info .is_enum and defn .type_vars :
992
992
self .fail ("Enum class cannot be generic" , defn )
993
993
994
- def get_base_type_exprs (self , defn : ClassDef ) -> List [Expression ]:
995
- # Special-case six.with_metaclass(M, B1, B2, ...)
996
- # TODO: factor out in helper method
994
+ def check_with_metaclass (self , defn : ClassDef ) -> List [Expression ]:
995
+ # Special-case six.with_metaclass(M, B1, B2, ...).
996
+ # May update defn.metaclass.
997
997
base_type_exprs = defn .base_type_exprs
998
998
if defn .metaclass is None and len (base_type_exprs ) == 1 :
999
999
base_expr = base_type_exprs [0 ]
Original file line number Diff line number Diff line change @@ -3158,6 +3158,24 @@ class M(type):
3158
3158
class A(with_metaclass(M)): pass
3159
3159
reveal_type(type(A).x) # E: Revealed type is 'builtins.int'
3160
3160
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
+
3161
3179
[case testSixWithMetaclassAndBase]
3162
3180
import six
3163
3181
class M(type):
@@ -3180,10 +3198,12 @@ C2().baz() # E: "C2" has no attribute "baz"
3180
3198
import six
3181
3199
class M(type): pass
3182
3200
class A(object): pass
3201
+ def f() -> type: return M
3183
3202
class C1(six.with_metaclass(M), object): pass # E: Invalid base class
3184
3203
class C2(C1, six.with_metaclass(M)): pass # E: Invalid base class
3185
3204
class C3(six.with_metaclass(A)): pass # E: Metaclasses not inheriting from 'type' are not supported
3186
3205
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'
3187
3207
3188
3208
[case testSixWithMetaclassErrors_python2-skip]
3189
3209
# No error here yet
You can’t perform that action at this time.
0 commit comments