overload, Protocol and generic self: annotations
Open
Nobody has claimed this yet.
bug
topic-overloads
topic-protocols
topic-self-types
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
overload with a self annotation involving a protocol never get matched:
class Proto[A_contra](Protocol):
def __call__(self, arg: A_contra, /) -> None: ...
class Fn:
def __call__(self, arg: int, /) -> None:
pass
@dataclass(frozen=True)
class Testclass[T_co]:
val: T_co
# this overload is problematic
@overload
def meth[T2](self: Testclass[Proto[T2]], arg: T2) -> int: ...
@overload
def meth(self, arg: None) -> str: ...
def meth(self: Any, *args, **kwargs):
return self.val
def fn(x: Testclass[Fn]):
# error: No overload variant of "meth" of "Testclass" matches argument type "int" [call-overload]
# note: Possible overload variant:
# note: def meth(self, arg: None) -> str
x.meth(5)
it works fine if it is not an overload, or if using Callable rather than Proto.
https://mypy-play.net/?mypy=latest&python=3.12&gist=39ec9991c68ca9ed50f149359459e858
this works fine in pyright.
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 minimal Python example and mypy-play.net reproduction linked in the issue, then compare its result with pyright. Trace the overload matching path for a generic self annotation involving the Proto protocol. Done means the first overload accepts x.meth(5) as shown, without regressing the None overload or the Callable case.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100