Keep `Literal` after indexing?
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
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
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
Start by reproducing the MWE in the issue and compare the inferred types for slices using a variable index versus a literal index. The change is complete when the variable-index slice preserves the tuple's Literal element type without regressing the existing literal-index behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100