-
-
Notifications
You must be signed in to change notification settings - Fork 30.6k
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
PEG Parser: Cannot used starred expression in parenthesised expr #84811
Comments
The new PEG parser fails when a parenthesised expression with a single child (a group) contains a starred expression. Example: ╰─ ./python.exe
Python 3.9.0a6+ (heads/master-dirty:4a12d12186, May 15 2020, 14:53:45)
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import ast
>>> ast.parse('(*a)')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/lysnikolaou/Repositories/cpython/Lib/ast.py", line 50, in parse
return compile(source, filename, mode, flags,
File "<unknown>", line 1
(*a)
^
SyntaxError: invalid syntax This was valid syntax up until now: ╰─ ./python.exe -X oldparser
Python 3.9.0a6+ (heads/master-dirty:4a12d12186, May 15 2020, 14:53:45)
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import ast
>>> ast.dump(ast.parse('(*a)'))
"Module(body=[Expr(value=Starred(value=Name(id='a', ctx=Load()), ctx=Load()))], type_ignores=[])" |
Whoops, false alarm. It's just that we moved the check for invalid starred expressions to the parser, while it previously was in the compiler. ╰─ ./python.exe -X oldparser
Python 3.9.0a6+ (heads/master-dirty:003708bcf8, May 15 2020, 15:08:21)
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> (*a)
File "<stdin>", line 1
SyntaxError: can't use starred expression here Sorry for the noise! |
Well, there is actually a bug: root@f1b4a742d8fc:/# python3.9
Python 3.9.1 (default, Dec 8 2020, 03:24:52)
[GCC 7.5.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> x = [1, 2]
>>> y = 3
>>> *x, y
(1, 2, 3)
>>> (*x), y
File "<stdin>", line 1
(*x), y
^
SyntaxError: can't use starred expression here
root@f1b4a742d8fc:/# python3.8
Python 3.8.6 (default, Oct 6 2020, 03:22:36)
[GCC 7.5.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> x = [1, 2]
>>> y = 3
>>> *x, y
(1, 2, 3)
>>> (*x), y
(1, 2, 3) This is different from the previous message where the starred expression is "alone" (and thus invalid). Since this bug happens in 3.9 but not in 3.8, it might be due to the PEG parser. Also,
root@f1b4a742d8fc:/# python3.9
Python 3.9.1 (default, Dec 8 2020, 03:24:52)
[GCC 7.5.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> del *x
File "<stdin>", line 1
del *x
^
SyntaxError: cannot delete starred
>>> del (*x)
File "<stdin>", line 1
del (*x)
^
SyntaxError: can't use starred expression here The latter case should also report "SyntaxError: cannot delete starred". |
Honestly this seems like a bug in 3.8 to me (if it indeed behaves like this): >>> (*x), y
(1, 2, 3) Every time I mistakenly tried (*x) I really meant (*x,), so it's surprising that (*x), y would be interpreted as (*x, y) rather than flagging (*x) as an error. Please don't "fix" this even if it is a regression. |
It makes sense to me to be able to do |
Parenthesis can be added around expression. But |
Also the current behavior allows |
I disagree. *a is not an expression, so the normal rules for parenthesizing those don't apply. I've always thought of *a as a feature of the "comma" syntax. Note too that (**a) is not valid and never was. Also note that 2.7 doesn't support f((*a)). In fact 3.4 doesn't either -- but 3.5 does. I don't know how this slipped into earlier Python 3 versions -- apparently there aren't tests for this, and it's not used in popular 3rd code either, or we would have found out when we first implemented PEP-617. Most likely it's due to the general problem where the parser would just accept parenthesized stuff in various places where it shouldn't (e.g. also f(a=1) could be spelled as f((a)=1) -- this was fixed in 3.8).
I agree. |
Yup, this all sounds much more reasonable. Thanks for the explanation, Guido.
This is allowed in 3.9.1 and 3.10.0a2, but not allowed in 3.9.0 and 3.10.0a1. I'll work on finding out when this got messed up and fix it. |
Apparently is commit bca7014 (HEAD)
~/github/python/master v3.10.0a2~61 9s ~/github/python/master v3.10.0a2~61 ~/github/python/master remotes/welikeparsers/master The following modules found by detect_modules() in setup.py, have been ~/github/python/master remotes/welikeparsers/master |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: