Skip to content
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -5120,7 +5120,11 @@ def visit_yield_from_expr(self, e: YieldFromExpr, allow_none_return: bool = Fals

# Determine the type of the entire yield from expression.
iter_type = get_proper_type(iter_type)
if isinstance(iter_type, Instance) and iter_type.type.fullname == "typing.Generator":
if (
isinstance(iter_type, Instance)
and iter_type.type.fullname == "typing.Generator"
or isinstance(iter_type, UnionType)
Comment thread
omarsilva1 marked this conversation as resolved.
Outdated
):
expr_type = self.chk.get_generator_return_type(iter_type, False)
else:
# Non-Generators don't return anything from `yield from` expressions.
Expand Down
17 changes: 17 additions & 0 deletions test-data/unit/check-statements.test
Original file line number Diff line number Diff line change
Expand Up @@ -2195,6 +2195,23 @@ class B: pass
def foo(x: int) -> Union[Generator[A, None, None], Generator[B, None, None]]:
yield x # E: Incompatible types in "yield" (actual type "int", expected type "Union[A, B]")

[case testReturnTypeOfGeneratorUnion]
from typing import Generator, Union

class T: pass

def foo(arg: Union[Generator[int, None, T], Generator[str, None, T]]) -> Generator[Union[int, str], None, T]:
Comment thread
omarsilva1 marked this conversation as resolved.
return (yield from arg)

[case testYieldFromError]
from typing import Generator, Union

class T: pass
class R: pass

def foo(arg: Union[T, R]) -> Generator[Union[int, str], None, T]:
Comment thread
omarsilva1 marked this conversation as resolved.
return (yield from arg) # E: "yield from" cannot be applied to "Union[T, R]"

[case testNoCrashOnStarRightHandSide]
x = *(1, 2, 3) # E: Can use starred expression only as assignment target
[builtins fixtures/tuple.pyi]
Expand Down