Keep `Literal` after indexing?
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 20.6k
- Fork
- 3.3k
- Metriche di merge delle PR
- Metriche PR in attesa
Descrizione
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
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia riproducendo l'MWE nell'issue e confronta i tipi inferiti per gli slice usando un indice variabile rispetto a un indice letterale. La modifica è completa quando lo slice con indice variabile preserva il tipo di elemento Literal della tupla senza introdurre regressioni nel comportamento esistente con indice letterale.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- tooling
- Tipo di issue
- Funzionalità
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 45/100