Skip to content

Commit ec0699c

Browse files
bpo-44168: Fix error message in the parser for keyword arguments for invalid expressions (GH-26210) (GH-26247)
(cherry picked from commit 33c0c90) Co-authored-by: Pablo Galindo <[email protected]> Co-authored-by: Pablo Galindo <[email protected]>
1 parent e87fd41 commit ec0699c

File tree

4 files changed

+611
-561
lines changed

4 files changed

+611
-561
lines changed

Grammar/python.gram

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -843,7 +843,7 @@ invalid_arguments:
843843
RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, asdl_seq_GET(b, b->size-1)->target, "Generator expression must be parenthesized") }
844844
| a=args ',' args { _PyPegen_arguments_parsing_error(p, a) }
845845
invalid_kwarg:
846-
| a=expression b='=' {
846+
| !(NAME '=') a=expression b='=' {
847847
RAISE_SYNTAX_ERROR_KNOWN_RANGE(
848848
a, b, "expression cannot contain assignment, perhaps you meant \"==\"?") }
849849

Lib/test/test_syntax.py

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -458,28 +458,33 @@
458458
... 290, 291, 292, 293, 294, 295, 296, 297, 298, 299) # doctest: +ELLIPSIS
459459
(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ..., 297, 298, 299)
460460
461-
# >>> f(lambda x: x[0] = 3)
462-
# Traceback (most recent call last):
463-
# SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
461+
>>> f(lambda x: x[0] = 3)
462+
Traceback (most recent call last):
463+
SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
464+
465+
# Check that this error doesn't trigger for names:
466+
>>> f(a={x: for x in {}})
467+
Traceback (most recent call last):
468+
SyntaxError: invalid syntax
464469
465470
The grammar accepts any test (basically, any expression) in the
466471
keyword slot of a call site. Test a few different options.
467472
468-
# >>> f(x()=2)
469-
# Traceback (most recent call last):
470-
# SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
471-
# >>> f(a or b=1)
472-
# Traceback (most recent call last):
473-
# SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
474-
# >>> f(x.y=1)
475-
# Traceback (most recent call last):
476-
# SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
477-
# >>> f((x)=2)
478-
# Traceback (most recent call last):
479-
# SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
480-
# >>> f(True=2)
481-
# Traceback (most recent call last):
482-
# SyntaxError: cannot assign to True here. Maybe you meant '==' instead of '='?
473+
>>> f(x()=2)
474+
Traceback (most recent call last):
475+
SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
476+
>>> f(a or b=1)
477+
Traceback (most recent call last):
478+
SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
479+
>>> f(x.y=1)
480+
Traceback (most recent call last):
481+
SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
482+
>>> f((x)=2)
483+
Traceback (most recent call last):
484+
SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
485+
>>> f(True=2)
486+
Traceback (most recent call last):
487+
SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
483488
>>> f(__debug__=1)
484489
Traceback (most recent call last):
485490
SyntaxError: cannot assign to __debug__
@@ -1422,7 +1427,7 @@ def case(x):
14221427
case(34)
14231428
"""
14241429
compile(code, "<string>", "exec")
1425-
1430+
14261431
def test_multiline_compiler_error_points_to_the_end(self):
14271432
self._check_error(
14281433
"call(\na=1,\na=1\n)",
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix error message in the parser involving keyword arguments with invalid
2+
expressions. Patch by Pablo Galindo

0 commit comments

Comments
 (0)