Return type of function overload is ignored if parameter has type Any
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
Bug Report
mypy ignores the return type of a function if the annotation uses @overload and one of the parameters has type Any.
Possible duplicate of #14995.
To Reproduce
https://mypy-play.net/?mypy=1.1.1&python=3.11&gist=5ca80f2b256e4d31a8c588b15702b085
@overload
def toInt(value: int) -> int: ...
@overload
def toInt(value: float) -> int: ...
@overload
def toInt(value: Any) -> int | None: ...
def toInt(value: Any) -> int | None:
try:
return int(value)
except (TypeError, ValueError):
return None
reveal_type(toInt(1))
any_obj: Any = object()
reveal_type(toInt(any_obj))
Expected Behavior
Revealed type is "builtins.int"
Revealed type is "Union[builtins.int, None]"
Actual Behavior
Output of mypy
Revealed type is "builtins.int"
Revealed type is "Any"
vs. output of pyright
Type of "toInt(1)" is "int"
Type of "toInt(any_obj)" is "int | None"
Your Environment
- Mypy version used: 1.1.1
- Mypy command-line flags:
--python-version 3.11 - Mypy configuration options from
mypy.ini(and other config files): None - Python version used: 3.11.2
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 using mypy 1.1.1 and Python 3.11, then inspect overload resolution for calls whose argument is typed as Any. The fix is done when reveal_type reports int for the int call and int | None for the Any call, matching the expected output and pyright comparison.
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
- 30/100