python / python/mypy

Unclear when Join is picked vs Union in upcast operation.

Open
#19,652 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-join-v-union
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

I noticed some rather erratic behavior when sometimes it picks union and other times the join.

from typing import Iterable

class A: ...
class B: ...
class C: ...

def upcast[T](arg: Iterable[T]) -> Iterable[T]: return arg

def test_builtin(x: tuple[int, str, None]) -> None:
    reveal_type(upcast(x))  # Iterable[str | int | None]

def test_custom(x: tuple[A, B, C]) -> None:
    reveal_type(upcast(x))  # Iterable[object]

def test_mixed1(x: tuple[A, str, None]) -> None:
    reveal_type(upcast(x))  # Iterable[str | A | None]

def test_mixed2(x: tuple[int, str, C]) -> None:
    reveal_type(upcast(x))  # Iterable[object]

https://mypy-play.net/?mypy=latest&python=3.12&gist=833674a36174c4140a89645b609da8a9

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

Reproduce the four examples in the linked mypy-play setup, starting with the upcast calls and their reveal_type results. Trace the type inference path that chooses a union or object join, then define the intended consistent behavior and verify it against these examples.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.