python / python/mypy

Union can cause Iterator to require a return statement

Open
#17,432 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Bug Report

Iterator doesn't require a return statement except when used in an union.

To Reproduce

from typing import Iterator, Union

Type = Union[Iterator[int], int]

def foo() -> Type:
    yield 1

playground link

Actual Behavior

error: Missing return statement [return]

Expected behavior

I expected Mypy to not return any errors like

from typing import Iterator, Union

Type = Iterator[int]

def foo() -> Type:
    yield 1

Your Environment

  • Mypy version used: 1.10.0 (Works on 1.9.0)
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): none
  • 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

No source file or test is named. Start by running the provided Python reproduction with mypy 1.10.0 and comparing it with 1.9.0, then trace handling of generator functions annotated with a Union containing Iterator. Done means the reproduced case no longer reports a missing return statement while the existing Iterator behavior remains unchanged.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.