gh-90994: Improve error messages upon call arguments syntax errors (GH-96893)

This commit is contained in:
Lysandros Nikolaou 2022-11-21 00:15:05 +01:00 committed by GitHub
parent e13d1d9dda
commit 6d8da238cc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 3407 additions and 3129 deletions

View File

@ -957,6 +957,7 @@ kwargs[asdl_seq*]:
| ','.kwarg_or_double_starred+ | ','.kwarg_or_double_starred+
starred_expression[expr_ty]: starred_expression[expr_ty]:
| invalid_starred_expression
| '*' a=expression { _PyAST_Starred(a, Load, EXTRA) } | '*' a=expression { _PyAST_Starred(a, Load, EXTRA) }
kwarg_or_starred[KeywordOrStarred*]: kwarg_or_starred[KeywordOrStarred*]:
@ -1083,6 +1084,8 @@ invalid_arguments:
RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, _PyPegen_get_last_comprehension_item(PyPegen_last_item(b, comprehension_ty)), "Generator expression must be parenthesized") } RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, _PyPegen_get_last_comprehension_item(PyPegen_last_item(b, comprehension_ty)), "Generator expression must be parenthesized") }
| a=NAME b='=' expression for_if_clauses { | a=NAME b='=' expression for_if_clauses {
RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid syntax. Maybe you meant '==' or ':=' instead of '='?")} RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid syntax. Maybe you meant '==' or ':=' instead of '='?")}
| (args ',')? a=NAME b='=' &(',' | ')') {
RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "expected argument value expression")}
| a=args b=for_if_clauses { _PyPegen_nonparen_genexp_in_call(p, a, b) } | a=args b=for_if_clauses { _PyPegen_nonparen_genexp_in_call(p, a, b) }
| args ',' a=expression b=for_if_clauses { | args ',' a=expression b=for_if_clauses {
RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, _PyPegen_get_last_comprehension_item(PyPegen_last_item(b, comprehension_ty)), "Generator expression must be parenthesized") } 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:
| !(NAME '=') a=expression b='=' { | !(NAME '=') a=expression b='=' {
RAISE_SYNTAX_ERROR_KNOWN_RANGE( RAISE_SYNTAX_ERROR_KNOWN_RANGE(
a, b, "expression cannot contain assignment, perhaps you meant \"==\"?") } a, b, "expression cannot contain assignment, perhaps you meant \"==\"?") }
| a='**' expression '=' b=expression {
RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "cannot assign to keyword argument unpacking") }
# IMPORTANT: Note that the "_without_invalid" suffix causes the rule to not call invalid rules under it # IMPORTANT: Note that the "_without_invalid" suffix causes the rule to not call invalid rules under it
expression_without_invalid[expr_ty]: expression_without_invalid[expr_ty]:
@ -1328,3 +1333,5 @@ invalid_kvpair:
RAISE_ERROR_KNOWN_LOCATION(p, PyExc_SyntaxError, a->lineno, a->end_col_offset - 1, a->end_lineno, -1, "':' expected after dictionary key") } RAISE_ERROR_KNOWN_LOCATION(p, PyExc_SyntaxError, a->lineno, a->end_col_offset - 1, a->end_lineno, -1, "':' expected after dictionary key") }
| expression ':' a='*' bitwise_or { RAISE_SYNTAX_ERROR_STARTING_FROM(a, "cannot use a starred expression in a dictionary value") } | expression ':' a='*' bitwise_or { RAISE_SYNTAX_ERROR_STARTING_FROM(a, "cannot use a starred expression in a dictionary value") }
| expression a=':' &('}'|',') {RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "expression expected after dictionary key and ':'") } | expression a=':' &('}'|',') {RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "expression expected after dictionary key and ':'") }
invalid_starred_expression:
| a='*' expression '=' b=expression { RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "cannot assign to iterable argument unpacking") }

View File

@ -756,6 +756,27 @@ SyntaxError: cannot assign to __debug__
>>> __debug__: int >>> __debug__: int
Traceback (most recent call last): Traceback (most recent call last):
SyntaxError: cannot assign to __debug__ SyntaxError: cannot assign to __debug__
>>> f(a=)
Traceback (most recent call last):
SyntaxError: expected argument value expression
>>> f(a, b, c=)
Traceback (most recent call last):
SyntaxError: expected argument value expression
>>> f(a, b, c=, d)
Traceback (most recent call last):
SyntaxError: expected argument value expression
>>> f(*args=[0])
Traceback (most recent call last):
SyntaxError: cannot assign to iterable argument unpacking
>>> f(a, b, *args=[0])
Traceback (most recent call last):
SyntaxError: cannot assign to iterable argument unpacking
>>> f(**kwargs={'a': 1})
Traceback (most recent call last):
SyntaxError: cannot assign to keyword argument unpacking
>>> f(a, b, *args, **kwargs={'a': 1})
Traceback (most recent call last):
SyntaxError: cannot assign to keyword argument unpacking
More set_context(): More set_context():

View File

@ -0,0 +1,4 @@
Improve error messages when there's a syntax error with call arguments. The following three cases are covered:
- No value is assigned to a named argument, eg ``foo(a=)``.
- A value is assigned to a star argument, eg ``foo(*args=[0])``.
- A value is assigned to a double-star keyword argument, eg ``foo(**kwarg={'a': 0})``.

6504
Parser/parser.c generated

File diff suppressed because it is too large Load Diff