Variadic tuple unpacking requires a star target -- why?
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
Bug Report
When attempting to unpack a tuple that uses Unpack, if you don't use a star target to capture the variadic tuple as a list, you get the error Variadic tuple unpacking requires a star target.
This is overly strict and prescriptivist and is not even applied consistently between similar type concepts within mypy.
- You definitely can do this if the lengths check out.
pyrightdoesn't raise an error like this. You are simply on your own for length checking the unpacking of indeterminate lengthtuples, just like you are forlist, another indeterminate length unpackable sequence.- In fact, this is also how
mypytreats unpackinglist. It doesn't raise an error if you don't use a star target. - I have a real use case where I want to do exactly this. I lay out my use case in this bug report to
pyright: https://github.com/microsoft/pyright/issues/7987
To Reproduce
from typing import Tuple
from typing_extensions import Unpack
def foo() -> Tuple[int, Unpack[Tuple[str, ...]]]:
return 1, "bar", "baz"
a, b, c = foo() # error: Variadic tuple unpacking requires a star target [misc]
d, e, f = [1, "bar", "baz"] # OK
Gist URL: https://gist.github.com/mypy-play/4014976ca069e54b5c1fe86a45353f58
Playground URL: https://mypy-play.net/?mypy=master&python=3.12&gist=4014976ca069e54b5c1fe86a45353f58
Expected Behavior
No errors in the above example. a's type should be revealed to be int, and b and c's types should be revealed to be str.
Actual Behavior
An error, and a, b, and c are all revealed to be Any.
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
Empieza con la reproducción mínima del informe o con su URL de mypy-play, y compara el diagnóstico y los tipos revelados para el desempaquetado de tuplas y listas. Se considera terminado cuando la asignación de la tupla no produce ningún error, muestra a como int y b y c como str, y no causa una regresión en el caso de la lista.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- python
- Área
- compilers
- Tipo de issue
- Error
- Dificultad
- 4/5
- Tiempo estimado
- 3-5 días
- Estado de actividad
- Estancado
- Claridad
- Bastante claro
- Aptitud para principiantes
- 42/100