Wrong type inference with "yield from" from an Iterator class
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by running the provided buggy.py reproduction with mypy --strict and compare the yield from line with the following for loop. Trace the type inference for the Iterator[int] instance; done means mypy reports the same incompatible-yield error for both paths.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 42/100