overload with wrong return type is ignored
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
Bug Report
When one of the alternatives of an overload has a return type T which is a subtype of the return type of the annotation U but that the body of the function returns a type V which is a subtype of U but not T, no error is reported
To Reproduce
from typing import overload
@overload
def f(a: int) -> str:
...
# this is completely wrong
@overload
def f(a: str) -> bool:
...
def f(a: str | int) -> str | bool:
return "Hello"
reveal_type(f("some string"))
https://mypy-play.net/?mypy=latest&python=3.12&flags=strict&gist=83c5a353e087d8bbe03d542e7340b4b5
Expected Behavior
mypy should complain that the body of the function returns an str which is incompatible with type bool (overload 2)
More generally, for each overload signature, mypy should check that the body of the function typechecks with the overload signature.
Actual Behavior
mypy reports no error
main.py:16: note: Revealed type is "builtins.bool"
Success: no issues found in 1 source file
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 Python reproducer shown in main.py and run it with the linked mypy-play strict configuration. Trace how overload signatures are checked against the implementation return type. Done means mypy reports the incompatible str return for overload 2 and the regression is covered by a test.
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
- 38/100