Assignability of overloaded implementation to `Proto[**P](Protocol)` is overload-order-sensitive
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
Bug Report, To Reproduce & Actual Behaviour
When matching a method signature in a protocol with an overloaded implementation, the implementation's overload order appears to matter, even when only one of the overloads match.
In the following example, only the first overload of the implementation is tried for compatibility with the protocol (which looks similar to the issue in #14070). See mypy Playground:
# mypy: disable-error-code=no-overload-impl
from typing import *
_A_contra = TypeVar("_A_contra", bound=Literal["a", "b"], contravariant=True)
_P = ParamSpec("_P")
class Callback(Protocol[_A_contra, _P]):
def method(self, a: _A_contra, *args: _P.args, **kwargs: _P.kwargs) -> None: ...
class Impl:
@overload
def method(self, a: Literal["a"], b: int) -> None: ...
@overload
def method(self, a: int, b: str) -> None: ...
class Impl2: # Overload signature order is reversed compared with `Impl`
@overload
def method(self, a: int, b: str) -> None: ...
@overload
def method(self, a: Literal["a"], b: int) -> None: ...
def accepts_callback(cb: Callback[Literal["a"], _P], *args: _P.args, **kwargs: _P.kwargs) -> int:
return 1
a = accepts_callback(Impl(), 1)
b = accepts_callback(Impl2(), 1) # Error
reveal_type(a)
reveal_type(b)
Expected Behavior
No errors
Your Environment
- Mypy version used: 1.16.1, master
- Python version used: 3.12
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the provided Python 3.12 reproducer in the mypy Playground and compare how Impl and Impl2 are checked against Callback. Verify the expected result by running the example and checking that both calls produce no error and that the revealed types remain consistent.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100