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: Tuple unpacking in getitem
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.10
process
Status: closed Resolution: duplicate
Dependencies: Superseder:
Assigned To: Nosy List: dakinjeppe, pablogsal
Priority: normal Keywords:

Created on 2021-04-12 11:35 by dakinjeppe, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (3)
msg390838 - (view) Author: Jeppe Dakin (dakinjeppe) Date: 2021-04-12 11:35
This is similar to issue 32117 (I'm the author of the StackOverflow query referenced within it), which extends the tuple unpacking syntax to a small corner of the language: https://bugs.python.org/issue32117

Currently tuple unpacking is disallowed within getitem (brackets):
d = {(1, 2, 3): 42}
t = (2, 3)
d[1, *t]  # SyntaxError

To make it work, explicit parenthesization is needed:
d[(1, *t)]  # OK


On top of being slightly more neat, the un-parenthesized version is more in harmony with the language generally:

1) Parentheses are usually only required in order to distinguish between several possible outcomes (grouping). This is not the case here, as leaving out the parentheses doesn't produce any outcome at all.

2) Sub-expressions can usually be extracted as a variable. That is, one would expect the illegal code
d[1, *t]  # SyntaxError
to be equivalent to the legal code
sub = 1, *t
d[sub]  # OK

3) The following are both allowed and equivalent:
d[1, 2, 3]
d[(1, 2, 3)]
where both gets transformed into d.__getitem__((1, 2, 3)), implying that implicit tuple packing occurs in the first case. Having this tuple packing feature without allowing for the * unpacking operator feels incomplete.

4) What the syntax d[1, *t] is supposed to mean is obvious, enough so that I was surprised to find that it was disallowed. Though hardly a stumbling block, I suspect many have written such code, gotten the SyntaxError and remedied the code by putting in the (apparently non-superfluous) parentheses.


I propose allowing such tuple unpacking within getitem [], which I believe amounts to a slight adjustment of the grammar. This should not break any existing code as it merely extends the space of allowed syntax.
msg390886 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2021-04-12 19:13
This is currently being proposed as part of https://www.python.org/dev/peps/pep-0646.
msg390887 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2021-04-12 19:15
I am closing this as duplicate of https://www.python.org/dev/peps/pep-0646, but feel free to reopen if you think there is some missing considerations.
History
Date User Action Args
2022-04-11 14:59:44adminsetgithub: 87978
2021-10-24 02:45:48Dennis Sweeneylinkissue45586 superseder
2021-04-12 19:15:14pablogsalsetstatus: open -> closed
resolution: duplicate
messages: + msg390887

stage: resolved
2021-04-12 19:13:45pablogsalsetnosy: + pablogsal
messages: + msg390886
2021-04-12 11:35:12dakinjeppecreate