Skip to content
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
5 changes: 5 additions & 0 deletions Grammar/python.gram
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,7 @@ mapping_pattern[pattern_ty]:
CHECK(asdl_pattern_seq*, _PyPegen_get_patterns(p, items)),
NULL,
EXTRA) }
| invalid_mapping_pattern

items_pattern[asdl_seq*]:
| ','.key_value_pattern+
Expand Down Expand Up @@ -1490,6 +1491,10 @@ invalid_class_pattern:
PyPegen_first_item(a, pattern_ty),
PyPegen_last_item(a, pattern_ty),
"positional patterns follow keyword patterns") }
invalid_mapping_pattern:
| '{' (items_pattern ',')? rest=double_star_pattern ',' items_pattern ','? '}' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
rest,
"double star pattern must be the last (right-most) subpattern in the mapping pattern") }
invalid_class_argument_pattern[asdl_pattern_seq*]:
| [positional_patterns ','] keyword_patterns ',' a=positional_patterns { a }
invalid_if_stmt:
Expand Down
11 changes: 10 additions & 1 deletion Lib/test/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,16 @@ def testSyntaxErrorOffset(self):
check('[\nfile\nfor str(file)\nin\n[]\n]', 3, 5)
check('[file for\n str(file) in []]', 2, 2)
check("ages = {'Alice'=22, 'Bob'=23}", 1, 9)
check('match ...:\n case {**rest, "key": value}:\n ...', 2, 19)
check(dedent("""\
match ...:
case {**rest1, "after": after}:
...
"""), 2, 11)
check(dedent("""\
match ...:
case {"before": before, **rest2, "after": after}:
...
"""), 2, 29)
Comment on lines +255 to +264
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made it multi-line with dedent so it's easier to read--these tests were missing a case where some items were placed before the double-star pattern.

Copy link
Contributor Author

@bswck bswck Oct 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm using rest1 and rest2 names here so that they differ from what is in the grammar file and perhaps in other places. Just my intuition.

check("[a b c d e f]", 1, 2)
check("for x yfff:", 1, 7)
check("f(a for a in b, c)", 1, 3, 1, 15)
Expand Down
26 changes: 19 additions & 7 deletions Lib/test/test_syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,12 +370,6 @@
Traceback (most recent call last):
SyntaxError: invalid syntax

>>> match ...:
... case {**rest, "key": value}:
... ...
Traceback (most recent call last):
SyntaxError: invalid syntax

>>> match ...:
... case {**_}:
... ...
Expand Down Expand Up @@ -2240,7 +2234,7 @@
Traceback (most recent call last):
SyntaxError: invalid character '£' (U+00A3)

Invalid pattern matching constructs:
Invalid pattern matching constructs:
Copy link
Contributor Author

@bswck bswck Oct 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was likely an accidental indentation, fixed it by the way.


>>> match ...:
... case 42 as _:
Expand Down Expand Up @@ -2302,6 +2296,24 @@
Traceback (most recent call last):
SyntaxError: positional patterns follow keyword patterns

>>> match ...:
... case {**double_star, "spam": "eggs"}:
... ...
Traceback (most recent call last):
SyntaxError: double star pattern must be the last (right-most) subpattern in the mapping pattern

>>> match ...:
... case {"foo": 1, **double_star, "spam": "eggs"}:
... ...
Traceback (most recent call last):
SyntaxError: double star pattern must be the last (right-most) subpattern in the mapping pattern

>>> match ...:
... case {"spam": "eggs", "b": {**d, "ham": "bacon"}}:
... ...
Traceback (most recent call last):
SyntaxError: double star pattern must be the last (right-most) subpattern in the mapping pattern

Uses of the star operator which should fail:

A[:*b]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Wrong placement of a double-star pattern inside a mapping pattern now throws a specialized syntax error.
Contributed by Bartosz Sławecki in :gh:`140253`.
Loading
Loading