|
1 | 1 | PEP: 845 |
2 | 2 | Title: Leading-Dot Value Patterns |
3 | | -Author: Thomas Kehrenberg <tmke8@posteo.net>, Marc Mueller |
| 3 | +Author: Thomas Kehrenberg <tmke8@posteo.net>, |
| 4 | + Marc Mueller |
4 | 5 | Sponsor: Ethan Furman <ethan@stoneleaf.us> |
5 | 6 | Discussions-To: https://discuss.python.org/t/pep-845-leading-dot-value-patterns/109100 |
6 | 7 | Status: Draft |
@@ -120,6 +121,25 @@ The value pattern will be extended to support simple names |
120 | 121 | if they are prefixed by a leading dot. The lookup is performed following the |
121 | 122 | standard Python name resolution rules. |
122 | 123 |
|
| 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 | +
|
123 | 143 | Grammar |
124 | 144 | ------- |
125 | 145 |
|
@@ -288,6 +308,18 @@ Standard name resolution covers all of these scopes without additional syntax. |
288 | 308 | In addition, ``nonlocal.NAME`` does not have an equivalent expression form |
289 | 309 | elsewhere in Python. |
290 | 310 |
|
| 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 | + |
291 | 323 | Distinguishing by case of the name |
292 | 324 | ---------------------------------- |
293 | 325 |
|
@@ -355,6 +387,15 @@ Note the difference from the class pattern ``case int():``, which matches |
355 | 387 | object itself. |
356 | 388 |
|
357 | 389 |
|
| 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 | + |
358 | 399 | Copyright |
359 | 400 | ========= |
360 | 401 |
|
|
0 commit comments