classification
Title: Subscript unpacking raises SyntaxError
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.7
process
Status: closed Resolution: duplicate
Dependencies: Superseder: Tuple unpacking in return and yield statements
View: 32117
Assigned To: Nosy List: Ben Burrill, Joshua.Landau, NeilGirdhar, serhiy.storchaka, terry.reedy
Priority: normal Keywords:

Created on 2018-01-22 22:39 by Ben Burrill, last changed 2018-05-09 09:54 by serhiy.storchaka. This issue is now closed.

Messages (5)
msg310447 - (view) Author: Ben Burrill (Ben Burrill) Date: 2018-01-22 22:39
PEP 448 defines unpacking generalizations for tuples.  However, this does not currently work for subscripted tuples that are not delimited by parentheses.

Current behavior (Tested on 3.6/3.7a4):
>>> class Subscriptable:
...     def __getitem__(self, item):
...         return item
...
>>> ss = Subscriptable()
>>> 
>>> 1, 2, 3
(1, 2, 3)
>>> ss[1, 2, 3]
(1, 2, 3)
>>> *range(5), 42
(0, 1, 2, 3, 4, 42)
>>> ss[*range(5), 42]  # This should be the same as above
  File "<stdin>", line 1
    ss[*range(5), 42]
       ^
SyntaxError: invalid syntax
>>> ss[(*range(5), 42)]  # Workaround
(0, 1, 2, 3, 4, 42)
msg310824 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2018-01-27 00:06
Tuples often need parentheses when embedded in expressions.  So often that beginners often think that they are required.  I believe this should be closed as 'not a bug'.  I nosied the two authors of the implementation for their opinion.
msg310825 - (view) Author: Ben Burrill (Ben Burrill) Date: 2018-01-27 00:11
Yeah, but in this case, you don't need parentheses unless you use unpacking.  That is unexpected behavior.
msg310886 - (view) Author: Neil Girdhar (NeilGirdhar) * Date: 2018-01-27 20:50
This came up already on python-ideas: https://groups.google.com/forum/#!topic/python-ideas/YOpT9fDQyFk

I think this was an oversight, and I'm with Ben that it's unexpected.  That said, this is usually the kind of thing that Guido likes to comment on.  My suggestion is to first check to see if you can change the grammar, and then ask Guido what he thinks.
msg316322 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-05-09 09:54
Issue32117 looks considering a more general question about iterable unpacking.
History
Date User Action Args
2018-05-09 09:54:55serhiy.storchakasetstatus: open -> closed

superseder: Tuple unpacking in return and yield statements

nosy: + serhiy.storchaka
messages: + msg316322
resolution: duplicate
stage: resolved
2018-01-27 20:50:30NeilGirdharsetmessages: + msg310886
2018-01-27 00:11:28Ben Burrillsetmessages: + msg310825
2018-01-27 00:06:55terry.reedysetnosy: + Joshua.Landau, terry.reedy, NeilGirdhar
messages: + msg310824
2018-01-22 22:39:59Ben Burrillcreate