Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion crates/ruff_linter/src/checkers/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,8 @@ impl SemanticSyntaxContext for Checker<'_> {
| SemanticSyntaxErrorKind::MultipleCaseAssignment(_)
| SemanticSyntaxErrorKind::IrrefutableCasePattern(_)
| SemanticSyntaxErrorKind::SingleStarredAssignment
| SemanticSyntaxErrorKind::WriteToDebug(_) => {
| SemanticSyntaxErrorKind::WriteToDebug(_)
| SemanticSyntaxErrorKind::InvalidStarExpression => {
if self.settings.preview.is_enabled() {
self.semantic_errors.borrow_mut().push(error);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
for _ in *x: ...
for *x in xs: ...
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
def f(): return *x
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
def f(): yield *x
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
def f(): yield (*x,)
def f(): return (*x,)
for _ in (*x,): ...
for (*x,) in xs: ...
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,5 @@
yield (x, y)
yield x == y
yield (x := 1)
yield *y
yield x, *y
yield *x,
yield *x | y
10 changes: 0 additions & 10 deletions crates/ruff_python_parser/resources/valid/statement/for.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,9 @@
for (a, b) in iter:
pass

for target in *x.attr:
pass

for target in [1, 2]:
pass

for *target in a, b, c,:
pass
else:
pass

for target in *x | y: ...
for target in *await x: ...
for target in await x: ...
for target in lambda x: x: ...
for target in x if True else y: ...
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
return
return x
return *x
return *x | y
return *x, *y
return (x := 1)
return None
Expand Down
53 changes: 53 additions & 0 deletions crates/ruff_python_parser/src/semantic_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,42 @@ impl SemanticSyntaxChecker {
);
}
}
Stmt::Return(ast::StmtReturn {
value: Some(value), ..
}) => {
// test_err single_star_return
// def f(): return *x
Self::invalid_star_expression(value, ctx);
}
Stmt::For(ast::StmtFor { target, iter, .. }) => {
// test_err single_star_for
// for _ in *x: ...
// for *x in xs: ...
Self::invalid_star_expression(target, ctx);
Self::invalid_star_expression(iter, ctx);
}
_ => {}
}

Self::debug_shadowing(stmt, ctx);
}

/// Emit a [`SemanticSyntaxErrorKind::InvalidStarExpression`] if `expr` is starred.
fn invalid_star_expression<Ctx: SemanticSyntaxContext>(expr: &Expr, ctx: &Ctx) {
// test_ok single_star_in_tuple
// def f(): yield (*x,)
// def f(): return (*x,)
// for _ in (*x,): ...
// for (*x,) in xs: ...
if expr.is_starred_expr() {
Self::add_error(
ctx,
SemanticSyntaxErrorKind::InvalidStarExpression,
expr.range(),
);
}
}

