Skip to content

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

Open
@arnimarj

Description

@arnimarj

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions