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 Oct 2, 2022
1 parent d03f201 commit 51d9858
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 @@ -883,6 +883,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 @@ -3264,3 +3264,13 @@ from typing import Dict, Iterable, Tuple, Union
def foo(x: Union[Tuple[str, Dict[str, int], str], Iterable[object]]) -> None: ...
foo(("a", {"a": "b"}, "b"))
[builtins fixtures/dict.pyi]

[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 51d9858

Please sign in to comment.