mypy icon indicating copy to clipboard operation
mypy copied to clipboard

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

Open arnimarj opened this issue 1 year ago • 4 comments

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 avatar Jun 29 '24 17:06 arnimarj

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)

arnimarj avatar Jun 30 '24 23:06 arnimarj

I will try to fix it

gege-hoho avatar Jul 13 '24 13:07 gege-hoho

It seems, the error lays in the get_generator_yield_type function which is not able to get the yielding type for such a constellation accuratley and assumes Any

gege-hoho avatar Jul 16 '24 18:07 gege-hoho

Hello, I am interested in this issue, can I work on it?

ChengPuPu avatar Oct 13 '24 02:10 ChengPuPu

I will look at that during PyCon US 2025 sprints.

east825 avatar May 19 '25 19:05 east825