Skip to content

[3.9] bpo-44168: Fix error message in the parser for keyword arguments for invalid expressions (GH-26210). #26250

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Grammar/python.gram
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,7 @@ invalid_arguments:
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "Generator expression must be parenthesized") }
| a=args ',' args { _PyPegen_arguments_parsing_error(p, a) }
invalid_kwarg:
| a=expression '=' {
| !(NAME '=') a=expression b='=' {
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
a, "expression cannot contain assignment, perhaps you meant \"==\"?") }
invalid_named_expression:
Expand Down
41 changes: 23 additions & 18 deletions Lib/test/test_syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,28 +405,33 @@
... 290, 291, 292, 293, 294, 295, 296, 297, 298, 299) # doctest: +ELLIPSIS
(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ..., 297, 298, 299)

# >>> f(lambda x: x[0] = 3)
# Traceback (most recent call last):
# SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
>>> f(lambda x: x[0] = 3)
Traceback (most recent call last):
SyntaxError: expression cannot contain assignment, perhaps you meant "=="?

# Check that this error doesn't trigger for names:
>>> f(a={x: for x in {}})
Traceback (most recent call last):
SyntaxError: invalid syntax

The grammar accepts any test (basically, any expression) in the
keyword slot of a call site. Test a few different options.

# >>> f(x()=2)
# Traceback (most recent call last):
# SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
# >>> f(a or b=1)
# Traceback (most recent call last):
# SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
# >>> f(x.y=1)
# Traceback (most recent call last):
# SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
# >>> f((x)=2)
# Traceback (most recent call last):
# SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
# >>> f(True=2)
# Traceback (most recent call last):
# SyntaxError: cannot assign to True
>>> f(x()=2)
Traceback (most recent call last):
SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
>>> f(a or b=1)
Traceback (most recent call last):
SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
>>> f(x.y=1)
Traceback (most recent call last):
SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
>>> f((x)=2)
Traceback (most recent call last):
SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
>>> f(True=2)
Traceback (most recent call last):
SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
>>> f(__debug__=1)
Traceback (most recent call last):
SyntaxError: cannot assign to __debug__
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix error message in the parser involving keyword arguments with invalid
expressions. Patch by Pablo Galindo
Loading