Skip to content

Commit ee3122f

Browse files
authored
Fix subtyping between Instance and Overloaded (#18102)
Fixes #18101
1 parent 1f200dd commit ee3122f

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

mypy/subtypes.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -625,8 +625,8 @@ def visit_instance(self, left: Instance) -> bool:
625625
return is_named_instance(item, "builtins.object")
626626
if isinstance(right, LiteralType) and left.last_known_value is not None:
627627
return self._is_subtype(left.last_known_value, right)
628-
if isinstance(right, CallableType):
629-
# Special case: Instance can be a subtype of Callable.
628+
if isinstance(right, FunctionLike):
629+
# Special case: Instance can be a subtype of Callable / Overloaded.
630630
call = find_member("__call__", left, left, is_operator=True)
631631
if call:
632632
return self._is_subtype(call, right)

test-data/unit/check-protocols.test

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4215,3 +4215,24 @@ def g4(a: Input[bytes], b: Output[str]) -> None:
42154215
f(a, b) # E: Cannot infer type argument 1 of "f"
42164216

42174217
[builtins fixtures/tuple.pyi]
4218+
4219+
[case testOverloadProtocolSubtyping]
4220+
from typing import Protocol, Self, overload
4221+
4222+
class NumpyFloat:
4223+
__add__: "FloatOP"
4224+
4225+
class FloatOP(Protocol):
4226+
@overload
4227+
def __call__(self, other: float) -> NumpyFloat: ...
4228+
@overload
4229+
def __call__(self, other: NumpyFloat) -> NumpyFloat: ...
4230+
4231+
class SupportsAdd(Protocol):
4232+
@overload
4233+
def __add__(self, other: float) -> Self: ...
4234+
@overload
4235+
def __add__(self, other: NumpyFloat) -> Self: ...
4236+
4237+
x: SupportsAdd = NumpyFloat()
4238+
[builtins fixtures/tuple.pyi]

0 commit comments

Comments
 (0)