python / python/mypy

(🎁) Perform 'union math' on non overloaded calls

Open
#13,493 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

feature topic-type-variables topic-union-types
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]

playground

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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.