Skip to content

gh-109120: Fix handle of incorrect star expressions #117444

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 5 commits into from
Apr 2, 2024
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
6 changes: 4 additions & 2 deletions Grammar/python.gram
Original file line number Diff line number Diff line change
Expand Up @@ -1013,6 +1013,7 @@ kwargs[asdl_seq*]:
starred_expression[expr_ty]:
| invalid_starred_expression
| '*' a=expression { _PyAST_Starred(a, Load, EXTRA) }
| '*' { RAISE_SYNTAX_ERROR("Invalid star expression") }

kwarg_or_starred[KeywordOrStarred*]:
| invalid_kwarg
Expand Down Expand Up @@ -1133,8 +1134,8 @@ func_type_comment[Token*]:

# From here on, there are rules for invalid syntax with specialised error messages
invalid_arguments:
| ((','.(starred_expression | ( assignment_expression | expression !':=') !'=')+ ',' kwargs) | kwargs) ',' b='*' {
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(b, "iterable argument unpacking follows keyword argument unpacking") }
| ((','.(starred_expression | ( assignment_expression | expression !':=') !'=')+ ',' kwargs) | kwargs) a=',' ','.(starred_expression !'=')+ {
RAISE_SYNTAX_ERROR_STARTING_FROM(a, "iterable argument unpacking follows keyword argument unpacking") }
| a=expression b=for_if_clauses ',' [args | expression 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") }
| a=NAME b='=' expression for_if_clauses {
Expand Down Expand Up @@ -1396,6 +1397,7 @@ invalid_kvpair:
| 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") }

invalid_replacement_field:
| '{' a='=' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "f-string: valid expression required before '='") }
| '{' a='!' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "f-string: valid expression required before '!'") }
Expand Down
26 changes: 19 additions & 7 deletions Lib/test/test_syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -1911,22 +1911,22 @@
>>> A[*(1:2)]
Traceback (most recent call last):
...
SyntaxError: invalid syntax
SyntaxError: Invalid star expression
>>> A[*(1:2)] = 1
Traceback (most recent call last):
...
SyntaxError: invalid syntax
SyntaxError: Invalid star expression
>>> del A[*(1:2)]
Traceback (most recent call last):
...
SyntaxError: invalid syntax
SyntaxError: Invalid star expression
A[*:] and A[:*]
>>> A[*:]
Traceback (most recent call last):
...
SyntaxError: invalid syntax
SyntaxError: Invalid star expression
>>> A[:*]
Traceback (most recent call last):
...
Expand All @@ -1937,7 +1937,7 @@
>>> A[*]
Traceback (most recent call last):
...
SyntaxError: invalid syntax
SyntaxError: Invalid star expression
A[**]
Expand Down Expand Up @@ -2081,11 +2081,23 @@ def f(x: *b)
>>> f(**x, *)
Traceback (most recent call last):
SyntaxError: iterable argument unpacking follows keyword argument unpacking
SyntaxError: Invalid star expression
>>> f(x, *:)
Traceback (most recent call last):
SyntaxError: invalid syntax
SyntaxError: Invalid star expression
>>> f(x, *)
Traceback (most recent call last):
SyntaxError: Invalid star expression
>>> f(x = 5, *)
Traceback (most recent call last):
SyntaxError: Invalid star expression
>>> f(x = 5, *:)
Traceback (most recent call last):
SyntaxError: Invalid star expression
"""

import re
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Added handle of incorrect star expressions, e.g ``f(3, *)``. Patch by
Grigoryev Semyon
Loading