Skip to content

Commit 6d219a6

Browse files
author
Guido van Rossum
committed
Disallow yield [from] in async def.
1 parent 69565e9 commit 6d219a6

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

mypy/semanal.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1821,7 +1821,10 @@ def visit_yield_from_expr(self, e: YieldFromExpr) -> None:
18211821
if not self.is_func_scope(): # not sure
18221822
self.fail("'yield from' outside function", e, True, blocker=True)
18231823
else:
1824-
self.function_stack[-1].is_generator = True
1824+
if self.function_stack[-1].is_coroutine:
1825+
self.fail("'yield from' in async function", e, True, blocker=True)
1826+
else:
1827+
self.function_stack[-1].is_generator = True
18251828
if e.expr:
18261829
e.expr.accept(self)
18271830

@@ -2074,7 +2077,10 @@ def visit_yield_expr(self, expr: YieldExpr) -> None:
20742077
if not self.is_func_scope():
20752078
self.fail("'yield' outside function", expr, True, blocker=True)
20762079
else:
2077-
self.function_stack[-1].is_generator = True
2080+
if self.function_stack[-1].is_coroutine:
2081+
self.fail("'yield' in async function", expr, True, blocker=True)
2082+
else:
2083+
self.function_stack[-1].is_generator = True
20782084
if expr.expr:
20792085
expr.expr.accept(self)
20802086

test-data/unit/check-async-await.test

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,33 @@ async def f() -> None:
7979
[builtins fixtures/async_await.py]
8080
[out]
8181
main: note: In function "f":
82+
83+
[case testNoYieldInAsyncDef]
84+
# options: fast_parser
85+
async def f():
86+
yield None
87+
async def g():
88+
yield
89+
async def h():
90+
x = yield
91+
[builtins fixtures/async_await.py]
92+
[out]
93+
main: note: In function "f":
94+
main:3: error: 'yield' in async function
95+
main: note: In function "g":
96+
main:5: error: 'yield' in async function
97+
main: note: In function "h":
98+
main:7: error: 'yield' in async function
99+
100+
[case testNoYieldFromInAsyncDef]
101+
# options: fast_parser
102+
async def f():
103+
yield from []
104+
async def g():
105+
x = yield from []
106+
[builtins fixtures/async_await.py]
107+
[out]
108+
main: note: In function "f":
109+
main:3: error: 'yield from' in async function
110+
main: note: In function "g":
111+
main:5: error: 'yield from' in async function

test-data/unit/fixtures/async_await.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ class type: pass
55
class function: pass
66
class int: pass
77
class str: pass
8+
class list: pass

0 commit comments

Comments
 (0)