False positive: "overloaded signature will never be matched" with Self
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
Bug Report
When combining Self and base class overloads, mypy incorrectly warns about the former being "the same or broader" as the latter. However, those overloads are both actually used.
I do know that this can be solved with a single TypeVar - the case is extracted from a more complicated code where similar TypeVar solution doesn't seem to apply.
To Reproduce
from __future__ import annotations
from typing import Self, overload
class Base:
@overload
def fn(self, other: Self) -> Self: ...
@overload
def fn(self, other: Base) -> Base: ...
def fn(self, other: Base) -> Base:
return other
class X(Base): ...
class Y(Base): ...
reveal_type(X().fn(X()))
reveal_type(X().fn(Y()))
reveal_type(Y().fn(X()))
reveal_type(Y().fn(Y()))
Expected Behavior
No errors reported
Actual Behavior
main.py:10: error: Overloaded function signature 2 will never be matched: signature 1's parameter type(s) are the same or broader [misc]
main.py:18: note: Revealed type is "__main__.X"
main.py:19: note: Revealed type is "__main__.Base"
main.py:20: note: Revealed type is "__main__.Base"
main.py:21: note: Revealed type is "__main__.Y"
Found 1 error in 1 file (checked 1 source file)
Your Environment
- Mypy version used: 1.11.1 and master (playground)
- Mypy command-line flags: none and
--strict- both reproduce - Mypy configuration options from
mypy.ini(and other config files): none - Python version used: 3.11, 3.12 - does not affect
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 linked mypy-play reproduction and the overload example in the issue, then trace how mypy checks overlapping overload signatures involving Self and base classes. Done means the example reports no false-positive error while preserving the four revealed types shown in the report.
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