Skip to content

Commit 55ee086

Browse files
authored
Restore Type vs Callable special-casing that was broken in refactoring (#13784)
Fixes #13756
1 parent 46aee5a commit 55ee086

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

mypy/subtypes.py

+4
Original file line numberDiff line numberDiff line change
@@ -893,6 +893,10 @@ def visit_type_type(self, left: TypeType) -> bool:
893893
if isinstance(right, TypeType):
894894
return self._is_subtype(left.item, right.item)
895895
if isinstance(right, CallableType):
896+
if self.proper_subtype and not right.is_type_obj():
897+
# We can't accept `Type[X]` as a *proper* subtype of Callable[P, X]
898+
# since this will break transitivity of subtyping.
899+
return False
896900
# This is unsound, we don't check the __init__ signature.
897901
return self._is_subtype(left.item, right.ret_type)
898902
if isinstance(right, Instance):

test-data/unit/check-inference.test

+10
Original file line numberDiff line numberDiff line change
@@ -3372,3 +3372,13 @@ class B:
33723372
[out]
33733373
tmp/b.py:2: note: Revealed type is "builtins.int"
33743374
tmp/a.py:2: note: Revealed type is "builtins.int"
3375+
3376+
[case testUnionTypeCallableInference]
3377+
from typing import Callable, Type, TypeVar, Union
3378+
3379+
class A:
3380+
def __init__(self, x: str) -> None: ...
3381+
3382+
T = TypeVar("T")
3383+
def type_or_callable(value: T, tp: Union[Type[T], Callable[[int], T]]) -> T: ...
3384+
reveal_type(type_or_callable(A("test"), A)) # N: Revealed type is "__main__.A"

0 commit comments

Comments
 (0)