bpo-44838: Refine the custom syntax errors for invalid 'if' expressions (GH-27615)

This commit is contained in:
Pablo Galindo Salgado 2021-08-05 18:28:57 +01:00 committed by GitHub
parent 3d315c3116
commit f5cbea6b1b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 1370 additions and 1303 deletions

View file

@ -1083,7 +1083,7 @@ invalid_expression:
# Soft keywords need to also be ignored because they can be parsed as NAME NAME
| !(NAME STRING | SOFT_KEYWORD) a=disjunction b=expression_without_invalid {
RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid syntax. Perhaps you forgot a comma?") }
| a=disjunction 'if' b=disjunction !'else' { RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "expected 'else' after 'if' expression") }
| a=disjunction 'if' b=disjunction !('else'|':') { RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "expected 'else' after 'if' expression") }
invalid_named_expression:
| a=expression ':=' expression {

View file

@ -152,6 +152,14 @@
Traceback (most recent call last):
SyntaxError: expected 'else' after 'if' expression
>>> if True:
... print("Hello"
...
... if 2:
... print(123))
Traceback (most recent call last):
SyntaxError: invalid syntax
>>> True = True = 3
Traceback (most recent call last):
SyntaxError: cannot assign to True

View file

@ -0,0 +1,2 @@
Fixed a bug that was causing the parser to raise an incorrect custom
:exc:`SyntaxError` for invalid 'if' expressions. Patch by Pablo Galindo.

File diff suppressed because it is too large Load diff