diff --git a/peps/pep-0845.rst b/peps/pep-0845.rst index c57ddedd089..b4b0c9f3e1a 100644 --- a/peps/pep-0845.rst +++ b/peps/pep-0845.rst @@ -1,6 +1,7 @@ PEP: 845 Title: Leading-Dot Value Patterns -Author: Thomas Kehrenberg , Marc Mueller +Author: Thomas Kehrenberg , + Marc Mueller Sponsor: Ethan Furman Discussions-To: https://discuss.python.org/t/pep-845-leading-dot-value-patterns/109100 Status: Draft @@ -120,6 +121,25 @@ The value pattern will be extended to support simple names if they are prefixed by a leading dot. The lookup is performed following the standard Python name resolution rules. +Interaction with capture patterns +--------------------------------- + +**Capture patterns** are only assigned **after** a case matches but +before the guard clause is evaluated. Any lookup will resolve to an +existing variable if one exists, or raise an error. + +.. code-block:: python-console + + >>> match [1, 1]: + ... case [x, *_, .x]: + ... pass + ... + Traceback (most recent call last): + File "", line 2, in + case [x, *_, .x]: + ^ + NameError: name 'x' is not defined + Grammar ------- @@ -288,6 +308,18 @@ Standard name resolution covers all of these scopes without additional syntax. In addition, ``nonlocal.NAME`` does not have an equivalent expression form elsewhere in Python. +Reuse existing typing constructs (``Literal[NAME]``) +---------------------------------------------------- + +It was suggested to reuse ``Literal`` from :py:class:`typing.Literal` to +match simple names. So far no names other than ``_`` have a dedicated meaning +in the match statement mini-syntax. Assigning one to ``Literal`` would +thus be surprising. Additionally, subscription is not defined for use in +match cases yet. Using it here would limit future options unnecessarily. +Lastly, :py:class:`typing.Literal` only supports literal values as arguments, +e.g. ``1``, ``"Hello"`` or ``True``. Allowing simple names only inside +match cases would therefore be confusing. + Distinguishing by case of the name ---------------------------------- @@ -355,6 +387,15 @@ Note the difference from the class pattern ``case int():``, which matches object itself. +Change History +============== + +* 25-Sep-2026 + + - Clarify interaction with **capture patterns** + - Added rejected idea for ``Literal[NAME]`` + + Copyright =========