Open arnimarj opened 3 days ago
As a reference point, pyright
does warn about this (output taken from a reduce version of the above):
(tools_venv3) ➜ m python3 -m pyright buggy.py
/home/arni/m/buggy.py
/home/arni/m/buggy.py:16:16 - error: Return type of generator function must be compatible with "Generator[int, Any, Any]"
"Generator[int, Unknown, Unknown]" is incompatible with "Generator[tuple[int, ...], None, None]"
Type parameter "_YieldT_co@Generator" is covariant, but "int" is not a subtype of "tuple[int, ...]"
"int" is incompatible with "tuple[int, ...]" (reportReturnType)
1 error, 0 warnings, 0 informations
The reduced example:
from typing import Generator, Iterator, Self
class goes_to_11(Iterator[int]):
def __init__(self) -> None:
self.elevens = iter((11,))
def __next__(self) -> int:
return next(self.elevens)
def __iter__(self) -> Self:
return self
def bunch_of_11() -> Generator[tuple[int, ...]]:
yield from goes_to_11()
for value in bunch_of_11():
print(type(value), value)
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
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
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