python / python/mypy

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

Open
#19,355 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-reachability
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Bug Report

Split off from https://github.com/python/mypy/issues/7374#issuecomment-944883194

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the example in mypy-play using the reported mypy 1.16.1 environment, focusing on control-flow handling after yield from of a NoReturn generator. Trace the relevant generator and return-type analysis entry points; done means the example produces no incompatible return-value error while other return checking remains intact.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.