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 <bartosz@ilikepython.com>
This commit is contained in:
sobolevn 2025-10-24 12:20:54 +03:00 committed by GitHub
parent 161b3064ef
commit 92c0c45563
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 80 additions and 0 deletions

View file

@ -376,6 +376,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