Skip to content

Commit 730372f

Browse files
authored
PEP 845: Add clarification and rejected idea (GH-5143)
1 parent 85e7ab0 commit 730372f

1 file changed

Lines changed: 42 additions & 1 deletion

File tree

‎peps/pep-0845.rst‎

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
PEP: 845
22
Title: Leading-Dot Value Patterns
3-
Author: Thomas Kehrenberg <tmke8@posteo.net>, Marc Mueller
3+
Author: Thomas Kehrenberg <tmke8@posteo.net>,
4+
Marc Mueller
45
Sponsor: Ethan Furman <ethan@stoneleaf.us>
56
Discussions-To: https://discuss.python.org/t/pep-845-leading-dot-value-patterns/109100
67
Status: Draft
@@ -120,6 +121,25 @@ The value pattern will be extended to support simple names
120121
if they are prefixed by a leading dot. The lookup is performed following the
121122
standard Python name resolution rules.
122123

124+
Interaction with capture patterns
125+
---------------------------------
126+
127+
**Capture patterns** are only assigned **after** a case matches but
128+
before the guard clause is evaluated. Any lookup will resolve to an
129+
existing variable if one exists, or raise an error.
130+
131+
.. code-block:: python-console
132+
133+
>>> match [1, 1]:
134+
... case [x, *_, .x]:
135+
... pass
136+
...
137+
Traceback (most recent call last):
138+
File "<python-input-0>", line 2, in <module>
139+
case [x, *_, .x]:
140+
^
141+
NameError: name 'x' is not defined
142+
123143
Grammar
124144
-------
125145

@@ -288,6 +308,18 @@ Standard name resolution covers all of these scopes without additional syntax.
288308
In addition, ``nonlocal.NAME`` does not have an equivalent expression form
289309
elsewhere in Python.
290310

311+
Reuse existing typing constructs (``Literal[NAME]``)
312+
----------------------------------------------------
313+
314+
It was suggested to reuse ``Literal`` from :py:class:`typing.Literal` to
315+
match simple names. So far no names other than ``_`` have a dedicated meaning
316+
in the match statement mini-syntax. Assigning one to ``Literal`` would
317+
thus be surprising. Additionally, subscription is not defined for use in
318+
match cases yet. Using it here would limit future options unnecessarily.
319+
Lastly, :py:class:`typing.Literal` only supports literal values as arguments,
320+
e.g. ``1``, ``"Hello"`` or ``True``. Allowing simple names only inside
321+
match cases would therefore be confusing.
322+
291323
Distinguishing by case of the name
292324
----------------------------------
293325

@@ -355,6 +387,15 @@ Note the difference from the class pattern ``case int():``, which matches
355387
object itself.
356388

357389

390+
Change History
391+
==============
392+
393+
* 25-Sep-2026
394+
395+
- Clarify interaction with **capture patterns**
396+
- Added rejected idea for ``Literal[NAME]``
397+
398+
358399
Copyright
359400
=========
360401

0 commit comments

Comments
 (0)