Skip to content

Commit fd97d1f

Browse files
dacutgvanrossum
authored andcommitted
bpo-32117: Allow tuple unpacking in return and yield statements (pythongh-4509)
Iterable unpacking is now allowed without parentheses in yield and return statements, e.g. ``yield 1, 2, 3, *rest``. Thanks to David Cuthbert for the change and jChapman for added tests.
1 parent 7279b51 commit fd97d1f

File tree

4 files changed

+18
-6
lines changed

4 files changed

+18
-6
lines changed

Grammar/Grammar

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ pass_stmt: 'pass'
5050
flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt | yield_stmt
5151
break_stmt: 'break'
5252
continue_stmt: 'continue'
53-
return_stmt: 'return' [testlist]
53+
return_stmt: 'return' [testlist_star_expr]
5454
yield_stmt: yield_expr
5555
raise_stmt: 'raise' [test ['from' test]]
5656
import_stmt: import_name | import_from
@@ -147,4 +147,4 @@ comp_if: 'if' test_nocond [comp_iter]
147147
encoding_decl: NAME
148148

149149
yield_expr: 'yield' [yield_arg]
150-
yield_arg: 'from' test | testlist
150+
yield_arg: 'from' test | testlist_star_expr

Lib/test/test_grammar.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -824,11 +824,17 @@ def test_inner(extra_burning_oil = 1, count=0):
824824
test_inner()
825825

826826
def test_return(self):
827-
# 'return' [testlist]
827+
# 'return' [testlist_star_expr]
828828
def g1(): return
829829
def g2(): return 1
830+
def g3():
831+
z = [2, 3]
832+
return 1, *z
833+
830834
g1()
831835
x = g2()
836+
y = g3()
837+
self.assertEqual(y, (1, 2, 3), "unparenthesized star expr return")
832838
check_syntax_error(self, "class foo:return 1")
833839

834840
def test_break_in_finally(self):
@@ -981,6 +987,9 @@ def g(): f((yield 1))
981987
def g(): f((yield 1), 1)
982988
def g(): f((yield from ()))
983989
def g(): f((yield from ()), 1)
990+
# Do not require parenthesis for tuple unpacking
991+
def g(): rest = 4, 5, 6; yield 1, 2, 3, *rest
992+
self.assertEquals(list(g()), [(1, 2, 3, 4, 5, 6)])
984993
check_syntax_error(self, "def g(): f(yield 1)")
985994
check_syntax_error(self, "def g(): f(yield 1, 1)")
986995
check_syntax_error(self, "def g(): f(yield from ())")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Iterable unpacking is now allowed without parenthesis in yield and return
2+
statements, e.g. ``yield 1, 2, 3, *rest``. Thanks to David Cuthbert for the
3+
change and jChapman for added tests.

Python/graminit.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,7 @@ static arc arcs_25_0[1] = {
613613
{75, 1},
614614
};
615615
static arc arcs_25_1[2] = {
616-
{9, 2},
616+
{47, 2},
617617
{0, 1},
618618
};
619619
static arc arcs_25_2[1] = {
@@ -1900,7 +1900,7 @@ static state states_85[3] = {
19001900
};
19011901
static arc arcs_86_0[2] = {
19021902
{77, 1},
1903-
{9, 2},
1903+
{47, 2},
19041904
};
19051905
static arc arcs_86_1[1] = {
19061906
{26, 2},
@@ -2087,7 +2087,7 @@ static dfa dfas[87] = {
20872087
{341, "yield_expr", 0, 3, states_85,
20882088
"\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\200\000"},
20892089
{342, "yield_arg", 0, 3, states_86,
2090-
"\000\040\200\000\000\000\000\000\000\040\010\000\000\000\020\002\000\300\220\050\037\000\000"},
2090+
"\000\040\200\000\002\000\000\000\000\040\010\000\000\000\020\002\000\300\220\050\037\000\000"},
20912091
};
20922092
static label labels[177] = {
20932093
{0, "EMPTY"},

0 commit comments

Comments
 (0)