Skip to content

Unclear when Join is picked vs Union in upcast operation. #19652

@randolf-scholz

Description

@randolf-scholz

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-join-v-unionUsing join vs. using unions

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions