python / python/cpython

with statement star target

Open
#135,617 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

docs topic-parser
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

Documentation

https://docs.python.org/3/reference/simple_stmts.html#grammar-token-python-grammar-target

According to the docs, the grammar for the with statement allows:

with_stmt                          ::= "with" with_stmt_contents
with_stmt_contents           ::= with_item
with_item                          ::= expression "as" target
target                                ::= identifier  | "*" target

from my understanding that would allow a python syntax like this:


class Test:
    def __enter__(self):
        return (1, 2)

    def __exit__(self, exc_type, exc_value, traceback):
        pass


with Test() as *mambo:
    print(mambo, type(mambo))

But for me with python version 3.12.8

I get a syntax error:

  File "/home/gringo/accessia/main/controller_start/test.py", line 9
    with Test() as *mambo:
                   ^^^^^^
SyntaxError: starred assignment target must be in a list or tuple

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the referenced Python language-reference section on the with-statement grammar and reproduce the example on Python 3.12.8. Compare the documented target grammar with the reported SyntaxError. Done means the documentation and observed behavior are reconciled, with the relevant wording or grammar clarified.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
documentation
Issue type
Documentation
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.