Skip to content
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

SyntaxError in Python 3.11 when unpacking sequences inside of [] #2321

Closed
ned2 opened this issue Jan 29, 2023 · 10 comments · Fixed by #3089
Closed

SyntaxError in Python 3.11 when unpacking sequences inside of [] #2321

ned2 opened this issue Jan 29, 2023 · 10 comments · Fixed by #3089
Labels
bug Something isn't working

Comments

@ned2
Copy link

ned2 commented Jan 29, 2023

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
Copy link
Author

ned2 commented Jan 29, 2023

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

@charliermarsh
Copy link
Member

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

@ned2
Copy link
Author

ned2 commented Jan 31, 2023

Done: RustPython/RustPython#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
Copy link
Author

ned2 commented Jan 31, 2023

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.

@youknowone
Copy link
Contributor

The parser parts are resolved
RustPython/RustPython#4531
RustPython/RustPython#4532

@LeeeeT
Copy link

LeeeeT commented Feb 23, 2023

@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
Copy link

LeeeeT commented Feb 23, 2023

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?

@charliermarsh
Copy link
Member

charliermarsh commented Feb 23, 2023

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

@charliermarsh
Copy link
Member

@LeeeeT - Nevermind, I filed as #3170. ExceptionGroup was fixed via #3167.

@LeeeeT
Copy link

LeeeeT commented Feb 23, 2023

Thank you for fixing it so quickly! ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants