Skip to content

Commit 178e44d

Browse files
gege-hohoblurb-it[bot]picnixz
authored
gh-121657: Display correct error message for yield from outside of a function (GH-121680)
Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com> Co-authored-by: Bénédikt Tran <[email protected]>
1 parent f4d6e45 commit 178e44d

File tree

3 files changed

+8
-1
lines changed

3 files changed

+8
-1
lines changed

Lib/test/test_generators.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2247,6 +2247,11 @@ def printsolution(self, x):
22472247
...
22482248
SyntaxError: 'yield' outside function
22492249
2250+
>>> yield from [1,2]
2251+
Traceback (most recent call last):
2252+
...
2253+
SyntaxError: 'yield from' outside function
2254+
22502255
>>> def f(): x = yield = y
22512256
Traceback (most recent call last):
22522257
...
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Improve the :exc:`SyntaxError` message if the user tries to use
2+
:keyword:`yield from <yield>` outside a function.

Python/compile.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6119,7 +6119,7 @@ compiler_visit_expr(struct compiler *c, expr_ty e)
61196119
break;
61206120
case YieldFrom_kind:
61216121
if (!_PyST_IsFunctionLike(SYMTABLE_ENTRY(c))) {
6122-
return compiler_error(c, loc, "'yield' outside function");
6122+
return compiler_error(c, loc, "'yield from' outside function");
61236123
}
61246124
if (c->u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION) {
61256125
return compiler_error(c, loc, "'yield from' inside async function");

0 commit comments

Comments
 (0)