/// Check for [`SemanticSyntaxErrorKind::WriteToDebug`] in `stmt`.
fn debug_shadowing<Ctx: SemanticSyntaxContext>(stmt: &ast::Stmt, ctx: &Ctx) {
match stmt {
Expand Down Expand Up @@ -368,6 +398,13 @@ impl SemanticSyntaxChecker {
};
}
}
Expr::Yield(ast::ExprYield {
value: Some(value), ..
}) => {
// test_err single_star_yield
// def f(): yield *x
Self::invalid_star_expression(value, ctx);
}
_ => {}
}
}
Expand Down Expand Up @@ -462,6 +499,9 @@ impl Display for SemanticSyntaxError {
write!(f, "cannot delete `__debug__` on Python {python_version} (syntax was removed in 3.9)")
}
},
SemanticSyntaxErrorKind::InvalidStarExpression => {
f.write_str("can't use starred expression here")
}
}
}
}
Expand Down Expand Up @@ -575,6 +615,19 @@ pub enum SemanticSyntaxErrorKind {
///
/// [BPO 45000]: https://github.com/python/cpython/issues/89163
WriteToDebug(WriteToDebugKind),

/// Represents the use of a starred expression in an invalid location, such as a `return` or
/// `yield` statement.
///
/// ## Examples
///
/// ```python
/// def f(): return *x
/// def f(): yield *x
/// for _ in *x: ...
/// for *x in xs: ...
/// ```
InvalidStarExpression,
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,14 @@ Module(
4 | yield *x and y, z
| ^^^^^^^ Syntax Error: Boolean expression cannot be used here
|


## Semantic Syntax Errors

|
1 | # Cannot use starred expression here
2 | yield (*x)
| ^^ Syntax Error: can't use starred expression here
3 |
4 | yield *x and y, z
|
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
---
source: crates/ruff_python_parser/tests/fixtures.rs
input_file: crates/ruff_python_parser/resources/inline/err/for_stmt_invalid_iter_expr.py
snapshot_kind: text
---
## AST

Expand Down Expand Up @@ -182,3 +181,13 @@ Module(
3 | for target in x := 1: ...
| ^ Syntax Error: Invalid annotated assignment target
|


## Semantic Syntax Errors

|
1 | for x in *a and b: ...
| ^^^^^^^^ Syntax Error: can't use starred expression here
2 | for x in yield a: ...
3 | for target in x := 1: ...
|
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
---
source: crates/ruff_python_parser/tests/fixtures.rs
input_file: crates/ruff_python_parser/resources/inline/err/for_stmt_invalid_target.py
snapshot_kind: text
---
## AST

Expand Down Expand Up @@ -462,3 +461,25 @@ Module(
7 | for [x, 1, y, *["a"]] in z: ...
| ^^^ Syntax Error: Invalid assignment target
|


## Semantic Syntax Errors

|
1 | for 1 in x: ...
2 | for "a" in x: ...
3 | for *x and y in z: ...
| ^^^^^^^^ Syntax Error: can't use starred expression here
4 | for *x | y in z: ...
5 | for await x in z: ...
|


|
2 | for "a" in x: ...
3 | for *x and y in z: ...
4 | for *x | y in z: ...
| ^^^^^^ Syntax Error: can't use starred expression here
5 | for await x in z: ...
6 | for yield x in y: ...
|
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
---
source: crates/ruff_python_parser/tests/fixtures.rs
input_file: crates/ruff_python_parser/resources/inline/err/return_stmt_invalid_expr.py
snapshot_kind: text
---
## AST

Expand Down Expand Up @@ -181,3 +180,21 @@ Module(
5 | return *x and y
| ^^^^^^^ Syntax Error: Boolean expression cannot be used here
|


## Semantic Syntax Errors

|
1 | return *
| ^ Syntax Error: can't use starred expression here
2 | return yield x
3 | return yield from x
|


|
3 | return yield from x
4 | return x := 1
5 | return *x and y
| ^^^^^^^^ Syntax Error: can't use starred expression here
|
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
---
source: crates/ruff_python_parser/tests/fixtures.rs
input_file: crates/ruff_python_parser/resources/inline/err/single_star_for.py
---
## AST

```
Module(
ModModule {
range: 0..35,
body: [
For(
StmtFor {
range: 0..16,
is_async: false,
target: Name(
ExprName {
range: 4..5,
id: Name("_"),
ctx: Store,
},
),
iter: Starred(
ExprStarred {
range: 9..11,
value: Name(
ExprName {
range: 10..11,
id: Name("x"),
ctx: Load,
},
),
ctx: Load,
},
),
body: [
Expr(
StmtExpr {
range: 13..16,
value: EllipsisLiteral(
ExprEllipsisLiteral {
range: 13..16,
},
),
},
),
],
orelse: [],
},
),
For(
StmtFor {
range: 17..34,
is_async: false,
target: Starred(
ExprStarred {
range: 21..23,
value: Name(
ExprName {
range: 22..23,
id: Name("x"),
ctx: Store,
},
),
ctx: Store,
},
),
iter: Name(
ExprName {
range: 27..29,
id: Name("xs"),
ctx: Load,
},
),
body: [
Expr(
StmtExpr {
range: 31..34,
value: EllipsisLiteral(
ExprEllipsisLiteral {
range: 31..34,
},
),
},
),
],
orelse: [],
},
),
],
},
)
```
## Semantic Syntax Errors

|
1 | for _ in *x: ...
| ^^ Syntax Error: can't use starred expression here
2 | for *x in xs: ...
|


|
1 | for _ in *x: ...
2 | for *x in xs: ...
| ^^ Syntax Error: can't use starred expression here
|
Loading
Loading