Union can cause Iterator to require a return statement
Open
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
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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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