Skip to content

gh-115528: Update language reference for PEP 646 #121181

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions Doc/reference/compound_stmts.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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`

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

Parameters may have an :term:`annotation <function 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
Expand All @@ -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`.

.. index:: pair: lambda; expression

It is also possible to create anonymous functions (functions not bound to a
Expand Down
29 changes: 19 additions & 10 deletions Doc/reference/expressions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
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 built-in objects, there are two types of objects that support subscription
via :meth:`~object.__getitem__`:
Expand Down Expand Up @@ -1874,10 +1878,12 @@ Expression lists
single: , (comma); expression list

.. productionlist:: python-grammar
expression_list: `expression` ("," `expression`)* [","]
starred_list: `starred_item` ("," `starred_item`)* [","]
starred_expression: `expression` | (`starred_item` ",")* [`starred_item`]
starred_item: `assignment_expression` | "*" `or_expr`
starred_expression: ["*"] `or_expr`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or_expr is valid for what comes after *, but starred_expression now also gets used (via expression_list) in the rule for list_display, and there other expressions are valid, e.g. comparisons. I think this should be:

Suggested change
starred_expression: ["*"] `or_expr`
starred_expression: "*" `or_expr` | `expression`

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think expression is already included in expression_list through assignment_expression - though maybe it would be worth adding the extra | expression here to make it clear?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah you're right, I missed that assignment_expression includes expression.

But I think there's still something wrong here: yield a == b, c == d is valid, but a == b is a comparison, and the grammar for yield_list indicates that after the comma we can only have a starred_expression.

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`]

.. index:: pair: object; tuple

Expand All @@ -1898,6 +1904,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,
Expand Down
Loading