Skip to content

Commit

Permalink
Restore Type vs Callable special-casing that was broken in refactoring (
Browse files Browse the repository at this point in the history
#13784)

Fixes #13756
  • Loading branch information
ilevkivskyi committed Sep 30, 2022
1 parent 46aee5a commit 55ee086
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
4 changes: 4 additions & 0 deletions mypy/subtypes.py
Expand Up @@ -893,6 +893,10 @@ def visit_type_type(self, left: TypeType) -> bool:
if isinstance(right, TypeType):
return self._is_subtype(left.item, right.item)
if isinstance(right, CallableType):
if self.proper_subtype and not right.is_type_obj():
# We can't accept `Type[X]` as a *proper* subtype of Callable[P, X]
# since this will break transitivity of subtyping.
return False
# This is unsound, we don't check the __init__ signature.
return self._is_subtype(left.item, right.ret_type)
if isinstance(right, Instance):
Expand Down
10 changes: 10 additions & 0 deletions test-data/unit/check-inference.test
Expand Up @@ -3372,3 +3372,13 @@ class B:
[out]
tmp/b.py:2: note: Revealed type is "builtins.int"
tmp/a.py:2: note: Revealed type is "builtins.int"

[case testUnionTypeCallableInference]
from typing import Callable, Type, TypeVar, Union

class A:
def __init__(self, x: str) -> None: ...

T = TypeVar("T")
def type_or_callable(value: T, tp: Union[Type[T], Callable[[int], T]]) -> T: ...
reveal_type(type_or_callable(A("test"), A)) # N: Revealed type is "__main__.A"

0 comments on commit 55ee086

Please sign in to comment.