Skip to content

Literal list ternary behave unexpected #19534

@randolf-scholz

Description

@randolf-scholz

https://mypy-play.net/?mypy=1.17.0&python=3.12&gist=25e4031ca17d70626baee7a468ac4d9f

def ternary_list[T, S](x: list[T], y: list[S]) -> None:
    a = x if "" else y
    reveal_type(a)  # list[T] | list[S] ✅

def ternary_list2() -> None:
    a = list[int]() if "" else list[str]()
    reveal_type(a)  # list[int] | list[str] ✅

def ternary_list_literal() -> None:
    a = [1] if "" else ["a"]  # E: List item 0 has incompatible type "str"
    reveal_type(a) # list[int] ???

I would have expected list[int] | list[str] in the literal case as well.

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