Skip to content

Commit 6d8da23

Browse files
authored
gh-90994: Improve error messages upon call arguments syntax errors (GH-96893)
1 parent e13d1d9 commit 6d8da23

File tree

4 files changed

+1508
-1230
lines changed

4 files changed

+1508
-1230
lines changed

Grammar/python.gram

+7
Original file line numberDiff line numberDiff line change
@@ -957,6 +957,7 @@ kwargs[asdl_seq*]:
957957
| ','.kwarg_or_double_starred+
958958

959959
starred_expression[expr_ty]:
960+
| invalid_starred_expression
960961
| '*' a=expression { _PyAST_Starred(a, Load, EXTRA) }
961962

962963
kwarg_or_starred[KeywordOrStarred*]:
@@ -1083,6 +1084,8 @@ invalid_arguments:
10831084
RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, _PyPegen_get_last_comprehension_item(PyPegen_last_item(b, comprehension_ty)), "Generator expression must be parenthesized") }
10841085
| a=NAME b='=' expression for_if_clauses {
10851086
RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid syntax. Maybe you meant '==' or ':=' instead of '='?")}
1087+
| (args ',')? a=NAME b='=' &(',' | ')') {
1088+
RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "expected argument value expression")}
10861089
| a=args b=for_if_clauses { _PyPegen_nonparen_genexp_in_call(p, a, b) }
10871090
| args ',' a=expression b=for_if_clauses {
10881091
RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, _PyPegen_get_last_comprehension_item(PyPegen_last_item(b, comprehension_ty)), "Generator expression must be parenthesized") }
@@ -1095,6 +1098,8 @@ invalid_kwarg:
10951098
| !(NAME '=') a=expression b='=' {
10961099
RAISE_SYNTAX_ERROR_KNOWN_RANGE(
10971100
a, b, "expression cannot contain assignment, perhaps you meant \"==\"?") }
1101+
| a='**' expression '=' b=expression {
1102+
RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "cannot assign to keyword argument unpacking") }
10981103

10991104
# IMPORTANT: Note that the "_without_invalid" suffix causes the rule to not call invalid rules under it
11001105
expression_without_invalid[expr_ty]:
@@ -1328,3 +1333,5 @@ invalid_kvpair:
13281333
RAISE_ERROR_KNOWN_LOCATION(p, PyExc_SyntaxError, a->lineno, a->end_col_offset - 1, a->end_lineno, -1, "':' expected after dictionary key") }
13291334
| expression ':' a='*' bitwise_or { RAISE_SYNTAX_ERROR_STARTING_FROM(a, "cannot use a starred expression in a dictionary value") }
13301335
| expression a=':' &('}'|',') {RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "expression expected after dictionary key and ':'") }
1336+
invalid_starred_expression:
1337+
| a='*' expression '=' b=expression { RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "cannot assign to iterable argument unpacking") }

Lib/test/test_syntax.py

+21
Original file line numberDiff line numberDiff line change
@@ -756,6 +756,27 @@
756756
>>> __debug__: int
757757
Traceback (most recent call last):
758758
SyntaxError: cannot assign to __debug__
759+
>>> f(a=)
760+
Traceback (most recent call last):
761+
SyntaxError: expected argument value expression
762+
>>> f(a, b, c=)
763+
Traceback (most recent call last):
764+
SyntaxError: expected argument value expression
765+
>>> f(a, b, c=, d)
766+
Traceback (most recent call last):
767+
SyntaxError: expected argument value expression
768+
>>> f(*args=[0])
769+
Traceback (most recent call last):
770+
SyntaxError: cannot assign to iterable argument unpacking
771+
>>> f(a, b, *args=[0])
772+
Traceback (most recent call last):
773+
SyntaxError: cannot assign to iterable argument unpacking
774+
>>> f(**kwargs={'a': 1})
775+
Traceback (most recent call last):
776+
SyntaxError: cannot assign to keyword argument unpacking
777+
>>> f(a, b, *args, **kwargs={'a': 1})
778+
Traceback (most recent call last):
779+
SyntaxError: cannot assign to keyword argument unpacking
759780
760781
761782
More set_context():
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Improve error messages when there's a syntax error with call arguments. The following three cases are covered:
2+
- No value is assigned to a named argument, eg ``foo(a=)``.
3+
- A value is assigned to a star argument, eg ``foo(*args=[0])``.
4+
- A value is assigned to a double-star keyword argument, eg ``foo(**kwarg={'a': 0})``.

0 commit comments

Comments
 (0)