Skip to content

Commit 5618c81

Browse files
authored
[3.10] bpo-45716: Improve the error message when using True/False/None as keywords in a call (GH-29413). (GH-29428)
(cherry picked from commit e2d6563) Co-authored-by: Pablo Galindo Salgado <[email protected]>
1 parent 7bac598 commit 5618c81

File tree

4 files changed

+690
-574
lines changed

4 files changed

+690
-574
lines changed

Grammar/python.gram

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -838,6 +838,8 @@ invalid_arguments:
838838
RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, asdl_seq_GET(b, b->size-1)->target, "Generator expression must be parenthesized") }
839839
| a=args ',' args { _PyPegen_arguments_parsing_error(p, a) }
840840
invalid_kwarg:
841+
| a[Token*]=('True'|'False'|'None') b='=' {
842+
RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "cannot assign to %s", PyBytes_AS_STRING(a->bytes)) }
841843
| a=NAME b='=' expression for_if_clauses {
842844
RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid syntax. Maybe you meant '==' or ':=' instead of '='?")}
843845
| !(NAME '=') a=expression b='=' {

Lib/test/test_syntax.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -524,9 +524,15 @@
524524
>>> f((x)=2)
525525
Traceback (most recent call last):
526526
SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
527-
>>> f(True=2)
527+
>>> f(True=1)
528528
Traceback (most recent call last):
529-
SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
529+
SyntaxError: cannot assign to True
530+
>>> f(False=1)
531+
Traceback (most recent call last):
532+
SyntaxError: cannot assign to False
533+
>>> f(None=1)
534+
Traceback (most recent call last):
535+
SyntaxError: cannot assign to None
530536
>>> f(__debug__=1)
531537
Traceback (most recent call last):
532538
SyntaxError: cannot assign to __debug__
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Improve the :exc:`SyntaxError` message when using ``True``, ``None`` or
2+
``False`` as keywords in a function call. Patch by Pablo Galindo.

0 commit comments

Comments
 (0)