From fd40249e4edbf1200826675ceb644037f54f40f0 Mon Sep 17 00:00:00 2001 From: Stefano Borini Date: Sun, 27 Sep 2020 00:37:32 +0100 Subject: [PATCH] PEP 637 allow for unpacking of starargs (#1622) --- pep-0637.rst | 37 +++++++++++++++++++++++++++---------- 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/pep-0637.rst b/pep-0637.rst index c9e3240a3..3e40a9f81 100644 --- a/pep-0637.rst +++ b/pep-0637.rst @@ -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):