Skip to content

Commit e2d6563

Browse files
authored
bpo-45716: Improve the error message when using True/False/None as keywords in a call (GH-29413)
1 parent 32f55d1 commit e2d6563

File tree

4 files changed

+807
-691
lines changed

4 files changed

+807
-691
lines changed

Grammar/python.gram

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1062,6 +1062,8 @@ invalid_arguments:
10621062
RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, asdl_seq_GET(b, b->size-1)->target, "Generator expression must be parenthesized") }
10631063
| a=args ',' args { _PyPegen_arguments_parsing_error(p, a) }
10641064
invalid_kwarg:
1065+
| a[Token*]=('True'|'False'|'None') b='=' {
1066+
RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "cannot assign to %s", PyBytes_AS_STRING(a->bytes)) }
10651067
| a=NAME b='=' expression for_if_clauses {
10661068
RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid syntax. Maybe you meant '==' or ':=' instead of '='?")}
10671069
| !(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)