Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Two CPython 3.11 syntax changes involving * operator are unsupported #4479

Open
@ned2

Description

@ned2

Feature

In Python 3.11, the except* was added for working with ExceptionGroups. The availability of ExceptionGroup itself will depend on #4247, but in the meantime, the following snippet is valid CPython 3.11 that currently throws a SyntaxError in RustPython:

try:
    print("yay")
except* Exception as e:
    print("oh no")

I discovered this while digging into some code that ruff cannot handle, which on the surface looks like a slightly different issue. The following snippet is valid CPython 3.11 but also throws a SyntaxError in RustPython:

my_dict = {}
my_dict[*"ab"] = 1

The expected behaviour is that the string should be unpacked and the value of the dict with key ('a','b') set to 1. In case that feels a little contrived, here's what I was doing at the time:

import typing
values = ["dog", "cat"]
Pets = typing.Literal[*values]

Python Documentation

The first incompatibility issue is clearly documented here, and called out under New syntax features in What’s New In Python 3.11.

However the second incompatibility is not documented in the changelog, and I did confirm that this was new in CPython 3.11, as the snippet throws a syntax error in 3.10.

I wonder if this was a (nice?) side-effect of the changes that were introduced to make except* work. There is a seemingly related precedent for this, where using * to unpack without parentheses in for statements was enabled via the PEG parser in CPython 3.9, but was not noticed then and only documented in 3.11.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-vmArea: virtual machine

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions