Unclear when Join is picked vs Union in upcast operation.
Open
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
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
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