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

@ -184,7 +184,7 @@ have anonymous argument semantic, exactly like the indexing operation. However,
print(index)
The index operation basically forwards the content of the square brackets "as is"
in the ``index`` argument::
in the ``index`` argument::
>>> x=X()
>>> x[0]
@ -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.
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::
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.
7. Sequence unpacking remains a syntax error inside subscripts::
7. Sequence unpacking is allowed inside subscripts::
obj[*items]
Reason: unpacking items would result it being immediately repacked into
a tuple. Anyone using sequence unpacking in the subscript is probably
confused as to what is happening, and it is best if they receive an
immediate syntax error with an informative error message.
This allows notations such as ``[:, *args, :]``, which could be treated
as ``[(slice(None), *args, slice(None))]``.
This restriction has however been considered arbitrary by some, and it might
be lifted at a later stage for symmetry with kwargs unpacking, see next.
The following notation equivalence should be honored::
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::
@ -365,6 +374,14 @@ The successful implementation of this PEP will result in the following behavior:
obj[index, **items]
# 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::
obj[spam=1, eggs=2]
@ -779,7 +796,7 @@ more than this::
obj[1, k=3] # __getitem__(1, k=3). Integer
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::
>>> def foo(*args, **kwargs):