-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Flag starred expressions in return
and yield
#16520
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
Comments
Python's Python 3.11.4 (main, Sep 30 2023, 10:54:38) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import ast
>>> ast.parse('def f(): return *x')
<ast.Module object at 0x7d5c2433d0> which indicates to me that this syntax error is detected by CPython's compiler rather than CPython's parser. That means that this issue is a sub-issue of #11934. It doesn't mean that we shouldn't detect the syntax error (we absolutely should!). But it does mean that we may want a way of suppressing the diagnostic internally so that we can continue to test our parser directly against CPython's parser using e.g. the |
Thanks @AlexWaygood That would suggest that this should be implemented as a semantic syntax error, outside the parser. |
I'll start moving some of the other errors from #6591 to #11934 too. I think the Python 3.12.9 (main, Feb 12 2025, 14:50:50) [Clang 19.1.6 ] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import ast
>>> ast.parse("""
... a=5
...
... def f():
... try:
... pass
... except:
... global a
... else:
... print(a)
... """)
<ast.Module object at 0x79c0c0e06d10> |
Good call. Some of the existing errors listed in #11934 and its sub-issues may also have been introduced in recent versions of CPython; we'll need to check when implementing those semantic syntax errors whether they should be emitted for all target versions or not |
(I think from Python 3.9, you can test the CPython parser directly using |
This issue may also include Python 3.13.1 (main, Dec 4 2024, 18:05:56) [GCC 14.2.1 20240910] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import ast
>>> ast.parse("for _ in *rest:...")
<ast.Module object at 0x7cd78d9bb450>
>>> for _ in *rest: ...
File "<python-input-2>", line 1
for _ in *rest: ...
^^^^^
SyntaxError: can't use starred expression here
>>> |
before Python 3.9 Summary -- This PR reuses a slightly modified version of the `check_tuple_unpacking` method added for detecting unpacking in `return` and `yield` statements to detect the same issue in the iterator clause of `for` loops. I ran into the same issue with a bare `for x in *rest: ...` example (invalid on every Python) and added it as a comment on #16520. I considered just making this an additional `StarTupleKind` variant as well, but this change was in a different version of Python, so I kept it separate. Test Plan -- New inline tests.
…fore Python 3.9 (#16558) Summary -- This PR reuses a slightly modified version of the `check_tuple_unpacking` method added for detecting unpacking in `return` and `yield` statements to detect the same issue in the iterator clause of `for` loops. I ran into the same issue with a bare `for x in *rest: ...` example (invalid even on Python 3.13) and added it as a comment on #16520. I considered just making this an additional `StarTupleKind` variant as well, but this change was in a different version of Python, so I kept it separate. Test Plan -- New inline tests.
Summary -- Fixes #16520 by flagging single, starred expressions in `return`, `yield`, and `for` statements. I thought `yield from` would also be included here, but that error is emitted by the CPython parser: ```pycon >>> ast.parse("def f(): yield from *x") Traceback (most recent call last): File "<python-input-214>", line 1, in <module> ast.parse("def f(): yield from *x") ~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/lib/python3.13/ast.py", line 54, in parse return compile(source, filename, mode, flags, _feature_version=feature_version, optimize=optimize) File "<unknown>", line 1 def f(): yield from *x ^ SyntaxError: invalid syntax ``` And we also already catch it in our parser. Test Plan -- New inline tests.
…-sh#17134) Summary -- Fixes astral-sh#16520 by flagging single, starred expressions in `return`, `yield`, and `for` statements. I thought `yield from` would also be included here, but that error is emitted by the CPython parser: ```pycon >>> ast.parse("def f(): yield from *x") Traceback (most recent call last): File "<python-input-214>", line 1, in <module> ast.parse("def f(): yield from *x") ~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/lib/python3.13/ast.py", line 54, in parse return compile(source, filename, mode, flags, _feature_version=feature_version, optimize=optimize) File "<unknown>", line 1 def f(): yield from *x ^ SyntaxError: invalid syntax ``` And we also already catch it in our parser. Test Plan -- New inline tests and updates to existing tests.
Both of these are invalid on every Python version I've tried and should be flagged as
ParseError
s in the parser:They result in
SyntaxError: can't use starred expression here
on 3.8 and 3.13 and the less helpfulSyntaxError: invalid syntax
on 3.7.This is separate from the related case in #16485 where these are part of a tuple, which is allowed after 3.8.
Originally posted by @ntBre in #16485 (comment)
The text was updated successfully, but these errors were encountered: