Skip to content

For loop with a Generator[Any, Any, NoReturn] doesn't cause unreachable code. #18891

Open
@MoonMountain2k

Description

@MoonMountain2k

Bug Report

A for loop iterating over a Generator[Any, Any, NoReturn] isn't assumed to continue indefinitely.

To Reproduce

https://mypy-play.net/?mypy=latest&python=3.12&gist=cd9b4f28655f7a5460983d6c9a520331

from typing import Generator, NoReturn

def generator() -> Generator[int, None, NoReturn]:
    while True:
        yield 1

def foo() -> int:
    for x in generator():
        return x
    # Code past this point should be unreachable.
    # But mypy expects a return statement here.

Expected Behavior

Any code following for loops that iterate over unending generators should be recognized as unreachable, and the execution shouldn't branch.

Actual Behavior

main.py:7: error: Missing return statement  [return]

Your Environment

  • Mypy version used: 1.15.0
  • 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