This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: *() Invalid Syntax: iterable unpacking of empty tuple
Type: Stage: resolved
Components: Versions: Python 3.9
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: serhiy.storchaka, spralja
Priority: normal Keywords:

Created on 2022-02-26 09:04 by spralja, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (4)
msg414085 - (view) Author: Robert Spralja (spralja) Date: 2022-02-26 09:04
`
>>> def foo(num=1):
...     return num
...
>>> foo(*(bool,) is bool else *())
  File "<stdin>", line 1
    foo(*(bool,) is bool else *())
                         ^
SyntaxError: invalid syntax
>>> foo(*(bool,) if bool else *())
  File "<stdin>", line 1
    foo(*(bool,) if bool else *())
                              ^
SyntaxError: invalid syntax
>>> def foo(num=1):
...     return num
...
>>> stri = ''
>>> foo(*(stri,) if stri else *())
  File "<stdin>", line 1
    foo(*(stri,) if stri else *())
                              ^
SyntaxError: invalid syntax
>>> foo(*((stri,) if stri else ()))
1
>>>
`

Iterable unpacking of empty tuple seems to not work in one example but does in another.
msg414086 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2022-02-26 09:07
It is an invalid syntax. Write foo(*((stri,) if stri else ())).
msg414087 - (view) Author: Robert Spralja (spralja) Date: 2022-02-26 09:09
I understand that it's invalid synatax, but not why.

On Sat, 26 Feb 2022 at 10:07, Serhiy Storchaka <report@bugs.python.org>
wrote:

>
> Serhiy Storchaka <storchaka+cpython@gmail.com> added the comment:
>
> It is an invalid syntax. Write foo(*((stri,) if stri else ())).
>
> ----------
> nosy: +serhiy.storchaka
> resolution:  -> not a bug
> stage:  -> resolved
> status: open -> closed
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <https://bugs.python.org/issue46865>
> _______________________________________
>
msg414088 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2022-02-26 10:09
See the grammar. https://docs.python.org/3/reference/expressions.html#conditional-expressions


conditional_expression ::=  or_test ["if" or_test "else" expression]
expression             ::=  conditional_expression | lambda_expr

`*()` is not an expression. It is either a starred_item (which can be a part of starred_list or a starred_expression), or a positional_item or a starred_and_keywords which are parts of an argument_list. None of them are expressions.

`foo(*(stri,) if stri else x)` is interpreted as `foo(*((stri,) if stri else x))` if x is an expression. But `*()` is not an expression, thus a syntax error.
History
Date User Action Args
2022-04-11 14:59:56adminsetgithub: 91021
2022-02-26 10:09:17serhiy.storchakasetmessages: + msg414088
2022-02-26 09:09:46spraljasetmessages: + msg414087
2022-02-26 09:07:18serhiy.storchakasetstatus: open -> closed

nosy: + serhiy.storchaka
messages: + msg414086

resolution: not a bug
stage: resolved
2022-02-26 09:04:32spraljacreate