Skip to content

Commit 7b4f631

Browse files
authored
Add missing branch for is_subtype(TypeType, Overload) (#18975)
Fixes #18974. This simply adds a missed branch - we do support `is_subtype(Overload, TypeType)` but not another type order.
1 parent 6aec4b8 commit 7b4f631

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

mypy/subtypes.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1091,6 +1091,11 @@ 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, Overloaded) and right.is_type_obj():
1095+
# Same as in other direction: if it's a constructor callable, all
1096+
# items should belong to the same class' constructor, so it's enough
1097+
# to check one of them.
1098+
return self._is_subtype(left, right.items[0])
10941099
if isinstance(right, CallableType):
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]

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)