python / mypy

Optional static typing for Python
https://www.mypy-lang.org/
Other
17.85k stars 2.74k forks source link

Union can cause Iterator to require a return statement #17432

Open tvuotila opened 1 week ago

tvuotila commented 1 week ago

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