Wrong type inference with "yield from" from an Iterator class
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
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
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 eseguendo la riproduzione fornita in buggy.py con mypy --strict e confronta la riga yield from con il seguente ciclo for. Traccia l’inferenza del tipo per l’istanza Iterator[int]; il lavoro è completato quando mypy segnala lo stesso errore di yield incompatibile per entrambi i percorsi.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- devtools
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Specificata chiaramente
- Idoneità per principianti
- 42/100