Mypy should ensure overloads are compatible with the implementation
Open
Nobody has claimed this yet.
feature
topic-overloads
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
Bug Report
Mypy should ensure overloads are compatible with the implementation
To Reproduce
from typing import Union, overload
from typing_extensions import Literal
@overload
def f(*, flag: Literal[True]) -> float: ...
@overload
def f(*, flag: Literal[False] = False) -> str: ...
def f(*, flag: bool = False) -> Union[str, float]:
return 5
reveal_type(f(flag=True))
reveal_type(f(flag=False))
reveal_type(f())
Expected Behavior
an error along the lines of "signature 2 incompatible with implementation" because it cannot return a str when flag=False
Actual Behavior
$ mypy test.py
test.py:11:13: note: Revealed type is "builtins.float"
test.py:12:13: note: Revealed type is "builtins.str"
test.py:13:13: note: Revealed type is "builtins.str"
Your Environment
- Mypy version used:
$ mypy --version
mypy 0.910
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 by running the provided overload example with mypy 0.910 and confirm the revealed types and missing diagnostic. Trace the overload compatibility checks for the implementation signature; done means incompatible overloads, such as the false-flag case returning str, produce an error without rejecting compatible overloads.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100