Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
8 changes: 7 additions & 1 deletion mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -6273,7 +6273,13 @@ def visit_yield_from_expr(self, e: YieldFromExpr, allow_none_return: bool = Fals
# Check that the iterator's item type matches the type yielded by the Generator function
# containing this `yield from` expression.
expected_item_type = self.chk.get_generator_yield_type(return_type, False)
actual_item_type = self.chk.get_generator_yield_type(iter_type, False)

iter_type_proper = get_proper_type(iter_type)

if isinstance(iter_type_proper, Instance) and iter_type_proper.args:
actual_item_type = iter_type_proper.args[0]
else:
actual_item_type = self.chk.get_generator_yield_type(iter_type, False)

self.chk.check_subtype(
actual_item_type,
Expand Down
11 changes: 11 additions & 0 deletions test-data/unit/check-functions.test
Original file line number Diff line number Diff line change
Expand Up @@ -3768,3 +3768,14 @@ class C:

def defer() -> int:
return 1


[case testYieldFromIteratorMismatch]
from typing import Generator, Iterator

class A(Iterator[int]):
def __next__(self) -> int: ...
def __iter__(self) -> "A": ...

def f() -> Generator[tuple[int, ...], None, None]:
yield from A() # E: Incompatible types in "yield"
Loading