Skip to content
Open
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
4 changes: 4 additions & 0 deletions Grammar/python.gram
Original file line number Diff line number Diff line change
Expand Up @@ -1476,6 +1476,10 @@ invalid_match_stmt:
| "match" subject_expr NEWLINE { CHECK_VERSION(void*, 10, "Pattern matching is", RAISE_SYNTAX_ERROR("expected ':'") ) }
| a="match" subject=subject_expr ':' NEWLINE !INDENT {
RAISE_INDENTATION_ERROR("expected an indented block after 'match' statement on line %d", a->lineno) }
| a="case" patterns guard? b=':' block {
RAISE_SYNTAX_ERROR_KNOWN_RANGE(
a, b,
"case statement must be inside match statement") }
invalid_case_block:
| "case" patterns guard? NEWLINE { RAISE_SYNTAX_ERROR("expected ':'") }
| a="case" patterns guard? ':' NEWLINE !INDENT {
Expand Down
36 changes: 36 additions & 0 deletions Lib/test/test_syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,42 @@
Traceback (most recent call last):
SyntaxError: invalid syntax

# Check incorrect "case" placement with specialized error messages

>>> case "pattern": ...
Traceback (most recent call last):
SyntaxError: case statement must be inside match statement

>>> case 1 | 2: ...
Traceback (most recent call last):
SyntaxError: case statement must be inside match statement

>>> case klass(attr=1) | {}: ...
Traceback (most recent call last):
SyntaxError: case statement must be inside match statement

>>> case [] if x > 1: ...
Traceback (most recent call last):
SyntaxError: case statement must be inside match statement

>>> case match: ...
Traceback (most recent call last):
SyntaxError: case statement must be inside match statement

>>> case case: ...
Traceback (most recent call last):
SyntaxError: case statement must be inside match statement

>>> if some:
... case 1: ...
Traceback (most recent call last):
SyntaxError: case statement must be inside match statement

>>> case some:
... case 1: ...
Traceback (most recent call last):
SyntaxError: case statement must be inside match statement

# But prefixes of soft keywords should
# still raise specialized errors

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Improve :exc:`SyntaxError` message for ``case`` keyword placed outside
:keyword:`match` body.
38 changes: 38 additions & 0 deletions Parser/parser.c

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

Loading