ruff
ruff copied to clipboard
SyntaxError in Python 3.11 when unpacking sequences inside of []
Ruff version: 0.0.237 Python version: 3.11.1
Command run:
ruff check --isolated code.py
Contents of code.py
:
my_dict = {}
my_dict[*"ab"] = 1
This is valid Python 3.11 code, with the string 'ab'
being unpacked into a tuple, resulting the in they key with tuple ('a','b')
being assigned a value of 1
. The above invocation of ruff however throws this error:
E999 SyntaxError: invalid syntax. Got unexpected token '*'
The snippet itself doesn't work in Python 3.9 or 3.10 (throwing SyntaxErrors), so this seems like a syntax enhancement in 3.11 that should be supported. That said, I can't find where this is documented as a change in 3.11. All I can see is this reference to Starred unpacking expressions can now be used in for statements. , which references this CPython issue, but that looks to be a different context for using *
, and was only documenting behaviour that changed in 3.9.
Oh just occurred to me, would this be better submitted to the RustPython project?
Oh interesting! Yes, I think this would be at-home on RustPython. Do you mind filing there?
Done: https://github.com/RustPython/RustPython/issues/4479
As I mention in that issue, I wonder if it's related to the new except*
syntax that was introduced in 3.11 for ExceptionGroup
s. The change I picked up on above doesn't seem to be documented, whereas except*
is - maybe it's a side-effect of the change.
Also, maybe this issue should then should really be about the fact that ruff doesn't support the except*
syntax of 3.11 required to use ExceptionGroup
s, which is going to be more impactful to users.
Ok so it turns out this is an expected change in 3.11, but it wasn't called out in the changelog. See the section Change 1: Star Expressions in Indexes in PEP 646, which was accepted for 3.11.
The parser parts are resolved https://github.com/RustPython/RustPython/pull/4531 https://github.com/RustPython/RustPython/pull/4532
@charliermarsh, I'm still getting an error when trying to annotate *args
as a type variable tuple:
def f(*args: *tuple[int]) -> None: ...
error: Failed to parse file.py: invalid syntax. Got unexpected token '*' at line 1 column 14
file.py:1:15: E999 SyntaxError: invalid syntax. Got unexpected token '*'
Although, this is a valid syntax according to PEP 646, and it passes static type checking using Pyright.
ruff 0.0.252
Moreover, Ruff seems not to support exception groups.
print(ExceptionGroup)
file.py:1:7: F821 Undefined name `ExceptionGroup`
Should I create a separate issue for this?
Thanks @LeeeeT, can you file a separate issue for the *tuple
thing?
@LeeeeT - Nevermind, I filed as https://github.com/charliermarsh/ruff/issues/3170. ExceptionGroup
was fixed via #3167.
Thank you for fixing it so quickly! ❤️