cpython icon indicating copy to clipboard operation
cpython copied to clipboard

gh-115528: Update language reference for PEP 646

Open mrahtz opened this issue 1 year ago • 1 comments

There are two aims here:

  1. Make *args: *Ts valid.
  2. Make starred expression valid in the context of subscription, such that Generic[*a] and list[*a] are valid.

1 is straightforward, but 2 requires a bit more work:

The subscription rule relies on expression_list. But it's not as simple as changing expression_list, because it's also used by yield. For yield, although yield *a, b etc is valid, yield *a is not - the closest is yield *a,.

I also noticed that rules elsewhere that use this group of expression rules aren't reflective of the current grammar. Subscriptions, lists and sets all allow assignment expressions: Generic[x := 1], [x := 1] and {x := 1} all seem to be valid, at least in a REPL loop on Python 3.12.4.

Combining all this, this PR introduces a new rule flexible expression: assignment_expression | starred_expression, and changes expression_list, list_display and set_display to use this new rule. So all that fixes both starred expressions and assignment expressions valid in the contexts mentioned above.

(I've also made this whole group of expression rules less hopefully confusing by a) changing starred_expression to literally just be a single expression rather than potentially a list of expressions, and b) changed star_item to only be a starred expression.)

But for yield, things are different. Assignment expressions are not valid in the context of yield, and we need to express the stipulation that if the yield expression begins with a starred expression, there needs to be a comma. So I've introduced another new rule yield_list, that expresses this requirement.

  • Issue: gh-115528

📚 Documentation preview 📚: https://cpython-previews--121181.org.readthedocs.build/

mrahtz avatar Jun 30 '24 11:06 mrahtz

@JelleZijlstra Sorry this revision took a while - had a very busy few weeks at work, and it took a while to figure this out 😅

mrahtz avatar Jun 30 '24 12:06 mrahtz

Thanks @mrahtz for the PR, and @JelleZijlstra for merging it 🌮🎉.. I'm working now to backport this PR to: 3.12, 3.13. 🐍🍒⛏🤖

miss-islington-app[bot] avatar Sep 26 '24 19:09 miss-islington-app[bot]

GH-124632 is a backport of this pull request to the 3.13 branch.

bedevere-app[bot] avatar Sep 26 '24 19:09 bedevere-app[bot]

GH-124633 is a backport of this pull request to the 3.12 branch.

bedevere-app[bot] avatar Sep 26 '24 19:09 bedevere-app[bot]