Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restore Type vs Callable special-casing that was broken in refactoring #13784

Merged
merged 1 commit into from Sep 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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"