Skip to content

Keep Literal after indexing? #19152

Open
Open
@ego-thales

Description

@ego-thales

EDIT: MWE

from typing import Literal

foo = 0
bar: tuple[Literal[1], ...] = (1,)[foo:]  # error: Incompatible types in assignment (expression has type "tuple[int, ...]", variable has type "tuple[Literal[1], ...]")  [assignment]
baz: tuple[Literal[1], ...] = (1,)[0:]    # OK

bar_fix: tuple[Literal[1], ...] = (1,)    # In two steps...
bar_fix = bar_fix[foo:]                   # ...works fine

Original message

Hi there,

As always, I'm not sure whether I should post on mypy or python typing page. The problem I encounter is the following.

aligns: tuple[Literal["center", "right"], ...] = ("center", "right", "center")[no_extra:]
error: Incompatible types in assignment (expression has type "tuple[str, ...]", variable has type "tuple[Literal['center', 'right'], ...]")  [assignment]
            aligns: tuple[Literal["center", "right"], ...] = ("center", "right", "center")[no_extra:]

where no_extra is a bool. Essentially, I either keep or not the first element with my_tuple[no_extra:], and this makes it into a tuple[str, ...].

Is it normal? If so, is there a recommended practice around it, or should this be a new feature?

Thanks in advance!

All the best.
Élie

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions