Skip to content

Commit aec4595

Browse files
ilevkivskyigvanrossum
authored andcommitted
Fix a regression in Type[Any] (#3012)
Fixes #3010 This just takes care of a corner case: every metaclass is a subtype of Type[Any].
1 parent 44b5f41 commit aec4595

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

mypy/subtypes.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,14 @@ def visit_instance(self, left: Instance) -> bool:
160160
item = right.item
161161
if isinstance(item, TupleType):
162162
item = item.fallback
163-
return isinstance(item, Instance) and is_subtype(left, item.type.metaclass_type)
163+
if isinstance(item, Instance):
164+
return is_subtype(left, item.type.metaclass_type)
165+
elif isinstance(item, AnyType):
166+
# Special case: all metaclasses are subtypes of Type[Any]
167+
mro = left.type.mro or []
168+
return any(base.fullname() == 'builtins.type' for base in mro)
169+
else:
170+
return False
164171
else:
165172
return False
166173

test-data/unit/check-classes.test

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2217,6 +2217,20 @@ C1[Type[Any]], C2[type] # both these should not fail
22172217
[builtins fixtures/list.pyi]
22182218
[out]
22192219

2220+
[case testTypeEquivalentTypeAnyEdgeCase]
2221+
class C:
2222+
pass
2223+
2224+
class M(type):
2225+
def __init__(cls, x) -> None:
2226+
type.__init__(cls, x)
2227+
2228+
class Mbad(type):
2229+
def __init__(cls, x) -> None:
2230+
type.__init__(C(), x) # E: Argument 1 to "__init__" of "type" has incompatible type "C"; expected "type"
2231+
[builtins fixtures/primitives.pyi]
2232+
[out]
2233+
22202234
[case testTypeMatchesOverloadedFunctions]
22212235
from typing import Type, overload, Union
22222236

0 commit comments

Comments
 (0)