Keep `Literal` after indexing?
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Python
- Estrellas
- 20.6k
- Forks
- 3.3k
- Métricas de merge de PR
- Métricas de PR pendientes
Descripción
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
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Línea de trabajo
Comienza reproduciendo el MWE del issue y compara los tipos inferidos para los slices usando un índice variable frente a un índice literal. El cambio está completo cuando el slice con índice variable conserva el tipo de elemento Literal de la tupla sin degradar el comportamiento existente con índices literales.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- python
- Área
- tooling
- Tipo de issue
- Nueva funcionalidad
- Dificultad
- 4/5
- Tiempo estimado
- 3-5 días
- Estado de actividad
- Estancado
- Claridad
- Bastante claro
- Aptitud para principiantes
- 45/100