Skip to content

Commit f37551d

Browse files
committed
Add missing branch for is_subtype(TypeType, Overload)
1 parent 6aec4b8 commit f37551d

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

mypy/subtypes.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1091,7 +1091,12 @@ def visit_type_type(self, left: TypeType) -> bool:
10911091
right = self.right
10921092
if isinstance(right, TypeType):
10931093
return self._is_subtype(left.item, right.item)
1094-
if isinstance(right, CallableType):
1094+
if isinstance(right, (Overloaded, CallableType)):
1095+
if isinstance(right, Overloaded) and right.is_type_obj():
1096+
# Same as in other direction: if it's a constructor callable, all
1097+
# items should belong to the same class' constructor, so it's enough
1098+
# to check one of them.
1099+
right = right.items[0]
10951100
if self.proper_subtype and not right.is_type_obj():
10961101
# We can't accept `Type[X]` as a *proper* subtype of Callable[P, X]
10971102
# since this will break transitivity of subtyping.

test-data/unit/check-assert-type-fail.test

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,19 @@ def f(si: arr.array[int]):
3131
from typing import assert_type, Callable
3232
def myfunc(arg: int) -> None: pass
3333
assert_type(myfunc, Callable[[int], None]) # E: Expression is of type "Callable[[Arg(int, 'arg')], None]", not "Callable[[int], None]"
34+
35+
[case testAssertTypeOverload]
36+
from typing import assert_type, overload
37+
38+
class Foo:
39+
@overload
40+
def __new__(cls, x: int) -> Foo: ...
41+
@overload
42+
def __new__(cls, x: str) -> Foo: ...
43+
def __new__(cls, x: "int | str") -> Foo:
44+
return cls(0)
45+
46+
assert_type(Foo, type[Foo])
47+
A = Foo
48+
assert_type(A, type[Foo])
49+
[builtins fixtures/tuple.pyi]

0 commit comments

Comments
 (0)