Only use annotations of `@overload`ed functions to type check the implementation
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
Consider the following real-world example of a function. The type of one of the parameters of this function depends on another one:
from typing import *
K = TypeVar("K", bound=Hashable)
V = TypeVar("V")
@overload
def dict_get_factory(dictionary: Dict[K, V],
key: K,
factory: Callable[[], V],
takes_key: Literal[False]) -> V: ...
@overload
def dict_get_factory(dictionary: Dict[K, V],
key: K,
factory: Callable[[K], V],
takes_key: Literal[True]) -> V: ...
def dict_get_factory(dictionary: Dict[K, V],
key: K,
factory: Union[Callable[[], V], Callable[[K], V]],
takes_key: bool=False) -> V:
"""Like dict.setdefault(), but uses a factory
>>> dict_get_factory(d := {}, "foo", int)
0
>>> dict_get_factory(d, "bar", lambda key: len(key), takes_key=True)
3
>>> d
{'foo': 0, 'bar': 3}
"""
try:
return dictionary[key]
except KeyError:
return dictionary.setdefault(key,
factory(key) if takes_key else factory())
i1: int = dict_get_factory({"foo": 1}, "bar", lambda: 1, False) # ok
i2: int = dict_get_factory({"foo": 1}, "bar", lambda s: 1, True) # ok
It doesn't typecheck as if you only look at the annotations of the implementation, the signature of the factory is ambiguous:
main.py:36: error: Too many arguments
main.py:36: error: Too few arguments
I don't think it's possible to annotate the implementation in such a way that it would typecheck. So it would be nice if mypy used the signatures of the @overloaded methods, and one wouldn't have to annotate the signature of the implementation at all. Even if you could annotate it in such a way that it would typecheck, wouldn't it be redundant?
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 overload example in the issue and inspect mypy's handling of implementation signatures and overload checking. Determine the intended behavior for calls inside the implementation, then add focused regression coverage and verify that the example no longer reports conflicting factory-call errors.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- compilers, devtools
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100