Skip to content

Comments

[syntax-errors] Starred expressions in return, yield, and for#17134

Merged
ntBre merged 4 commits intomainfrom
brent/syn-single-stars
Apr 2, 2025
Merged

[syntax-errors] Starred expressions in return, yield, and for#17134
ntBre merged 4 commits intomainfrom
brent/syn-single-stars

Conversation

@ntBre
Copy link
Contributor

@ntBre ntBre commented Apr 1, 2025

Summary

Fixes #16520 by flagging single, starred expressions in return, yield, and
for statements.

I thought yield from would also be included here, but that error is emitted by
the CPython parser:

>>> ast.parse("def f(): yield from *x")
Traceback (most recent call last):
  File "<python-input-214>", line 1, in <module>
    ast.parse("def f(): yield from *x")
    ~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.13/ast.py", line 54, in parse
    return compile(source, filename, mode, flags,
                   _feature_version=feature_version, optimize=optimize)
  File "<unknown>", line 1
    def f(): yield from *x
                        ^
SyntaxError: invalid syntax

And we also already catch it in our parser.

Test Plan

New inline tests and updates to existing tests.

ntBre added 4 commits April 1, 2025 15:55
Summary
--

Fixes #16520 by flagging single, starred expressions in `return`, `yield`, and
`for` statements.

I thought `yield from` would also be included here, but that error is emitted by
the CPython parser:

```pycon
>>> ast.parse("def f(): yield from *x")
Traceback (most recent call last):
  File "<python-input-214>", line 1, in <module>
    ast.parse("def f(): yield from *x")
    ~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.13/ast.py", line 54, in parse
    return compile(source, filename, mode, flags,
                   _feature_version=feature_version, optimize=optimize)
  File "<unknown>", line 1
    def f(): yield from *x
                        ^
SyntaxError: invalid syntax
```

And we also already catch it in our parser.

Test Plan
--

New inline tests.
@ntBre ntBre added rule Implementing or modifying a lint rule preview Related to preview mode features labels Apr 1, 2025
@github-actions
Copy link
Contributor

github-actions bot commented Apr 1, 2025

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

Formatter (stable)

✅ ecosystem check detected no format changes.

Formatter (preview)

✅ ecosystem check detected no format changes.

@ntBre ntBre merged commit d455932 into main Apr 2, 2025
22 checks passed
@ntBre ntBre deleted the brent/syn-single-stars branch April 2, 2025 12:38
maxmynter pushed a commit to maxmynter/ruff that referenced this pull request Apr 3, 2025
…-sh#17134)

Summary
--

Fixes astral-sh#16520 by flagging single,
starred expressions in `return`, `yield`, and
`for` statements.

I thought `yield from` would also be included here, but that error is
emitted by
the CPython parser:

```pycon
>>> ast.parse("def f(): yield from *x")
Traceback (most recent call last):
  File "<python-input-214>", line 1, in <module>
    ast.parse("def f(): yield from *x")
    ~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.13/ast.py", line 54, in parse
    return compile(source, filename, mode, flags,
                   _feature_version=feature_version, optimize=optimize)
  File "<unknown>", line 1
    def f(): yield from *x
                        ^
SyntaxError: invalid syntax
```

And we also already catch it in our parser.

Test Plan
--

New inline tests and updates to existing tests.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

preview Related to preview mode features rule Implementing or modifying a lint rule

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Flag starred expressions in return and yield

2 participants