Skip to content

Commit 43c4fb6

Browse files
bpo-30858: Improve error location for expressions with assignments (GH-23753)
Co-authored-by: Lysandros Nikolaou <[email protected]>
1 parent da431f7 commit 43c4fb6

File tree

5 files changed

+15
-6
lines changed

5 files changed

+15
-6
lines changed

Grammar/python.gram

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -646,7 +646,7 @@ invalid_arguments:
646646
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "Generator expression must be parenthesized") }
647647
| a=args ',' args { _PyPegen_arguments_parsing_error(p, a) }
648648
invalid_kwarg:
649-
| a=expression '=' {
649+
| expression a='=' {
650650
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
651651
a, "expression cannot contain assignment, perhaps you meant \"==\"?") }
652652
invalid_named_expression:

Lib/test/test_exceptions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ def baz():
252252
check('from __future__ import doesnt_exist', 1, 1)
253253
check('from __future__ import braces', 1, 1)
254254
check('x=1\nfrom __future__ import division', 2, 1)
255-
check('foo(1=2)', 1, 5)
255+
check('foo(1=2)', 1, 6)
256256
check('def f():\n x, y: int', 2, 3)
257257
check('[*x for x in xs]', 1, 2)
258258
check('foo(x for x in range(10), 100)', 1, 5)

Lib/test/test_syntax.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -802,6 +802,13 @@ def _check_error(self, code, errtext,
802802
else:
803803
self.fail("compile() did not raise SyntaxError")
804804

805+
def test_expression_with_assignment(self):
806+
self._check_error(
807+
"print(end1 + end2 = ' ')",
808+
'expression cannot contain assignment, perhaps you meant "=="?',
809+
offset=19
810+
)
811+
805812
def test_curly_brace_after_primary_raises_immediately(self):
806813
self._check_error("f{", "invalid syntax", mode="single")
807814

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Improve error location in expressions that contain assignments. Patch by
2+
Pablo Galindo and Lysandros Nikolaou.

Parser/parser.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14562,12 +14562,12 @@ invalid_kwarg_rule(Parser *p)
1456214562
return NULL;
1456314563
}
1456414564
D(fprintf(stderr, "%*c> invalid_kwarg[%d-%d]: %s\n", p->level, ' ', _mark, p->mark, "expression '='"));
14565-
Token * _literal;
14566-
expr_ty a;
14565+
Token * a;
14566+
expr_ty expression_var;
1456714567
if (
14568-
(a = expression_rule(p)) // expression
14568+
(expression_var = expression_rule(p)) // expression
1456914569
&&
14570-
(_literal = _PyPegen_expect_token(p, 22)) // token='='
14570+
(a = _PyPegen_expect_token(p, 22)) // token='='
1457114571
)
1457214572
{
1457314573
D(fprintf(stderr, "%*c+ invalid_kwarg[%d-%d]: %s succeeded!\n", p->level, ' ', _mark, p->mark, "expression '='"));

0 commit comments

Comments
 (0)