`@overload` breaks with named tuple and some overloaded functions
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
Bug Report
Mypy incorrectly reports Any as the resolved type (quite possibly due this https://github.com/python/typing/issues/253#issuecomment-235358878 )
To Reproduce
from collections import namedtuple
from typing import Any, Callable, cast, Dict, List, NamedTuple, overload, TypeVar, Union
F = TypeVar("F", bound=Callable[..., Any])
Arg: NamedTuple = namedtuple("Arg", ["arg"])
class Test:
@overload
def __call__(self, fn: F) -> F:
...
@overload
def __call__(self, *args: Any, **kwargs: Any) -> "Test":
...
def __call__(self, *args: Any, **kwargs: Any) -> Union[F, "Test"]:
if len(args) == 1:
return cast(F, args[0])
return self
reveal_type(Test()(Test))
def broken(arg: Arg) -> Arg:
return arg
reveal_type(broken)
reveal_type(Test()(broken))
def working(arg: int) -> int:
return arg
reveal_type(Test()(working))
@overload
def overloaded_broken(arg: F) -> F:
...
@overload
def overloaded_broken(*args: Any, **kwargs: Any) -> Dict[str, int]:
...
def overloaded_broken(*args: Any, **kwargs: Any) -> Union[F, Dict[str, int]]:
if len(args) == 1:
return cast(F, args[0])
return dict(args=len(args), kwargs=len(kwargs))
reveal_type(overloaded_broken)
reveal_type(Test()(overloaded_broken))
@overload
def overloaded_working(arg: str) -> str:
...
@overload
def overloaded_working(arg: bytes) -> bytes:
...
def overloaded_working(arg: Union[bytes, str]) -> Union[bytes, str]:
return arg
reveal_type(Test()(overloaded_working))
Expected Behavior
All revealed types are printed as non Any
Actual Behavior
# a class is OK
test.py:23:13: note: Revealed type is 'def () -> test.Test'
# this is what is expected when using a named tuple, but it resolves as any
test.py:30:13: note: Revealed type is 'def (arg: Tuple[Any, fallback=test.Arg]) -> Tuple[Any, fallback=test.Arg]'
test.py:31:13: note: Revealed type is 'Any'
# this is not using a named tuple and it works as expected
test.py:38:13: note: Revealed type is 'def (arg: builtins.int) -> builtins.int'
# this is what is expected for a complex overloaded function, but it resolves as any
test.py:57:13: note: Revealed type is 'Overload(def [F <: def (*Any, **Any) -> Any] (arg: F`-1) -> F`-1, def (*args: Any, **kwargs: Any) -> builtins.dict[builtins.str, builtins.int])'
test.py:58:13: note: Revealed type is 'Any'
# passing a simpler overloaded function
test.py:73:13: note: Revealed type is 'Overload(def (arg: builtins.str) -> builtins.str, def (arg: builtins.bytes) -> builtins.bytes)'
Your Environment
mypy --version: mypy 0.812 (default config)
python --version: Python 3.8.9
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 reproduction in test.py with mypy 0.812 and compare the revealed types for the named-tuple and overloaded-function cases. Trace overload resolution and the handling of named-tuple types, then verify that the affected reveals resolve to their expected callable types rather than Any.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100