Wrong type inference with "yield from" from an Iterator class
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Python
- Estrellas
- 20.6k
- Forks
- 3.3k
- Merge medio
- 1 d 18 h
- PR fusionados (30 d)
- 54
Descripción
Bug Report
When using "yield from" for an iterable instance the type inference if wrong. Normal for-loop iteration over the same iterable does work though.
To Reproduce
from typing import Iterator, Self
class goes_to_11(Iterator[int]):
def __init__(self):
self.elevens = iter((11,))
def __next__(self) -> int:
return next(self.elevens)
def __iter__(self) -> Self:
return self
def bunch_of_11() -> Iterator[tuple[int, ...]]:
yield from goes_to_11()
for integer in goes_to_11():
yield integer
for value in bunch_of_11():
print(type(value), value)
Expected Behavior
Mypy should complain about both the "yield from" line and the "yield integer" line, but for the former it doesn't.
Actual Behavior
mypy --strict buggy.py
buggy.py:19: error: Incompatible types in "yield" (actual type "int", expected type "tuple[int, ...]") [misc]
Found 1 error in 1 file (checked 1 source file)
Your Environment
Tested on Linux Python 3.12.3 using mypy wheel mypy-1.11.0+dev.b88fdbd32fe0a45d40531a6504317aa3fd48489e-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
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 ejecutando la reproducción proporcionada en buggy.py con mypy --strict y compara la línea yield from con el siguiente bucle for. Sigue la inferencia de tipos de la instancia Iterator[int]; se considera terminado cuando mypy informa del mismo error de yield incompatible para ambas rutas.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- python
- Área
- devtools
- Tipo de issue
- Error
- Dificultad
- 4/5
- Tiempo estimado
- 3-5 días
- Estado de actividad
- Estancado
- Claridad
- Bien especificado
- Aptitud para principiantes
- 42/100