PEP 637 allow for unpacking of starargs (#1622)

This commit is contained in:
Stefano Borini 2020-09-27 00:37:32 +01:00 committed by GitHub
parent 6ca7702c94
commit fd40249e4e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 27 additions and 10 deletions

View File

@ -270,7 +270,7 @@ The successful implementation of this PEP will result in the following behavior:
This remains the case even if the index is followed by keywords; see point 5 below. This remains the case even if the index is followed by keywords; see point 5 below.
3. Comma-seperated arguments are still parsed as a tuple and passed as 3. Comma-separated arguments are still parsed as a tuple and passed as
a single positional argument:: a single positional argument::
obj[spam, eggs] obj[spam, eggs]
@ -347,17 +347,26 @@ The successful implementation of this PEP will result in the following behavior:
- but if no ``**kwargs`` parameter is defined, it is an error. - but if no ``**kwargs`` parameter is defined, it is an error.
7. Sequence unpacking remains a syntax error inside subscripts:: 7. Sequence unpacking is allowed inside subscripts::
obj[*items] obj[*items]
Reason: unpacking items would result it being immediately repacked into This allows notations such as ``[:, *args, :]``, which could be treated
a tuple. Anyone using sequence unpacking in the subscript is probably as ``[(slice(None), *args, slice(None))]``.
confused as to what is happening, and it is best if they receive an
immediate syntax error with an informative error message.
This restriction has however been considered arbitrary by some, and it might The following notation equivalence should be honored::
be lifted at a later stage for symmetry with kwargs unpacking, see next.
obj[*()]
# Error. Equivalent to obj[]
obj[*(), foo=3]
# Equivalent to obj[foo=3]
obj[*(x,)]
# Equivalent to obj[x]
obj[*(x,),]
# Equivalent to obj[x,]
8. Dict unpacking is permitted:: 8. Dict unpacking is permitted::
@ -365,6 +374,14 @@ The successful implementation of this PEP will result in the following behavior:
obj[index, **items] obj[index, **items]
# equivalent to obj[index, spam=1, eggs=2] # equivalent to obj[index, spam=1, eggs=2]
The following notation equivalent should be honored::
obj[**{}]
# Error. Equivalent to obj[]
obj[3, **{}]
# Equivalent to obj[3]
9. Keyword-only subscripts are permitted. The positional index will be the empty tuple:: 9. Keyword-only subscripts are permitted. The positional index will be the empty tuple::
obj[spam=1, eggs=2] obj[spam=1, eggs=2]
@ -779,7 +796,7 @@ more than this::
obj[1, k=3] # __getitem__(1, k=3). Integer obj[1, k=3] # __getitem__(1, k=3). Integer
obj[1, 2, k=3] # __getitem__((1, 2), k=3). Tuple obj[1, 2, k=3] # __getitem__((1, 2), k=3). Tuple
With the first more in line with a \*args semantics for calling a routine with With the first more in line with a ``*args`` semantics for calling a routine with
no positional arguments:: no positional arguments::
>>> def foo(*args, **kwargs): >>> def foo(*args, **kwargs):