bpo-40631: Single-element starred elements in parenthesised expressions cannot be parsed#24014
bpo-40631: Single-element starred elements in parenthesised expressions cannot be parsed#24014pablogsal wants to merge 1 commit into
Conversation
…ns cannot be parsed
| file[mod_ty]: a=[statements] ENDMARKER { _PyPegen_make_module(p, a) } | ||
| interactive[mod_ty]: a=statement_newline { Interactive(a, p->arena) } | ||
| eval[mod_ty]: a=expressions NEWLINE* ENDMARKER { Expression(a, p->arena) } | ||
| eval[mod_ty]: a=star_expressions NEWLINE* ENDMARKER { Expression(a, p->arena) } |
There was a problem hiding this comment.
@lysnikolaou Do you remember why this was expressions and not star_expressions? I cannot see any edge case that makes this change incorrect but now I am curious.
There was a problem hiding this comment.
I think that this was for consistency with 3.8. While 3.8 and 3.9 currently fail at parser level for eval('*a'), this patch fails at bytecode compiler level.
There was a problem hiding this comment.
We could make that fail at the parser level if you want but not sure if is worth it because we would need to factor that into some rule and I don't know if we can reuse the rest. What's your opinion?
There was a problem hiding this comment.
Wouldn't leaving it as is work?
The reason I'm pushing for the error to be in parser level is due to the quality of the error message. Here's an example:
Without this patch:
➜ cpython git:(bpo-40631) ✗ cat a.py
a = [1, 2, 3]
eval('''
*a''')
➜ cpython git:(bpo-40631) ✗ python a.py
Traceback (most recent call last):
File "/home/lysnikolaou/repos/cpython/a.py", line 2, in <module>
eval('''
File "<string>", line 4
*a
^
SyntaxError: invalid syntax
With this patch:
➜ cpython git:(bpo-40631) ✗ cat a.py
a = [1, 2, 3]
eval('''
*a''')
➜ cpython git:(bpo-40631) ✗ ./python a.py
Traceback (most recent call last):
File "/home/lysnikolaou/repos/cpython/a.py", line 2, in <module>
eval('''
File "<string>", line 4
SyntaxError: can't use starred expression here
The first one makes a much better job of making clear what the error is I feel. But if it's too much effort and leaving it as is doesn't work, I'm okay with it.
There was a problem hiding this comment.
I very much agree. I have closed the PR based on this discussion and Guido's request. Thanks for the examples and the comment.
|
|
||
| star_element[expr_ty]: | ||
| | '*' a=bitwise_or { _Py_Starred(a, Load, EXTRA) } | ||
| | '(' '*' a=bitwise_or ')' { _Py_Starred(a, Load, EXTRA) } |
There was a problem hiding this comment.
This patch makes (*x), y valid, but with multiple layers of parentheses (((*x))), y is still considered invalid.
|
I am closing this as per Guido's request in the issue tracker. |
…
https://bugs.python.org/issue40631