(🎁) Perform 'union math' on non overloaded calls
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
Here the call to f is distributing the generic over the return type (based), but the call to the standalone function can't be resolved
from typing import Iterable, overload, TypeVar
a: list[int] | list[str]
@overload
def f[T](i: list[T]) -> list[T]: ...
@overload
def f() -> None: ...
def f[T](i: list[T] = []) -> list[T] | None: ...
reveal_type(f(a)) # list[int] | list[str]
def g[T](i: list[T]) -> list[T]: return []
reveal_type(g(a)) # error: Cannot infer type argument 1 of "g" [misc]
What's happening here is that union_overload_result, which is intended for when a call with a union would match multiple overload parts, is matching twice against the same overload, resulting is a more accurate return type.
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 reproducing the example in the linked mypy playground, then trace the union_overload_result logic described in the issue. The work is done when a standalone generic call distributes over a union and infers the corresponding union return type without the current inference error, while existing overload behavior remains correct.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- compilers, devtools
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100