@@ -69,11 +69,6 @@ def is_subtype(left: Type, right: Type,
69
69
elif is_subtype_of_item :
70
70
return True
71
71
# otherwise, fall through
72
- # Treat builtins.type the same as Type[Any]
73
- elif is_named_instance (left , 'builtins.type' ):
74
- return is_subtype (TypeType (AnyType ()), right )
75
- elif is_named_instance (right , 'builtins.type' ):
76
- return is_subtype (left , TypeType (AnyType ()))
77
72
return left .accept (SubtypeVisitor (right , type_parameter_checker ,
78
73
ignore_pos_arg_names = ignore_pos_arg_names ))
79
74
@@ -155,19 +150,18 @@ def visit_instance(self, left: Instance) -> bool:
155
150
for lefta , righta , tvar in
156
151
zip (t .args , right .args , right .type .defn .type_vars ))
157
152
if isinstance (right , TypeType ):
158
- item = right .item
159
- if isinstance (item , TupleType ):
160
- item = item .fallback
161
- if isinstance (item , Instance ):
162
- return is_subtype (left , item .type .metaclass_type )
163
- elif isinstance (item , AnyType ):
164
- # Special case: all metaclasses are subtypes of Type[Any]
165
- mro = left .type .mro or []
166
- return any (base .fullname () == 'builtins.type' for base in mro )
167
- else :
168
- return False
169
- else :
170
- return False
153
+ if is_named_instance (left , 'builtins.type' ):
154
+ return is_subtype (TypeType (AnyType ()), right )
155
+ if left .type .is_metaclass ():
156
+ if isinstance (right .item , AnyType ):
157
+ return True
158
+ if isinstance (right .item , Instance ):
159
+ # Special-case enum since we don't have better way of expressing it
160
+ if (is_named_instance (left , 'enum.EnumMeta' )
161
+ and is_named_instance (right .item , 'enum.Enum' )):
162
+ return True
163
+ return is_named_instance (right .item , 'builtins.object' )
164
+ return False
171
165
172
166
def visit_type_var (self , left : TypeVarType ) -> bool :
173
167
right = self .right
@@ -263,8 +257,7 @@ def visit_overloaded(self, left: Overloaded) -> bool:
263
257
elif isinstance (right , TypeType ):
264
258
# All the items must have the same type object status, so
265
259
# it's sufficient to query only (any) one of them.
266
- # This is unsound, we don't check the __init__ signature.
267
- return left .is_type_obj () and is_subtype (left .items ()[0 ].ret_type , right .item )
260
+ return left .is_type_obj () and is_subtype (left .items ()[0 ], right )
268
261
else :
269
262
return False
270
263
@@ -284,11 +277,14 @@ def visit_type_type(self, left: TypeType) -> bool:
284
277
# This is unsound, we don't check the __init__ signature.
285
278
return is_subtype (left .item , right .ret_type )
286
279
if isinstance (right , Instance ):
287
- if right .type .fullname () == 'builtins.object' :
288
- # treat builtins.object the same as Any.
280
+ if right .type .fullname () in ['builtins.object' , 'builtins.type' ]:
289
281
return True
290
282
item = left .item
291
- return isinstance (item , Instance ) and is_subtype (item .type .metaclass_type , right )
283
+ if isinstance (item , TypeVarType ):
284
+ item = item .upper_bound
285
+ if isinstance (item , Instance ):
286
+ metaclass = item .type .metaclass_type
287
+ return metaclass is not None and is_subtype (metaclass , right )
292
288
return False
293
289
294
290
0 commit comments