Skip to content

Commit 28b6dc9

Browse files
authored
bpo-44792: Improve syntax errors for if expressions (GH-27506)
1 parent aa0894b commit 28b6dc9

File tree

5 files changed

+48
-0
lines changed

5 files changed

+48
-0
lines changed

Grammar/python.gram

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1083,6 +1083,7 @@ invalid_expression:
10831083
# Soft keywords need to also be ignored because they can be parsed as NAME NAME
10841084
| !(NAME STRING | SOFT_KEYWORD) a=disjunction b=expression_without_invalid {
10851085
RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid syntax. Perhaps you forgot a comma?") }
1086+
| a=disjunction 'if' b=disjunction !'else' { RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "expected 'else' after 'if' expression") }
10861087

10871088
invalid_named_expression:
10881089
| a=expression ':=' expression {

Lib/test/test_syntax.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,18 @@
140140
Traceback (most recent call last):
141141
SyntaxError: cannot assign to conditional expression
142142
143+
>>> a = 42 if True
144+
Traceback (most recent call last):
145+
SyntaxError: expected 'else' after 'if' expression
146+
147+
>>> a = (42 if True)
148+
Traceback (most recent call last):
149+
SyntaxError: expected 'else' after 'if' expression
150+
151+
>>> a = [1, 42 if True, 4]
152+
Traceback (most recent call last):
153+
SyntaxError: expected 'else' after 'if' expression
154+
143155
>>> True = True = 3
144156
Traceback (most recent call last):
145157
SyntaxError: cannot assign to True

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ Tom Bridgman
227227
Anthony Briggs
228228
Keith Briggs
229229
Tobias Brink
230+
Miguel Brito
230231
Dillon Brock
231232
Richard Brodie
232233
Michael Broghton
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Improve syntax errors for if expressions. Patch by Miguel Brito

Parser/parser.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18207,6 +18207,7 @@ invalid_legacy_expression_rule(Parser *p)
1820718207
// invalid_expression:
1820818208
// | invalid_legacy_expression
1820918209
// | !(NAME STRING | SOFT_KEYWORD) disjunction expression_without_invalid
18210+
// | disjunction 'if' disjunction !'else'
1821018211
static void *
1821118212
invalid_expression_rule(Parser *p)
1821218213
{
@@ -18265,6 +18266,38 @@ invalid_expression_rule(Parser *p)
1826518266
D(fprintf(stderr, "%*c%s invalid_expression[%d-%d]: %s failed!\n", p->level, ' ',
1826618267
p->error_indicator ? "ERROR!" : "-", _mark, p->mark, "!(NAME STRING | SOFT_KEYWORD) disjunction expression_without_invalid"));
1826718268
}
18269+
{ // disjunction 'if' disjunction !'else'
18270+
if (p->error_indicator) {
18271+
D(p->level--);
18272+
return NULL;
18273+
}
18274+
D(fprintf(stderr, "%*c> invalid_expression[%d-%d]: %s\n", p->level, ' ', _mark, p->mark, "disjunction 'if' disjunction !'else'"));
18275+
Token * _keyword;
18276+
expr_ty a;
18277+
expr_ty b;
18278+
if (
18279+
(a = disjunction_rule(p)) // disjunction
18280+
&&
18281+
(_keyword = _PyPegen_expect_token(p, 510)) // token='if'
18282+
&&
18283+
(b = disjunction_rule(p)) // disjunction
18284+
&&
18285+
_PyPegen_lookahead_with_int(0, _PyPegen_expect_token, p, 518) // token='else'
18286+
)
18287+
{
18288+
D(fprintf(stderr, "%*c+ invalid_expression[%d-%d]: %s succeeded!\n", p->level, ' ', _mark, p->mark, "disjunction 'if' disjunction !'else'"));
18289+
_res = RAISE_SYNTAX_ERROR_KNOWN_RANGE ( a , b , "expected 'else' after 'if' expression" );
18290+
if (_res == NULL && PyErr_Occurred()) {
18291+
p->error_indicator = 1;
18292+
D(p->level--);
18293+
return NULL;
18294+
}
18295+
goto done;
18296+
}
18297+
p->mark = _mark;
18298+
D(fprintf(stderr, "%*c%s invalid_expression[%d-%d]: %s failed!\n", p->level, ' ',
18299+
p->error_indicator ? "ERROR!" : "-", _mark, p->mark, "disjunction 'if' disjunction !'else'"));
18300+
}
1826818301
_res = NULL;
1826918302
done:
1827018303
D(p->level--);

0 commit comments

Comments
 (0)