Skip to content

Literal list ternary behave unexpected #19534

Description

@randolf-scholz

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

defternary_list[T, S](x: list[T], y: list[S]) ->None:
a=xif""elseyreveal_type(a) # list[T] | list[S] ✅defternary_list2() ->None:
a=list[int]() if""elselist[str]()
reveal_type(a) # list[int] | list[str] ✅defternary_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