Skip to content

Commit

Permalink
Fix subtyping between Instance and Overloaded (#18102)
Browse files Browse the repository at this point in the history
Fixes #18101
  • Loading branch information
hauntsaninja authored Nov 4, 2024
1 parent 1f200dd commit ee3122f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
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]

0 comments on commit ee3122f

Please sign in to comment.