[New-bugs-announce] [issue34508] return of non-parenthesized star-unpacking expression a SyntaxError

Mark Dickinson report at bugs.python.org
Sun Aug 26 10:25:50 EDT 2018

New submission from Mark Dickinson <dickinsm at gmail.com>:

[From https://stackoverflow.com/q/52026406/270986]

The following is valid, and works as expected:

>>> def f():
...     x = *(1, 2), 3
...     return x
>>> f()
(1, 2, 3)

But the tuple expression can't be used directly in a "return" statement:

>>> def f():
...     return *(1, 2), 3
  File "<stdin>", line 2
    return *(1, 2), 3
SyntaxError: invalid syntax

It's trivial to work around, by adding an extra pair of parentheses around the return target, but it seems a surprising inconsistency. Would it make sense to allow this? In terms of the grammar,

    return_stmt: 'return' [testlist]

would be replaced with:

    return_stmt: 'return' [testlist_star_expr]

There may be other places in the grammar where "testlist" could reasonably be replaced with "testlist_star_expr", for example:

    for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite]

components: Interpreter Core
messages: 324118
nosy: mark.dickinson
priority: normal
severity: normal
status: open
title: return of non-parenthesized star-unpacking expression a SyntaxError
type: enhancement
versions: Python 3.8

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list