python / mypy

Optional static typing for Python
https://www.mypy-lang.org/
Other
17.85k stars 2.74k forks source link

Wrong type inference with "yield from" from an Iterator class #17449

Open arnimarj opened 3 days ago

arnimarj commented 3 days ago

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

arnimarj commented 2 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)