Skip to content

Commit 51d9858

Browse files
committed
Restore Type vs Callable special-casing that was broken in refactoring (#13784)
Fixes #13756
1 parent d03f201 commit 51d9858

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

mypy/subtypes.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -883,6 +883,10 @@ def visit_type_type(self, left: TypeType) -> bool:
883883
if isinstance(right, TypeType):
884884
return self._is_subtype(left.item, right.item)
885885
if isinstance(right, CallableType):
886+
if self.proper_subtype and not right.is_type_obj():
887+
# We can't accept `Type[X]` as a *proper* subtype of Callable[P, X]
888+
# since this will break transitivity of subtyping.
889+
return False
886890
# This is unsound, we don't check the __init__ signature.
887891
return self._is_subtype(left.item, right.ret_type)
888892
if isinstance(right, Instance):

test-data/unit/check-inference.test

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3264,3 +3264,13 @@ from typing import Dict, Iterable, Tuple, Union
32643264
def foo(x: Union[Tuple[str, Dict[str, int], str], Iterable[object]]) -> None: ...
32653265
foo(("a", {"a": "b"}, "b"))
32663266
[builtins fixtures/dict.pyi]
3267+
3268+
[case testUnionTypeCallableInference]
3269+
from typing import Callable, Type, TypeVar, Union
3270+
3271+
class A:
3272+
def __init__(self, x: str) -> None: ...
3273+
3274+
T = TypeVar("T")
3275+
def type_or_callable(value: T, tp: Union[Type[T], Callable[[int], T]]) -> T: ...
3276+
reveal_type(type_or_callable(A("test"), A)) # N: Revealed type is "__main__.A"

0 commit comments

Comments
 (0)