Skip to content

gh-156058: Disallow lone starred expressions at the grammar level - #156059

Draft
encukou wants to merge 1 commit into
python:mainfrom
encukou:no-lone-star-expression
Draft

gh-156058: Disallow lone starred expressions at the grammar level#156059
encukou wants to merge 1 commit into
python:mainfrom
encukou:no-lone-star-expression

Conversation

@encukou

@encukou encukou commented Aug 19, 2026

Copy link
Copy Markdown
Member

Statements like the following (where () can be any iterable -- or at the syntax level, any value):

  • *()
  • x = *()
  • yield *()
  • return *()
  • for _ in *(): ...

are not valid without a comma after the iterable.
These are allowed by python.gram and only rejected in the code generation step. This means that the documented Full Grammar specification is incomplete.

Change the grammar itself to disallow "lone" starred expressions.

This interfered with the invalid_legacy_expression rule, where in return i*i for _ in _ the i *i was parsed similarly to print *i, generating "can't use starred expression here" from *i (before getting to the "Missing parentheses in call" message instead of a generic "invalid syntax".
Exclude the star in invalid_legacy_expression to prevent this.

Statements like the following (where `()` can be any
iterable -- or at the syntax level, any value):

    *()

    x = *()

    yield *()

    return *()

    for _ in *(): ...

are not valid without a comma after the iterable.
These were allowed by `python.gram` and only rejected
in the codegen step. This means that the
documented "Full Grammar specification" was incomplete.

Change the grammar itself to disallow "lone" starred
expressions.

This interfered with the `invalid_legacy_expression`
rule, where in ``return i*i for _ in _`` the ``i *i``
was parsed similarly to ``print *i``, generating
"can't use starred expression here" from `*i` (before
getting to the "Missing parentheses in call" message
instead of a generic "invalid syntax".
Exclude the star in `invalid_legacy_expression` to
prevent this.

Co-authored-by: Blaise Pabon <blaise@gmail.com>

.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant