Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions Grammar/python.gram
Original file line number Diff line number Diff line change
Expand Up @@ -1503,6 +1503,7 @@ invalid_case_block:
| "case" patterns guard? NEWLINE { RAISE_SYNTAX_ERROR("expected ':'") }
| a="case" patterns guard? ':' NEWLINE !INDENT {
RAISE_INDENTATION_ERROR("expected an indented block after 'case' statement on line %d", a->lineno) }
| !"case" { RAISE_SYNTAX_ERROR_ON_NEXT_TOKEN("expected 'case' statement inside 'match' statement") }
Comment thread
brianschubert marked this conversation as resolved.
Outdated
Copy link
Copy Markdown
Member

@pablogsal pablogsal Mar 28, 2026

Choose a reason for hiding this comment

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

Unless I'm missing something, is this still incomplete for invalid statements that start with soft-keyword case?

For example with

compile("match 1:\n    case = 1\n", "<repro>", "exec")
compile("match 1:\n    case += 1\n", "<repro>", "exec")
compile("match 1:\n    case, x = 1, 2\n", "<repro>", "exec")

you get:

SyntaxError: invalid syntax @ line 2, col 10
SyntaxError: invalid syntax @ line 2, col 10
SyntaxError: invalid syntax @ line 2, col 9

So the new diagnostic still doesn't seem to catch these case-starting non-case statements inside a match body. Should those be covered too?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Should those be covered too?

Hmm, possibly? It occurred that me that this wouldn't catch cases like this, but I figured that this would be tricky to do right and only represents a vanishingly small set of statements in practice, so I didn't give it much thought.

Do you think it's worth trying to catch these too? I'd worry that a rule that matches these cases would also run the risk of matching an actual malformed case statement, which could make for a rather confusing error for the user

invalid_as_pattern:
| or_pattern 'as' a="_" { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "cannot use '_' as a target") }
| or_pattern 'as' a=expression {
Expand Down
34 changes: 33 additions & 1 deletion Lib/test/test_syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@
>>> match x:
... y = 3
Traceback (most recent call last):
SyntaxError: invalid syntax
SyntaxError: expected 'case' statement inside 'match' statement

>>> match x:
... case y:
Expand Down Expand Up @@ -3466,6 +3466,38 @@ def test_match_stmt_invalid_as_expr(self):
end_offset=15 + len("obj.attr"),
)

def test_match_stmt_contains_invalid_stmt(self):
for stmt in [
"a",
"a = 1",
"if a:",
"else:",
"match 1:"
Comment thread
brianschubert marked this conversation as resolved.
Outdated
"pass",
"return",
"return 2",
"raise Exception('a')",
"del a",
"yield 2",
"assert False",
"break",
"continue",
"import",
"import ast",
"from",
"from ast import *"
]:
self._check_error(
textwrap.dedent(
f"""
match 1:
{stmt}
"""
),
errtext="expected 'case' statement inside 'match' statement",
lineno=3,
)

def test_ifexp_else_stmt(self):
msg = "expected expression after 'else', but statement is given"

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Improve :exc:`SyntaxError` message when a statement other than a
:keyword:`case` statement appears inside a :keyword:`match` statement.
Patch by Brian Schubert.
24 changes: 24 additions & 0 deletions Parser/parser.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading