Skip to content

[3.10] bpo-44456: Improve the syntax error when mixing keyword and positional patterns (GH-26793) #26900

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 1 commit into from
Jun 24, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 8 additions & 0 deletions Grammar/python.gram
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,7 @@ class_pattern[pattern_ty]:
CHECK(asdl_expr_seq*, _PyPegen_get_pattern_keys(p, keywords)))),
CHECK(asdl_pattern_seq*, _PyPegen_get_patterns(p, keywords)),
EXTRA) }
| invalid_class_pattern
positional_patterns[asdl_pattern_seq*]:
| args[asdl_pattern_seq*]=','.pattern+ { args }
keyword_patterns[asdl_seq*]:
Expand Down Expand Up @@ -978,6 +979,13 @@ invalid_case_block:
invalid_as_pattern:
| or_pattern 'as' a="_" { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "cannot use '_' as a target") }
| or_pattern 'as' !NAME a=expression { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "invalid pattern target") }
invalid_class_pattern:
| name_or_attr '(' a=invalid_class_argument_pattern { RAISE_SYNTAX_ERROR_KNOWN_RANGE(
PyPegen_first_item(a, pattern_ty),
PyPegen_last_item(a, pattern_ty),
"positional patterns follow keyword patterns") }
invalid_class_argument_pattern[asdl_pattern_seq*]:
| [positional_patterns ','] keyword_patterns ',' a=positional_patterns { a }
invalid_if_stmt:
| 'if' named_expression NEWLINE { RAISE_SYNTAX_ERROR("expected ':'") }
| a='if' a=named_expression ':' NEWLINE !INDENT {
Expand Down
24 changes: 24 additions & 0 deletions Lib/test/test_syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -1240,6 +1240,30 @@
... ...
Traceback (most recent call last):
SyntaxError: invalid pattern target

>>> match ...:
... case Foo(z=1, y=2, x):
... ...
Traceback (most recent call last):
SyntaxError: positional patterns follow keyword patterns

>>> match ...:
... case Foo(a, z=1, y=2, x):
... ...
Traceback (most recent call last):
SyntaxError: positional patterns follow keyword patterns

>>> match ...:
... case Foo(z=1, x, y=2):
... ...
Traceback (most recent call last):
SyntaxError: positional patterns follow keyword patterns

>>> match ...:
... case C(a=b, c, d=e, f, g=h, i, j=k, ...):
... ...
Traceback (most recent call last):
SyntaxError: positional patterns follow keyword patterns
"""

import re
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Improve the syntax error when mixing positional and keyword patterns. Patch
by Pablo Galindo.
Loading