From a44de6d539863bb30d535a12db4d994588001ede Mon Sep 17 00:00:00 2001 From: Matthew Rahtz Date: Sun, 9 Jun 2024 18:05:35 +0100 Subject: [PATCH 1/9] Update language reference for PEP 646 --- Doc/reference/compound_stmts.rst | 10 ++++++++-- Doc/reference/expressions.rst | 15 +++++++++++---- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/Doc/reference/compound_stmts.rst b/Doc/reference/compound_stmts.rst index 8181b9759517f6..5eb633d004b4b8 100644 --- a/Doc/reference/compound_stmts.rst +++ b/Doc/reference/compound_stmts.rst @@ -1217,9 +1217,10 @@ A function definition defines a user-defined function object (see section : | `parameter_list_no_posonly` parameter_list_no_posonly: `defparameter` ("," `defparameter`)* ["," [`parameter_list_starargs`]] : | `parameter_list_starargs` - parameter_list_starargs: "*" [`parameter`] ("," `defparameter`)* ["," ["**" `parameter` [","]]] + parameter_list_starargs: "*" [`star_parameter`] ("," `defparameter`)* ["," ["**" `parameter` [","]]] : | "**" `parameter` [","] parameter: `identifier` [":" `expression`] + star_parameter: `identifier` [":" ["*"] `expression`] defparameter: `parameter` ["=" `expression`] funcname: `identifier` @@ -1326,7 +1327,8 @@ and may only be passed by positional arguments. Parameters may have an :term:`annotation ` of the form "``: expression``" following the parameter name. Any parameter may have an annotation, even those of the form -``*identifier`` or ``**identifier``. Functions may have "return" annotation of +``*identifier`` or ``**identifier``. (As a special case, parameters of the form +``*identifier`` may have an annotation "``: *expression``".) Functions may have "return" annotation of the form "``-> expression``" after the parameter list. These annotations can be any valid Python expression. The presence of annotations does not change the semantics of a function. The annotation values are available as values of @@ -1337,6 +1339,10 @@ enables postponed evaluation. Otherwise, they are evaluated when the function definition is executed. In this case annotations may be evaluated in a different order than they appear in the source code. +.. versionchanged:: 3.11 + Parameters of the form "``*identifier``" may have an annotation + "``: *expression``". See :pep:`646` for details. + .. index:: pair: lambda; expression It is also possible to create anonymous functions (functions not bound to a diff --git a/Doc/reference/expressions.rst b/Doc/reference/expressions.rst index 872773f4d28235..139a0a4fb01c85 100644 --- a/Doc/reference/expressions.rst +++ b/Doc/reference/expressions.rst @@ -873,9 +873,13 @@ primary is subscripted, the evaluated result of the expression list will be passed to one of these methods. For more details on when ``__class_getitem__`` is called instead of ``__getitem__``, see :ref:`classgetitem-versus-getitem`. -If the expression list contains at least one comma, it will evaluate to a -:class:`tuple` containing the items of the expression list. Otherwise, the -expression list will evaluate to the value of the list's sole member. +If the expression list contains at least one comma, or if any of the expressions +is starred, the expression list will evaluate to a :class:`tuple` containing the +items of the expression list. Otherwise, the expression list will evaluate to +the value of the list's sole member. + +.. versionchanged:: 3.11 + Expressions in an expression list may be starred. See :pep:`646` for details. For built-in objects, there are two types of objects that support subscription via :meth:`~object.__getitem__`: @@ -1874,7 +1878,7 @@ Expression lists single: , (comma); expression list .. productionlist:: python-grammar - expression_list: `expression` ("," `expression`)* [","] + expression_list: `starred_expression` ("," `starred_expression`)* [","] starred_list: `starred_item` ("," `starred_item`)* [","] starred_expression: `expression` | (`starred_item` ",")* [`starred_item`] starred_item: `assignment_expression` | "*" `or_expr` @@ -1898,6 +1902,9 @@ the unpacking. .. versionadded:: 3.5 Iterable unpacking in expression lists, originally proposed by :pep:`448`. +.. versionadded:: 3.11 + Any item in an expression list may be starred. See :pep:`646`. + .. index:: pair: trailing; comma A trailing comma is required only to create a one-item tuple, From bdab6262eed854ad2028ef5b17009162be9aef2e Mon Sep 17 00:00:00 2001 From: Matthew Rahtz Date: Sun, 30 Jun 2024 11:39:25 +0100 Subject: [PATCH 2/9] Attempt 2 To recap: the objective is to make starred expressions valid in `subscription`, which is used for generics: `Generic[...]`, `list[...]`, etc. What _is_ gramatically valid in such contexts? Seemingly any of the following. (At least, none of the following throw `SyntaxError` in a 3.12.3 REPL.) Generic[x] Generic[*x] Generic[*x, y] Generic[y, *x] Generic[x := 1] Generic[x := 1, y := 2] So introducting flexible_expression: expression | assignment_expression | starred_item end then switching `subscription` to use `flexible_expression` sorts that. But then we need to field `yield` - for which any of the following are apparently valid: yield x yield x, yield x, y yield *x, yield *x, *y Introducing a separate `yield_list` is the simplest way I've been figure out to do this - separating out the special case of `starred_item ,`. --- Doc/reference/expressions.rst | 56 ++++++++++++++++++----------------- 1 file changed, 29 insertions(+), 27 deletions(-) diff --git a/Doc/reference/expressions.rst b/Doc/reference/expressions.rst index 139a0a4fb01c85..7e1a3665c03660 100644 --- a/Doc/reference/expressions.rst +++ b/Doc/reference/expressions.rst @@ -423,7 +423,7 @@ Yield expressions .. productionlist:: python-grammar yield_atom: "(" `yield_expression` ")" yield_from: "yield" "from" `expression` - yield_expression: "yield" `expression_list` | `yield_from` + yield_expression: "yield" `yield_list` | `yield_from` The yield expression is used when defining a :term:`generator` function or an :term:`asynchronous generator` function and @@ -454,9 +454,9 @@ When a generator function is called, it returns an iterator known as a generator. That generator then controls the execution of the generator function. The execution starts when one of the generator's methods is called. At that time, the execution proceeds to the first yield expression, where it is -suspended again, returning the value of :token:`~python-grammar:expression_list` -to the generator's caller, -or ``None`` if :token:`~python-grammar:expression_list` is omitted. +suspended again, returning the value of +:token:`~python-grammar:flexible_expression_list` to the generator's caller, +or ``None`` if :token:`~python-grammar:flexible_expression_list` is omitted. By suspended, we mean that all local state is retained, including the current bindings of local variables, the instruction pointer, the internal evaluation stack, and the state of any exception handling. @@ -545,9 +545,9 @@ is already executing raises a :exc:`ValueError` exception. :meth:`~generator.__next__` method, the current yield expression always evaluates to :const:`None`. The execution then continues to the next yield expression, where the generator is suspended again, and the value of the - :token:`~python-grammar:expression_list` is returned to :meth:`__next__`'s - caller. If the generator exits without yielding another value, a - :exc:`StopIteration` exception is raised. + :token:`~python-grammar:flexible_expression_list` is returned to + :meth:`__next__`'s caller. If the generator exits without yielding another + value, a :exc:`StopIteration` exception is raised. This method is normally called implicitly, e.g. by a :keyword:`for` loop, or by the built-in :func:`next` function. @@ -664,17 +664,17 @@ how a generator object would be used in a :keyword:`for` statement. Calling one of the asynchronous generator's methods returns an :term:`awaitable` object, and the execution starts when this object is awaited on. At that time, the execution proceeds to the first yield expression, where it is suspended -again, returning the value of :token:`~python-grammar:expression_list` to the -awaiting coroutine. As with a generator, suspension means that all local state -is retained, including the current bindings of local variables, the instruction -pointer, the internal evaluation stack, and the state of any exception handling. -When the execution is resumed by awaiting on the next object returned by the -asynchronous generator's methods, the function can proceed exactly as if the -yield expression were just another external call. The value of the yield -expression after resuming depends on the method which resumed the execution. If -:meth:`~agen.__anext__` is used then the result is :const:`None`. Otherwise, if -:meth:`~agen.asend` is used, then the result will be the value passed in to that -method. +again, returning the value of :token:`~python-grammar:flexible_expression_list` +to the awaiting coroutine. As with a generator, suspension means that all local +state is retained, including the current bindings of local variables, the +instruction pointer, the internal evaluation stack, and the state of any +exception handling. When the execution is resumed by awaiting on the next object +returned by the asynchronous generator's methods, the function can proceed +exactly as if the yield expression were just another external call. The value of +the yield expression after resuming depends on the method which resumed the +execution. If :meth:`~agen.__anext__` is used then the result is :const:`None`. +Otherwise, if :meth:`~agen.asend` is used, then the result will be the value +passed in to that method. If an asynchronous generator happens to exit early by :keyword:`break`, the caller task being cancelled, or other exceptions, the generator's async cleanup code @@ -728,10 +728,10 @@ which are used to control the execution of a generator function. asynchronous generator function is resumed with an :meth:`~agen.__anext__` method, the current yield expression always evaluates to :const:`None` in the returned awaitable, which when run will continue to the next yield - expression. The value of the :token:`~python-grammar:expression_list` of the - yield expression is the value of the :exc:`StopIteration` exception raised by - the completing coroutine. If the asynchronous generator exits without - yielding another value, the awaitable instead raises a + expression. The value of the :token:`~python-grammar:flexible_expression_list` + of the yield expression is the value of the :exc:`StopIteration` exception + raised by the completing coroutine. If the asynchronous generator exits + without yielding another value, the awaitable instead raises a :exc:`StopAsyncIteration` exception, signalling that the asynchronous iteration has completed. @@ -861,7 +861,7 @@ will generally select an element from the container. The subscription of a :ref:`GenericAlias ` object. .. productionlist:: python-grammar - subscription: `primary` "[" `expression_list` "]" + subscription: `primary` "[" `flexible_expression_list` "]" When an object is subscripted, the interpreter will evaluate the primary and the expression list. @@ -1878,10 +1878,12 @@ Expression lists single: , (comma); expression list .. productionlist:: python-grammar - expression_list: `starred_expression` ("," `starred_expression`)* [","] - starred_list: `starred_item` ("," `starred_item`)* [","] - starred_expression: `expression` | (`starred_item` ",")* [`starred_item`] - starred_item: `assignment_expression` | "*" `or_expr` + + starred_item: "*" `or_expr` + flexible_expression: `expression` | `assignment_expression` | `starred_item` + flexible_expression_list: `flexible_expression` ("," `flexible_expression`)* [","] + yield_list: `expression` ["," `flexible_expression_list`] + | `starred_item` "," [`flexible_expression_list`] .. index:: pair: object; tuple From 3059bd9c7296a87175d810e3f94440e97bcbfd87 Mon Sep 17 00:00:00 2001 From: Matthew Rahtz Date: Sun, 30 Jun 2024 12:07:48 +0100 Subject: [PATCH 3/9] Cleanups --- Doc/reference/compound_stmts.rst | 2 +- Doc/reference/expressions.rst | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Doc/reference/compound_stmts.rst b/Doc/reference/compound_stmts.rst index 5eb633d004b4b8..335edd0586959a 100644 --- a/Doc/reference/compound_stmts.rst +++ b/Doc/reference/compound_stmts.rst @@ -1341,7 +1341,7 @@ a different order than they appear in the source code. .. versionchanged:: 3.11 Parameters of the form "``*identifier``" may have an annotation - "``: *expression``". See :pep:`646` for details. + "``: *expression``". See :pep:`646`. .. index:: pair: lambda; expression diff --git a/Doc/reference/expressions.rst b/Doc/reference/expressions.rst index 7e1a3665c03660..1bd9e1e1271c84 100644 --- a/Doc/reference/expressions.rst +++ b/Doc/reference/expressions.rst @@ -874,12 +874,12 @@ passed to one of these methods. For more details on when ``__class_getitem__`` is called instead of ``__getitem__``, see :ref:`classgetitem-versus-getitem`. If the expression list contains at least one comma, or if any of the expressions -is starred, the expression list will evaluate to a :class:`tuple` containing the -items of the expression list. Otherwise, the expression list will evaluate to -the value of the list's sole member. +are starred, the expression list will evaluate to a :class:`tuple` containing +the items of the expression list. Otherwise, the expression list will evaluate +to the value of the list's sole member. .. versionchanged:: 3.11 - Expressions in an expression list may be starred. See :pep:`646` for details. + Expressions in an expression list may be starred. See :pep:`646`. For built-in objects, there are two types of objects that support subscription via :meth:`~object.__getitem__`: From e7c8c2540d822a0e2833495e7cbcd4aa817ebeba Mon Sep 17 00:00:00 2001 From: Matthew Rahtz Date: Sun, 30 Jun 2024 12:17:42 +0100 Subject: [PATCH 4/9] Simplify --- Doc/reference/expressions.rst | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/Doc/reference/expressions.rst b/Doc/reference/expressions.rst index 1bd9e1e1271c84..41725a51b93cc8 100644 --- a/Doc/reference/expressions.rst +++ b/Doc/reference/expressions.rst @@ -253,7 +253,7 @@ A list display is a possibly empty series of expressions enclosed in square brackets: .. productionlist:: python-grammar - list_display: "[" [`starred_list` | `comprehension`] "]" + list_display: "[" [`expression_list` | `comprehension`] "]" A list display yields a new list object, the contents being specified by either a list of expressions or a comprehension. When a comma-separated list of @@ -278,7 +278,7 @@ A set display is denoted by curly braces and distinguishable from dictionary displays by the lack of colons separating keys and values: .. productionlist:: python-grammar - set_display: "{" (`starred_list` | `comprehension`) "}" + set_display: "{" (`expression_list` | `comprehension`) "}" A set display yields a new mutable set object, the contents being specified by either a sequence of expressions or a comprehension. When a comma-separated @@ -455,8 +455,8 @@ generator. That generator then controls the execution of the generator function. The execution starts when one of the generator's methods is called. At that time, the execution proceeds to the first yield expression, where it is suspended again, returning the value of -:token:`~python-grammar:flexible_expression_list` to the generator's caller, -or ``None`` if :token:`~python-grammar:flexible_expression_list` is omitted. +:token:`~python-grammar:expression_list` to the generator's caller, +or ``None`` if :token:`~python-grammar:expression_list` is omitted. By suspended, we mean that all local state is retained, including the current bindings of local variables, the instruction pointer, the internal evaluation stack, and the state of any exception handling. @@ -545,7 +545,7 @@ is already executing raises a :exc:`ValueError` exception. :meth:`~generator.__next__` method, the current yield expression always evaluates to :const:`None`. The execution then continues to the next yield expression, where the generator is suspended again, and the value of the - :token:`~python-grammar:flexible_expression_list` is returned to + :token:`~python-grammar:expression_list` is returned to :meth:`__next__`'s caller. If the generator exits without yielding another value, a :exc:`StopIteration` exception is raised. @@ -664,7 +664,7 @@ how a generator object would be used in a :keyword:`for` statement. Calling one of the asynchronous generator's methods returns an :term:`awaitable` object, and the execution starts when this object is awaited on. At that time, the execution proceeds to the first yield expression, where it is suspended -again, returning the value of :token:`~python-grammar:flexible_expression_list` +again, returning the value of :token:`~python-grammar:expression_list` to the awaiting coroutine. As with a generator, suspension means that all local state is retained, including the current bindings of local variables, the instruction pointer, the internal evaluation stack, and the state of any @@ -728,7 +728,7 @@ which are used to control the execution of a generator function. asynchronous generator function is resumed with an :meth:`~agen.__anext__` method, the current yield expression always evaluates to :const:`None` in the returned awaitable, which when run will continue to the next yield - expression. The value of the :token:`~python-grammar:flexible_expression_list` + expression. The value of the :token:`~python-grammar:expression_list` of the yield expression is the value of the :exc:`StopIteration` exception raised by the completing coroutine. If the asynchronous generator exits without yielding another value, the awaitable instead raises a @@ -861,7 +861,7 @@ will generally select an element from the container. The subscription of a :ref:`GenericAlias ` object. .. productionlist:: python-grammar - subscription: `primary` "[" `flexible_expression_list` "]" + subscription: `primary` "[" `expression_list` "]" When an object is subscripted, the interpreter will evaluate the primary and the expression list. @@ -1879,11 +1879,11 @@ Expression lists .. productionlist:: python-grammar - starred_item: "*" `or_expr` - flexible_expression: `expression` | `assignment_expression` | `starred_item` - flexible_expression_list: `flexible_expression` ("," `flexible_expression`)* [","] - yield_list: `expression` ["," `flexible_expression_list`] - | `starred_item` "," [`flexible_expression_list`] + expression_list: `flexible_expression` ("," `flexible_expression`)* [","] + yield_list: `expression` ["," `expression_list`] + | `starred_expression` "," [`expression_list`] + flexible_expression: `assignment_expression` | `starred_expression` + starred_expression: "*" `or_expr` .. index:: pair: object; tuple From 79953ce182441b6868fff47a0153b5fba588725b Mon Sep 17 00:00:00 2001 From: Matthew Rahtz Date: Sun, 30 Jun 2024 12:45:28 +0100 Subject: [PATCH 5/9] Another fix --- Doc/reference/expressions.rst | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Doc/reference/expressions.rst b/Doc/reference/expressions.rst index 41725a51b93cc8..be81f70dda71f2 100644 --- a/Doc/reference/expressions.rst +++ b/Doc/reference/expressions.rst @@ -1880,10 +1880,11 @@ Expression lists .. productionlist:: python-grammar expression_list: `flexible_expression` ("," `flexible_expression`)* [","] - yield_list: `expression` ["," `expression_list`] - | `starred_expression` "," [`expression_list`] + yield_list: `expression` ["," `starred_expression_list`] + | `starred_expression` "," [`starred_expression_list`] + starred_expression_list: `starred_expression` ("," `starred_expression`)* [","] flexible_expression: `assignment_expression` | `starred_expression` - starred_expression: "*" `or_expr` + starred_expression: ["*"] `or_expr` .. index:: pair: object; tuple From b8e2f02bb667ebda630a302bdcf7797eeec5f5ac Mon Sep 17 00:00:00 2001 From: Matthew Rahtz Date: Sun, 30 Jun 2024 12:50:20 +0100 Subject: [PATCH 6/9] Fix some whitespace changes --- Doc/reference/expressions.rst | 41 +++++++++++++++++------------------ 1 file changed, 20 insertions(+), 21 deletions(-) diff --git a/Doc/reference/expressions.rst b/Doc/reference/expressions.rst index be81f70dda71f2..33f2c6f2a36cab 100644 --- a/Doc/reference/expressions.rst +++ b/Doc/reference/expressions.rst @@ -454,8 +454,8 @@ When a generator function is called, it returns an iterator known as a generator. That generator then controls the execution of the generator function. The execution starts when one of the generator's methods is called. At that time, the execution proceeds to the first yield expression, where it is -suspended again, returning the value of -:token:`~python-grammar:expression_list` to the generator's caller, +suspended again, returning the value of :token:`~python-grammar:expression_list` +to the generator's caller, or ``None`` if :token:`~python-grammar:expression_list` is omitted. By suspended, we mean that all local state is retained, including the current bindings of local variables, the instruction @@ -545,9 +545,9 @@ is already executing raises a :exc:`ValueError` exception. :meth:`~generator.__next__` method, the current yield expression always evaluates to :const:`None`. The execution then continues to the next yield expression, where the generator is suspended again, and the value of the - :token:`~python-grammar:expression_list` is returned to - :meth:`__next__`'s caller. If the generator exits without yielding another - value, a :exc:`StopIteration` exception is raised. + :token:`~python-grammar:expression_list` is returned to :meth:`__next__`'s + caller. If the generator exits without yielding another value, a + :exc:`StopIteration` exception is raised. This method is normally called implicitly, e.g. by a :keyword:`for` loop, or by the built-in :func:`next` function. @@ -664,17 +664,17 @@ how a generator object would be used in a :keyword:`for` statement. Calling one of the asynchronous generator's methods returns an :term:`awaitable` object, and the execution starts when this object is awaited on. At that time, the execution proceeds to the first yield expression, where it is suspended -again, returning the value of :token:`~python-grammar:expression_list` -to the awaiting coroutine. As with a generator, suspension means that all local -state is retained, including the current bindings of local variables, the -instruction pointer, the internal evaluation stack, and the state of any -exception handling. When the execution is resumed by awaiting on the next object -returned by the asynchronous generator's methods, the function can proceed -exactly as if the yield expression were just another external call. The value of -the yield expression after resuming depends on the method which resumed the -execution. If :meth:`~agen.__anext__` is used then the result is :const:`None`. -Otherwise, if :meth:`~agen.asend` is used, then the result will be the value -passed in to that method. +again, returning the value of :token:`~python-grammar:expression_list` to the +awaiting coroutine. As with a generator, suspension means that all local state +is retained, including the current bindings of local variables, the instruction +pointer, the internal evaluation stack, and the state of any exception handling. +When the execution is resumed by awaiting on the next object returned by the +asynchronous generator's methods, the function can proceed exactly as if the +yield expression were just another external call. The value of the yield +expression after resuming depends on the method which resumed the execution. If +:meth:`~agen.__anext__` is used then the result is :const:`None`. Otherwise, if +:meth:`~agen.asend` is used, then the result will be the value passed in to that +method. If an asynchronous generator happens to exit early by :keyword:`break`, the caller task being cancelled, or other exceptions, the generator's async cleanup code @@ -728,10 +728,10 @@ which are used to control the execution of a generator function. asynchronous generator function is resumed with an :meth:`~agen.__anext__` method, the current yield expression always evaluates to :const:`None` in the returned awaitable, which when run will continue to the next yield - expression. The value of the :token:`~python-grammar:expression_list` - of the yield expression is the value of the :exc:`StopIteration` exception - raised by the completing coroutine. If the asynchronous generator exits - without yielding another value, the awaitable instead raises a + expression. The value of the :token:`~python-grammar:expression_list` of the + yield expression is the value of the :exc:`StopIteration` exception raised by + the completing coroutine. If the asynchronous generator exits without + yielding another value, the awaitable instead raises a :exc:`StopAsyncIteration` exception, signalling that the asynchronous iteration has completed. @@ -1878,7 +1878,6 @@ Expression lists single: , (comma); expression list .. productionlist:: python-grammar - expression_list: `flexible_expression` ("," `flexible_expression`)* [","] yield_list: `expression` ["," `starred_expression_list`] | `starred_expression` "," [`starred_expression_list`] From 6fed4d5bf53ad3a7876b3be58591d74607aaee2e Mon Sep 17 00:00:00 2001 From: Matthew Rahtz Date: Sun, 30 Jun 2024 13:00:11 +0100 Subject: [PATCH 7/9] Fix order --- Doc/reference/expressions.rst | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Doc/reference/expressions.rst b/Doc/reference/expressions.rst index 33f2c6f2a36cab..2b19f63e3e1556 100644 --- a/Doc/reference/expressions.rst +++ b/Doc/reference/expressions.rst @@ -1878,12 +1878,12 @@ Expression lists single: , (comma); expression list .. productionlist:: python-grammar + starred_expression: ["*"] `or_expr` + flexible_expression: `assignment_expression` | `starred_expression` expression_list: `flexible_expression` ("," `flexible_expression`)* [","] + starred_expression_list: `starred_expression` ("," `starred_expression`)* [","] yield_list: `expression` ["," `starred_expression_list`] | `starred_expression` "," [`starred_expression_list`] - starred_expression_list: `starred_expression` ("," `starred_expression`)* [","] - flexible_expression: `assignment_expression` | `starred_expression` - starred_expression: ["*"] `or_expr` .. index:: pair: object; tuple From e01e592a17c953b3bf6603f4a66adc39ecb2440a Mon Sep 17 00:00:00 2001 From: Matthew Rahtz Date: Sun, 30 Jun 2024 21:43:12 +0100 Subject: [PATCH 8/9] Simplify `yield_list` Co-authored-by: Jelle Zijlstra --- Doc/reference/expressions.rst | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Doc/reference/expressions.rst b/Doc/reference/expressions.rst index 2b19f63e3e1556..3abffe6d52da41 100644 --- a/Doc/reference/expressions.rst +++ b/Doc/reference/expressions.rst @@ -1882,8 +1882,7 @@ Expression lists flexible_expression: `assignment_expression` | `starred_expression` expression_list: `flexible_expression` ("," `flexible_expression`)* [","] starred_expression_list: `starred_expression` ("," `starred_expression`)* [","] - yield_list: `expression` ["," `starred_expression_list`] - | `starred_expression` "," [`starred_expression_list`] + yield_list: `expression` | `starred_expression` "," [`starred_expression_list`] .. index:: pair: object; tuple From 73ddba1af2e4c319bb85efcf995976c5a45b5b69 Mon Sep 17 00:00:00 2001 From: Matthew Rahtz Date: Wed, 3 Jul 2024 10:45:47 +0200 Subject: [PATCH 9/9] Add expression_list to yield_list --- Doc/reference/expressions.rst | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/Doc/reference/expressions.rst b/Doc/reference/expressions.rst index 3abffe6d52da41..a2d927cc6ce9e6 100644 --- a/Doc/reference/expressions.rst +++ b/Doc/reference/expressions.rst @@ -253,7 +253,7 @@ A list display is a possibly empty series of expressions enclosed in square brackets: .. productionlist:: python-grammar - list_display: "[" [`expression_list` | `comprehension`] "]" + list_display: "[" [`flexible_expression_list` | `comprehension`] "]" A list display yields a new list object, the contents being specified by either a list of expressions or a comprehension. When a comma-separated list of @@ -278,7 +278,7 @@ A set display is denoted by curly braces and distinguishable from dictionary displays by the lack of colons separating keys and values: .. productionlist:: python-grammar - set_display: "{" (`expression_list` | `comprehension`) "}" + set_display: "{" (`flexible_expression_list` | `comprehension`) "}" A set display yields a new mutable set object, the contents being specified by either a sequence of expressions or a comprehension. When a comma-separated @@ -454,9 +454,9 @@ When a generator function is called, it returns an iterator known as a generator. That generator then controls the execution of the generator function. The execution starts when one of the generator's methods is called. At that time, the execution proceeds to the first yield expression, where it is -suspended again, returning the value of :token:`~python-grammar:expression_list` +suspended again, returning the value of :token:`~python-grammar:yield_list` to the generator's caller, -or ``None`` if :token:`~python-grammar:expression_list` is omitted. +or ``None`` if :token:`~python-grammar:yield_list` is omitted. By suspended, we mean that all local state is retained, including the current bindings of local variables, the instruction pointer, the internal evaluation stack, and the state of any exception handling. @@ -545,7 +545,7 @@ is already executing raises a :exc:`ValueError` exception. :meth:`~generator.__next__` method, the current yield expression always evaluates to :const:`None`. The execution then continues to the next yield expression, where the generator is suspended again, and the value of the - :token:`~python-grammar:expression_list` is returned to :meth:`__next__`'s + :token:`~python-grammar:yield_list` is returned to :meth:`__next__`'s caller. If the generator exits without yielding another value, a :exc:`StopIteration` exception is raised. @@ -664,7 +664,7 @@ how a generator object would be used in a :keyword:`for` statement. Calling one of the asynchronous generator's methods returns an :term:`awaitable` object, and the execution starts when this object is awaited on. At that time, the execution proceeds to the first yield expression, where it is suspended -again, returning the value of :token:`~python-grammar:expression_list` to the +again, returning the value of :token:`~python-grammar:yield_list` to the awaiting coroutine. As with a generator, suspension means that all local state is retained, including the current bindings of local variables, the instruction pointer, the internal evaluation stack, and the state of any exception handling. @@ -728,7 +728,7 @@ which are used to control the execution of a generator function. asynchronous generator function is resumed with an :meth:`~agen.__anext__` method, the current yield expression always evaluates to :const:`None` in the returned awaitable, which when run will continue to the next yield - expression. The value of the :token:`~python-grammar:expression_list` of the + expression. The value of the :token:`~python-grammar:yield_list` of the yield expression is the value of the :exc:`StopIteration` exception raised by the completing coroutine. If the asynchronous generator exits without yielding another value, the awaitable instead raises a @@ -861,7 +861,7 @@ will generally select an element from the container. The subscription of a :ref:`GenericAlias ` object. .. productionlist:: python-grammar - subscription: `primary` "[" `expression_list` "]" + subscription: `primary` "[" `flexible_expression_list` "]" When an object is subscripted, the interpreter will evaluate the primary and the expression list. @@ -1880,9 +1880,10 @@ Expression lists .. productionlist:: python-grammar starred_expression: ["*"] `or_expr` flexible_expression: `assignment_expression` | `starred_expression` - expression_list: `flexible_expression` ("," `flexible_expression`)* [","] + flexible_expression_list: `flexible_expression` ("," `flexible_expression`)* [","] starred_expression_list: `starred_expression` ("," `starred_expression`)* [","] - yield_list: `expression` | `starred_expression` "," [`starred_expression_list`] + expression_list: `expression` ("," `expression`)* [","] + yield_list: `expression_list` | `starred_expression` "," [`starred_expression_list`] .. index:: pair: object; tuple