ruff icon indicating copy to clipboard operation
ruff copied to clipboard

SyntaxError in Python 3.11 when unpacking sequences inside of []

Open ned2 opened this issue 2 years ago • 4 comments

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.

ned2 avatar Jan 29 '23 09:01 ned2

Oh just occurred to me, would this be better submitted to the RustPython project?

ned2 avatar Jan 29 '23 09:01 ned2

Oh interesting! Yes, I think this would be at-home on RustPython. Do you mind filing there?

charliermarsh avatar Jan 29 '23 16:01 charliermarsh

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 ExceptionGroups. 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 ExceptionGroups, which is going to be more impactful to users.

ned2 avatar Jan 31 '23 09:01 ned2

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.

ned2 avatar Jan 31 '23 10:01 ned2

The parser parts are resolved https://github.com/RustPython/RustPython/pull/4531 https://github.com/RustPython/RustPython/pull/4532

youknowone avatar Feb 21 '23 17:02 youknowone

@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

LeeeeT avatar Feb 23 '23 07:02 LeeeeT

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?

LeeeeT avatar Feb 23 '23 08:02 LeeeeT

Thanks @LeeeeT, can you file a separate issue for the *tuple thing?

charliermarsh avatar Feb 23 '23 12:02 charliermarsh

@LeeeeT - Nevermind, I filed as https://github.com/charliermarsh/ruff/issues/3170. ExceptionGroup was fixed via #3167.

charliermarsh avatar Feb 23 '23 13:02 charliermarsh

Thank you for fixing it so quickly! ❤️

LeeeeT avatar Feb 23 '23 13:02 LeeeeT