Skip to content

Commit 92c0c45

Browse files
sobolevnbswck
andauthored
gh-138857: Improve error message for case outside of match (#138858)
* gh-138857: Improve error message for `case` outside of `match` --------- Co-authored-by: Bartosz Sławecki <[email protected]>
1 parent 161b306 commit 92c0c45

File tree

4 files changed

+80
-0
lines changed

4 files changed

+80
-0
lines changed

Grammar/python.gram

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1477,6 +1477,10 @@ invalid_match_stmt:
14771477
| "match" subject_expr NEWLINE { CHECK_VERSION(void*, 10, "Pattern matching is", RAISE_SYNTAX_ERROR("expected ':'") ) }
14781478
| a="match" subject=subject_expr ':' NEWLINE !INDENT {
14791479
RAISE_INDENTATION_ERROR("expected an indented block after 'match' statement on line %d", a->lineno) }
1480+
| a="case" patterns guard? b=':' block {
1481+
RAISE_SYNTAX_ERROR_KNOWN_RANGE(
1482+
a, b,
1483+
"case statement must be inside match statement") }
14801484
invalid_case_block:
14811485
| "case" patterns guard? NEWLINE { RAISE_SYNTAX_ERROR("expected ':'") }
14821486
| a="case" patterns guard? ':' NEWLINE !INDENT {

Lib/test/test_syntax.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,42 @@
376376
Traceback (most recent call last):
377377
SyntaxError: invalid syntax
378378
379+
# Check incorrect "case" placement with specialized error messages
380+
381+
>>> case "pattern": ...
382+
Traceback (most recent call last):
383+
SyntaxError: case statement must be inside match statement
384+
385+
>>> case 1 | 2: ...
386+
Traceback (most recent call last):
387+
SyntaxError: case statement must be inside match statement
388+
389+
>>> case klass(attr=1) | {}: ...
390+
Traceback (most recent call last):
391+
SyntaxError: case statement must be inside match statement
392+
393+
>>> case [] if x > 1: ...
394+
Traceback (most recent call last):
395+
SyntaxError: case statement must be inside match statement
396+
397+
>>> case match: ...
398+
Traceback (most recent call last):
399+
SyntaxError: case statement must be inside match statement
400+
401+
>>> case case: ...
402+
Traceback (most recent call last):
403+
SyntaxError: case statement must be inside match statement
404+
405+
>>> if some:
406+
... case 1: ...
407+
Traceback (most recent call last):
408+
SyntaxError: case statement must be inside match statement
409+
410+
>>> case some:
411+
... case 1: ...
412+
Traceback (most recent call last):
413+
SyntaxError: case statement must be inside match statement
414+
379415
# But prefixes of soft keywords should
380416
# still raise specialized errors
381417
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Improve :exc:`SyntaxError` message for ``case`` keyword placed outside
2+
:keyword:`match` body.

Parser/parser.c

Lines changed: 38 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)