Skip to content

Commit fc9fa61

Browse files
gvanrossumilevkivskyi
authored andcommitted
Don't supply a context to the expression in yield from (#3815)
The actual expected type is complicated; it should be either a Generator[X, Y, Z] sharing X and Y with the containing generator function, or an Iterable[X]. Fixes #3808.
1 parent 69af980 commit fc9fa61

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

mypy/checkexpr.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2361,7 +2361,11 @@ def visit_yield_from_expr(self, e: YieldFromExpr, allow_none_return: bool = Fals
23612361
# thus decorated. But it accepts a generator regardless of
23622362
# how it's decorated.
23632363
return_type = self.chk.return_types[-1]
2364-
subexpr_type = self.accept(e.expr, return_type)
2364+
# TODO: What should the context for the sub-expression be?
2365+
# If the containing function has type Generator[X, Y, ...],
2366+
# the context should be Generator[X, Y, T], where T is the
2367+
# context of the 'yield from' itself (but it isn't known).
2368+
subexpr_type = self.accept(e.expr)
23652369
iter_type = None # type: Type
23662370

23672371
# Check that the expr is an instance of Iterable and get the type of the iterator produced

test-data/unit/check-statements.test

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,9 +1136,8 @@ def f() -> Iterator[None]:
11361136

11371137
-- Yield from statement
11381138
-- --------------------
1139-
1140-
-- Iterables
1141-
-- ----------
1139+
--
1140+
-- (It's not really a statement, but don't want to move the tests.)
11421141

11431142
[case testSimpleYieldFromWithIterator]
11441143
from typing import Iterator
@@ -1216,6 +1215,15 @@ def f(a):
12161215
return b
12171216
[out]
12181217

1218+
[case testYieldFromGenericCall]
1219+
from typing import Generator, TypeVar
1220+
T = TypeVar('T')
1221+
def f(a: T) -> Generator[int, str, T]: pass
1222+
def g() -> Generator[int, str, float]:
1223+
r = yield from f('')
1224+
reveal_type(r) # E: Revealed type is 'builtins.str*'
1225+
return 3.14
1226+
12191227
-- With statement
12201228
-- --------------
12211229

0 commit comments

Comments
 (0)