Skip to content

Commit c6f3f83

Browse files
miss-islingtonmrahtzJelleZijlstra
authored
[3.12] gh-115528: Update language reference for PEP 646 (GH-121181) (#124633)
gh-115528: Update language reference for PEP 646 (GH-121181) 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 ,`. (cherry picked from commit 7d3497f) Co-authored-by: Matthew Rahtz <[email protected]> Co-authored-by: Jelle Zijlstra <[email protected]>
1 parent 27890ed commit c6f3f83

File tree

2 files changed

+32
-17
lines changed

2 files changed

+32
-17
lines changed

Doc/reference/compound_stmts.rst

+8-2
Original file line numberDiff line numberDiff line change
@@ -1217,9 +1217,10 @@ A function definition defines a user-defined function object (see section
12171217
: | `parameter_list_no_posonly`
12181218
parameter_list_no_posonly: `defparameter` ("," `defparameter`)* ["," [`parameter_list_starargs`]]
12191219
: | `parameter_list_starargs`
1220-
parameter_list_starargs: "*" [`parameter`] ("," `defparameter`)* ["," ["**" `parameter` [","]]]
1220+
parameter_list_starargs: "*" [`star_parameter`] ("," `defparameter`)* ["," ["**" `parameter` [","]]]
12211221
: | "**" `parameter` [","]
12221222
parameter: `identifier` [":" `expression`]
1223+
star_parameter: `identifier` [":" ["*"] `expression`]
12231224
defparameter: `parameter` ["=" `expression`]
12241225
funcname: `identifier`
12251226

@@ -1326,7 +1327,8 @@ and may only be passed by positional arguments.
13261327

13271328
Parameters may have an :term:`annotation <function annotation>` of the form "``: expression``"
13281329
following the parameter name. Any parameter may have an annotation, even those of the form
1329-
``*identifier`` or ``**identifier``. Functions may have "return" annotation of
1330+
``*identifier`` or ``**identifier``. (As a special case, parameters of the form
1331+
``*identifier`` may have an annotation "``: *expression``".) Functions may have "return" annotation of
13301332
the form "``-> expression``" after the parameter list. These annotations can be
13311333
any valid Python expression. The presence of annotations does not change the
13321334
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
13371339
definition is executed. In this case annotations may be evaluated in
13381340
a different order than they appear in the source code.
13391341

1342+
.. versionchanged:: 3.11
1343+
Parameters of the form "``*identifier``" may have an annotation
1344+
"``: *expression``". See :pep:`646`.
1345+
13401346
.. index:: pair: lambda; expression
13411347

13421348
It is also possible to create anonymous functions (functions not bound to a

Doc/reference/expressions.rst

+24-15
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ A list display is a possibly empty series of expressions enclosed in square
284284
brackets:
285285

286286
.. productionlist:: python-grammar
287-
list_display: "[" [`starred_list` | `comprehension`] "]"
287+
list_display: "[" [`flexible_expression_list` | `comprehension`] "]"
288288

289289
A list display yields a new list object, the contents being specified by either
290290
a list of expressions or a comprehension. When a comma-separated list of
@@ -309,7 +309,7 @@ A set display is denoted by curly braces and distinguishable from dictionary
309309
displays by the lack of colons separating keys and values:
310310

311311
.. productionlist:: python-grammar
312-
set_display: "{" (`starred_list` | `comprehension`) "}"
312+
set_display: "{" (`flexible_expression_list` | `comprehension`) "}"
313313

314314
A set display yields a new mutable set object, the contents being specified by
315315
either a sequence of expressions or a comprehension. When a comma-separated
@@ -454,7 +454,7 @@ Yield expressions
454454
.. productionlist:: python-grammar
455455
yield_atom: "(" `yield_expression` ")"
456456
yield_from: "yield" "from" `expression`
457-
yield_expression: "yield" `expression_list` | `yield_from`
457+
yield_expression: "yield" `yield_list` | `yield_from`
458458

459459
The yield expression is used when defining a :term:`generator` function
460460
or an :term:`asynchronous generator` function and
@@ -485,9 +485,9 @@ When a generator function is called, it returns an iterator known as a
485485
generator. That generator then controls the execution of the generator
486486
function. The execution starts when one of the generator's methods is called.
487487
At that time, the execution proceeds to the first yield expression, where it is
488-
suspended again, returning the value of :token:`~python-grammar:expression_list`
488+
suspended again, returning the value of :token:`~python-grammar:yield_list`
489489
to the generator's caller,
490-
or ``None`` if :token:`~python-grammar:expression_list` is omitted.
490+
or ``None`` if :token:`~python-grammar:yield_list` is omitted.
491491
By suspended, we mean that all local state is
492492
retained, including the current bindings of local variables, the instruction
493493
pointer, the internal evaluation stack, and the state of any exception handling.
@@ -576,7 +576,7 @@ is already executing raises a :exc:`ValueError` exception.
576576
:meth:`~generator.__next__` method, the current yield expression always
577577
evaluates to :const:`None`. The execution then continues to the next yield
578578
expression, where the generator is suspended again, and the value of the
579-
:token:`~python-grammar:expression_list` is returned to :meth:`__next__`'s
579+
:token:`~python-grammar:yield_list` is returned to :meth:`__next__`'s
580580
caller. If the generator exits without yielding another value, a
581581
:exc:`StopIteration` exception is raised.
582582

@@ -688,7 +688,7 @@ how a generator object would be used in a :keyword:`for` statement.
688688
Calling one of the asynchronous generator's methods returns an :term:`awaitable`
689689
object, and the execution starts when this object is awaited on. At that time,
690690
the execution proceeds to the first yield expression, where it is suspended
691-
again, returning the value of :token:`~python-grammar:expression_list` to the
691+
again, returning the value of :token:`~python-grammar:yield_list` to the
692692
awaiting coroutine. As with a generator, suspension means that all local state
693693
is retained, including the current bindings of local variables, the instruction
694694
pointer, the internal evaluation stack, and the state of any exception handling.
@@ -752,7 +752,7 @@ which are used to control the execution of a generator function.
752752
asynchronous generator function is resumed with an :meth:`~agen.__anext__`
753753
method, the current yield expression always evaluates to :const:`None` in the
754754
returned awaitable, which when run will continue to the next yield
755-
expression. The value of the :token:`~python-grammar:expression_list` of the
755+
expression. The value of the :token:`~python-grammar:yield_list` of the
756756
yield expression is the value of the :exc:`StopIteration` exception raised by
757757
the completing coroutine. If the asynchronous generator exits without
758758
yielding another value, the awaitable instead raises a
@@ -885,7 +885,7 @@ will generally select an element from the container. The subscription of a
885885
:ref:`GenericAlias <types-genericalias>` object.
886886

887887
.. productionlist:: python-grammar
888-
subscription: `primary` "[" `expression_list` "]"
888+
subscription: `primary` "[" `flexible_expression_list` "]"
889889

890890
When an object is subscripted, the interpreter will evaluate the primary and
891891
the expression list.
@@ -897,9 +897,13 @@ primary is subscripted, the evaluated result of the expression list will be
897897
passed to one of these methods. For more details on when ``__class_getitem__``
898898
is called instead of ``__getitem__``, see :ref:`classgetitem-versus-getitem`.
899899

900-
If the expression list contains at least one comma, it will evaluate to a
901-
:class:`tuple` containing the items of the expression list. Otherwise, the
902-
expression list will evaluate to the value of the list's sole member.
900+
If the expression list contains at least one comma, or if any of the expressions
901+
are starred, the expression list will evaluate to a :class:`tuple` containing
902+
the items of the expression list. Otherwise, the expression list will evaluate
903+
to the value of the list's sole member.
904+
905+
.. versionchanged:: 3.11
906+
Expressions in an expression list may be starred. See :pep:`646`.
903907

904908
For built-in objects, there are two types of objects that support subscription
905909
via :meth:`~object.__getitem__`:
@@ -1898,10 +1902,12 @@ Expression lists
18981902
single: , (comma); expression list
18991903

19001904
.. productionlist:: python-grammar
1905+
starred_expression: ["*"] `or_expr`
1906+
flexible_expression: `assignment_expression` | `starred_expression`
1907+
flexible_expression_list: `flexible_expression` ("," `flexible_expression`)* [","]
1908+
starred_expression_list: `starred_expression` ("," `starred_expression`)* [","]
19011909
expression_list: `expression` ("," `expression`)* [","]
1902-
starred_list: `starred_item` ("," `starred_item`)* [","]
1903-
starred_expression: `expression` | (`starred_item` ",")* [`starred_item`]
1904-
starred_item: `assignment_expression` | "*" `or_expr`
1910+
yield_list: `expression_list` | `starred_expression` "," [`starred_expression_list`]
19051911

19061912
.. index:: pair: object; tuple
19071913

@@ -1922,6 +1928,9 @@ the unpacking.
19221928
.. versionadded:: 3.5
19231929
Iterable unpacking in expression lists, originally proposed by :pep:`448`.
19241930

1931+
.. versionadded:: 3.11
1932+
Any item in an expression list may be starred. See :pep:`646`.
1933+
19251934
.. index:: pair: trailing; comma
19261935

19271936
A trailing comma is required only to create a one-item tuple,

0 commit comments

Comments
 (0)