Skip to content

Commit d4a9264

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

File tree

4 files changed

+356
-306
lines changed

4 files changed

+356
-306
lines changed

Grammar/python.gram

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,7 @@ invalid_arguments:
633633
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "Generator expression must be parenthesized") }
634634
| a=args ',' args { _PyPegen_arguments_parsing_error(p, a) }
635635
invalid_kwarg:
636-
| a=expression '=' {
636+
| !(NAME '=') a=expression b='=' {
637637
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
638638
a, "expression cannot contain assignment, perhaps you meant \"==\"?") }
639639
invalid_named_expression:

Lib/test/test_syntax.py

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -405,28 +405,33 @@
405405
... 290, 291, 292, 293, 294, 295, 296, 297, 298, 299) # doctest: +ELLIPSIS
406406
(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ..., 297, 298, 299)
407407
408-
# >>> f(lambda x: x[0] = 3)
409-
# Traceback (most recent call last):
410-
# SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
408+
>>> f(lambda x: x[0] = 3)
409+
Traceback (most recent call last):
410+
SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
411+
412+
# Check that this error doesn't trigger for names:
413+
>>> f(a={x: for x in {}})
414+
Traceback (most recent call last):
415+
SyntaxError: invalid syntax
411416
412417
The grammar accepts any test (basically, any expression) in the
413418
keyword slot of a call site. Test a few different options.
414419
415-
# >>> f(x()=2)
416-
# Traceback (most recent call last):
417-
# SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
418-
# >>> f(a or b=1)
419-
# Traceback (most recent call last):
420-
# SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
421-
# >>> f(x.y=1)
422-
# Traceback (most recent call last):
423-
# SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
424-
# >>> f((x)=2)
425-
# Traceback (most recent call last):
426-
# SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
427-
# >>> f(True=2)
428-
# Traceback (most recent call last):
429-
# SyntaxError: cannot assign to True
420+
>>> f(x()=2)
421+
Traceback (most recent call last):
422+
SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
423+
>>> f(a or b=1)
424+
Traceback (most recent call last):
425+
SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
426+
>>> f(x.y=1)
427+
Traceback (most recent call last):
428+
SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
429+
>>> f((x)=2)
430+
Traceback (most recent call last):
431+
SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
432+
>>> f(True=2)
433+
Traceback (most recent call last):
434+
SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
430435
>>> f(__debug__=1)
431436
Traceback (most recent call last):
432437
SyntaxError: cannot assign to __debug__
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)