PEP 634: change walrus pattern to AS pattern (#1661)

Co-authored-by: Brandt Bucher <brandtbucher@gmail.com>
This commit is contained in:
Guido van Rossum 2020-10-19 15:30:33 -07:00 committed by GitHub
parent a4502e04d6
commit dad6766f2b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 17 additions and 35 deletions

View File

@ -158,7 +158,7 @@ Irrefutable case blocks
A pattern is considered irrefutable if we can prove from its syntax A pattern is considered irrefutable if we can prove from its syntax
alone that it will always succeed. In particular, capture patterns alone that it will always succeed. In particular, capture patterns
and wildcard patterns are irrefutable, as are walrus patterns whose and wildcard patterns are irrefutable, and so are AS patterns whose
right-hand side is irrefutable, OR patterns containing at least right-hand side is irrefutable, OR patterns containing at least
one irrefutable pattern, and parenthesized irrefutable patterns. one irrefutable pattern, and parenthesized irrefutable patterns.
@ -177,8 +177,8 @@ Patterns
The top-level syntax for patterns is as follows:: The top-level syntax for patterns is as follows::
patterns: open_sequence_pattern | pattern patterns: open_sequence_pattern | pattern
pattern: walrus_pattern | or_pattern pattern: as_pattern | or_pattern
walrus_pattern: capture_pattern ':=' or_pattern as_pattern: or_pattern 'as' capture_pattern
or_pattern: '|'.closed_pattern+ or_pattern: '|'.closed_pattern+
closed_pattern: closed_pattern:
| literal_pattern | literal_pattern
@ -191,34 +191,19 @@ The top-level syntax for patterns is as follows::
| class_pattern | class_pattern
Walrus Patterns AS Patterns
^^^^^^^^^^^^^^^ ^^^^^^^^^^^
Syntax:: Syntax::
walrus_pattern: capture_pattern ':=' or_pattern as_pattern: or_pattern 'as' capture_pattern
(Note: the name on the left may not be ``_``.) (Note: the name on the right may not be ``_``.)
A walrus pattern matches the OR pattern on the right of the ``:=`` An AS pattern matches the OR pattern on the left of the ``as``
operator against the subject. If this fails, the walrus pattern fails. keyword against the subject. If this fails, the AS pattern fails.
Otherwise, the walrus pattern binds the subject to the name on the left Otherwise, the AS pattern binds the subject to the name on the right
of the ``:=`` operator and succeeds. of the ``as`` keyword and succeeds.
Open Issue
~~~~~~~~~~
An alternate syntax for this construct has been put forward, whose
syntax would be::
walrus_pattern: or_pattern 'as' capture_pattern
The semantics would be the same: it matches the OR pattern against the
subject and on success binds the subject to the name in the capture
pattern.
We leave it to the Steering Council to decide which form to prefer (we
would rename "walrus pattern" to "AS pattern").
OR Patterns OR Patterns
@ -415,7 +400,7 @@ Syntax::
mapping_pattern: '{' [items_pattern] '}' mapping_pattern: '{' [items_pattern] '}'
items_pattern: ','.key_value_pattern+ ','? items_pattern: ','.key_value_pattern+ ','?
key_value_pattern: key_value_pattern:
| (literal_pattern | value_pattern) ':' or_pattern | (literal_pattern | value_pattern) ':' pattern
| double_star_pattern | double_star_pattern
double_star_pattern: '**' capture_pattern double_star_pattern: '**' capture_pattern
@ -463,10 +448,7 @@ Syntax::
| keyword_patterns | keyword_patterns
positional_patterns: ','.pattern+ positional_patterns: ','.pattern+
keyword_patterns: ','.keyword_pattern+ keyword_patterns: ','.keyword_pattern+
keyword_pattern: NAME '=' or_pattern keyword_pattern: NAME '=' pattern
(Note that positional patterns may be unparenthesized walrus patterns,
but keyword patterns may not.)
A class pattern may not repeat the same keyword multiple times. A class pattern may not repeat the same keyword multiple times.
@ -596,8 +578,8 @@ Other notation used beyond standard EBNF:
guard: 'if' named_expression guard: 'if' named_expression
patterns: open_sequence_pattern | pattern patterns: open_sequence_pattern | pattern
pattern: walrus_pattern | or_pattern pattern: as_pattern | or_pattern
walrus_pattern: capture_pattern ':=' or_pattern as_pattern: or_pattern 'as' capture_pattern
or_pattern: '|'.closed_pattern+ or_pattern: '|'.closed_pattern+
closed_pattern: closed_pattern:
| literal_pattern | literal_pattern
@ -640,7 +622,7 @@ Other notation used beyond standard EBNF:
mapping_pattern: '{' [items_pattern] '}' mapping_pattern: '{' [items_pattern] '}'
items_pattern: ','.key_value_pattern+ ','? items_pattern: ','.key_value_pattern+ ','?
key_value_pattern: key_value_pattern:
| (literal_pattern | value_pattern) ':' or_pattern | (literal_pattern | value_pattern) ':' pattern
| double_star_pattern | double_star_pattern
double_star_pattern: '**' capture_pattern double_star_pattern: '**' capture_pattern
@ -651,7 +633,7 @@ Other notation used beyond standard EBNF:
| keyword_patterns | keyword_patterns
positional_patterns: ','.pattern+ positional_patterns: ','.pattern+
keyword_patterns: ','.keyword_pattern+ keyword_patterns: ','.keyword_pattern+
keyword_pattern: NAME '=' or_pattern keyword_pattern: NAME '=' pattern
Copyright Copyright