Skip to content

Fix subtyping between Instance and Overloaded #18102

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

Merged
merged 1 commit into from
Nov 4, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions mypy/subtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -625,8 +625,8 @@ def visit_instance(self, left: Instance) -> bool:
return is_named_instance(item, "builtins.object")
if isinstance(right, LiteralType) and left.last_known_value is not None:
return self._is_subtype(left.last_known_value, right)
if isinstance(right, CallableType):
# Special case: Instance can be a subtype of Callable.
if isinstance(right, FunctionLike):
# Special case: Instance can be a subtype of Callable / Overloaded.
call = find_member("__call__", left, left, is_operator=True)
if call:
return self._is_subtype(call, right)
Expand Down
21 changes: 21 additions & 0 deletions test-data/unit/check-protocols.test
Original file line number Diff line number Diff line change
Expand Up @@ -4215,3 +4215,24 @@ def g4(a: Input[bytes], b: Output[str]) -> None:
f(a, b) # E: Cannot infer type argument 1 of "f"

[builtins fixtures/tuple.pyi]

[case testOverloadProtocolSubtyping]
from typing import Protocol, Self, overload

class NumpyFloat:
__add__: "FloatOP"

class FloatOP(Protocol):
@overload
def __call__(self, other: float) -> NumpyFloat: ...
@overload
def __call__(self, other: NumpyFloat) -> NumpyFloat: ...

class SupportsAdd(Protocol):
@overload
def __add__(self, other: float) -> Self: ...
@overload
def __add__(self, other: NumpyFloat) -> Self: ...

x: SupportsAdd = NumpyFloat()
[builtins fixtures/tuple.pyi]
Loading