Skip to content

Code after yield from a generator that cannot end should be unreachable #19355

Open
@A5rocks

Description

@A5rocks

Bug Report

Split off from #7374 (comment)

To Reproduce

from typing import Generator, NoReturn, Union

def inf() -> Generator[str, None, NoReturn]:
    while True:
        yield "xyz"


def generate(x: Union[str, int]) -> Generator[str, None, int]:
    if isinstance(x, str):
        yield from inf()  # `yield from` will never return, because the generator never returns 

    return x  # error: Incompatible return value type (got "Union[str, int]", expected "int")

Expected Behavior

No error

Actual Behavior

An error.

Your Environment

Reproduced in mypy-play.

  • Mypy version used: 1.16.1
  • Mypy command-line flags: N/A
  • Mypy configuration options from mypy.ini (and other config files): N/A
  • Python version used: 3.12

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-reachabilityDetecting unreachable code

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions