Skip to content

Commit 08fb8ac

Browse files
authored
bpo-42128: Add 'missing :' syntax error message to match statements (GH-24733)
1 parent 526fdeb commit 08fb8ac

File tree

3 files changed

+368
-184
lines changed

3 files changed

+368
-184
lines changed

Grammar/python.gram

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,13 +211,15 @@ finally_block[asdl_stmt_seq*]: 'finally' ':' a=block { a }
211211
match_stmt[stmt_ty]:
212212
| "match" subject=subject_expr ':' NEWLINE INDENT cases[asdl_match_case_seq*]=case_block+ DEDENT {
213213
CHECK_VERSION(stmt_ty, 10, "Pattern matching is", _Py_Match(subject, cases, EXTRA)) }
214+
| invalid_match_stmt
214215
subject_expr[expr_ty]:
215216
| value=star_named_expression ',' values=star_named_expressions? {
216217
_Py_Tuple(CHECK(asdl_expr_seq*, _PyPegen_seq_insert_in_front(p, value, values)), Load, EXTRA) }
217218
| named_expression
218219
case_block[match_case_ty]:
219220
| "case" pattern=patterns guard=guard? ':' body=block {
220221
_Py_match_case(pattern, guard, body, p->arena) }
222+
| invalid_case_block
221223
guard[expr_ty]: 'if' guard=named_expression { guard }
222224

223225
patterns[expr_ty]:
@@ -853,3 +855,9 @@ invalid_except_block:
853855
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "exception group must be parenthesized") }
854856
| 'except' expression ['as' NAME ] &&':'
855857
| 'except' &&':'
858+
859+
invalid_match_stmt:
860+
| "match" subject_expr !':' { CHECK_VERSION(void*, 10, "Pattern matching is", RAISE_SYNTAX_ERROR("expected ':'") ) }
861+
862+
invalid_case_block:
863+
| "case" patterns guard? !':' { RAISE_SYNTAX_ERROR("expected ':'") }

Lib/test/test_syntax.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -825,6 +825,24 @@
825825
Traceback (most recent call last):
826826
SyntaxError: expected ':'
827827
828+
>>> match x
829+
... case list():
830+
... pass
831+
Traceback (most recent call last):
832+
SyntaxError: expected ':'
833+
834+
>>> match x:
835+
... case list()
836+
... pass
837+
Traceback (most recent call last):
838+
SyntaxError: expected ':'
839+
840+
>>> match x:
841+
... case [y] if y > 0
842+
... pass
843+
Traceback (most recent call last):
844+
SyntaxError: expected ':'
845+
828846
Make sure that the old "raise X, Y[, Z]" form is gone:
829847
>>> raise X, Y
830848
Traceback (most recent call last):
@@ -1159,6 +1177,24 @@ def test_error_parenthesis(self):
11591177
for paren in ")]}":
11601178
self._check_error(paren + "1 + 2", f"unmatched '\\{paren}'")
11611179

1180+
def test_match_call_does_not_raise_syntax_error(self):
1181+
code = """
1182+
def match(x):
1183+
return 1+1
1184+
1185+
match(34)
1186+
"""
1187+
compile(code, "<string>", "exec")
1188+
1189+
def test_case_call_does_not_raise_syntax_error(self):
1190+
code = """
1191+
def case(x):
1192+
return 1+1
1193+
1194+
case(34)
1195+
"""
1196+
compile(code, "<string>", "exec")
1197+
11621198

11631199
def test_main():
11641200
support.run_unittest(SyntaxTestCase)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy