bpo-32117: Allow tuple unpacking in return and yield statements by dacut · Pull Request #4509 · python/cpython · GitHub
Skip to content
Merged
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
4 changes: 2 additions & 2 deletions Grammar/Grammar
11 changes: 10 additions & 1 deletion Lib/test/test_grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -824,11 +824,17 @@ def test_inner(extra_burning_oil = 1, count=0):
test_inner()

def test_return(self):
# 'return' [testlist]
# 'return' [testlist_star_expr]
def g1(): return
def g2(): return 1
def g3():
z = [2, 3]
return 1, *z

g1()
x = g2()
y = g3()
self.assertEqual(y, (1, 2, 3), "unparenthesized star expr return")
check_syntax_error(self, "class foo:return 1")

def test_break_in_finally(self):
Expand Down Expand Up @@ -981,6 +987,9 @@ def g(): f((yield 1))
def g(): f((yield 1), 1)
def g(): f((yield from ()))
def g(): f((yield from ()), 1)
# Do not require parenthesis for tuple unpacking
def g(): rest = 4, 5, 6; yield 1, 2, 3, *rest

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would add another test here to validate that this works, e.g. self.assertEqual(list(g()), [(1, 2, 3, 4, 5, 6)]).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was tempted to do self.assertEqual(*g(), (1, 2, 3, 4, 5, 6)), but I think we're testing enough unpacking

self.assertEquals(list(g()), [(1, 2, 3, 4, 5, 6)])
check_syntax_error(self, "def g(): f(yield 1)")
check_syntax_error(self, "def g(): f(yield 1, 1)")
check_syntax_error(self, "def g(): f(yield from ())")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Iterable unpacking is now allowed without parenthesis in yield and return
statements, e.g. ``yield 1, 2, 3, *rest``. Thanks to David Cuthbert for the
change and jChapman for added tests.
6 changes: 3 additions & 3 deletions Python/graminit.c