Overly broad type inference when using zip(*x) to tranpose 2d iterable
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Python
- Estrellas
- 5.1k
- Forks
- 2.1k
- Merge medio
- 1 d 19 h
- PR fusionados (30 d)
- 82
Descripción
Description:
When transposing a 2d iterable x: Iterable[Iterable[T]] using zip(*x), I've noticed typeshed's type annotations result in the return type being inferred as zip[tuple[Any, ...]] rather than the expected zip[tuple[T, ...]]. It would be nice if builtins.py^1 had an overload for this use case.
Expected behavior
When x has type Iterable[Iterable[T]], the expression zip(*x) should be inferred as zip[tuple[T, ...]], preserving the element type information through the transpose operation.
Actual behavior
The expression zip(*x) is inferred as zip[tuple[Any, ...]].
Possible solution
Add the following overloads to zip:
Pre 3.10:
@overload
def __new__(
cls,
*iterables: Iterable[_T1],
) -> zip[tuple[_T1, ...]]: ...
3.10 or later:
@overload
def __new__(
cls,
*iterables: Iterable[_T1],
*,
strict: bool = ...
) -> zip[tuple[_T1, ...]]: ...
While I have seen zip(*x) in the wild, I have not seen zip(a,b,c,*x) in any environment before, so I don't think it's necessary to write an overload for that edge case.
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
Abre stdlib/builtins.pyi en la definición enlazada de zip y compara las sobrecargas existentes con las firmas propuestas para antes de 3.10 y para 3.10+. Confirma que zip(*x) para Iterable[Iterable[T]] se infiere como zip[tuple[T, ...]] en lugar de zip[tuple[Any, ...]], manteniendo al mismo tiempo el manejo estricto.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- python
- Área
- tooling
- Tipo de issue
- Error
- Dificultad
- 2/5
- Tiempo estimado
- 1-3 horas
- Estado de actividad
- Estancado
- Claridad
- Bien especificado
- Aptitud para principiantes
- 58/100