Skip to content

Commit 0eeb02c

Browse files
[syntax-errors] Detect single starred expression assignment x = *y (#17624)
## Summary Part of #17412 Starred expressions cannot be used as values in assignment expressions. Add a new semantic syntax error to catch such instances. Note that we already have `ParseErrorType::InvalidStarredExpressionUsage` to catch some starred expression errors during parsing, but that does not cover top level assignment expressions. ## Test Plan - Added new inline tests for the new rule - Found some examples marked as "valid" in existing tests (`_ = *data`), which are not really valid (per this new rule) and updated them - There was an existing inline test - `assign_stmt_invalid_value_expr` which had instances of `*` expression which would be deemed invalid by this new rule. Converted these to tuples, so that they do not trigger this new rule.
1 parent f31b1c6 commit 0eeb02c

10 files changed

+612
-168
lines changed
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
x = *a and b
2-
x = *yield x
3-
x = *yield from x
4-
x = *lambda x: x
1+
x = (*a and b,)
2+
x = (42, *yield x)
3+
x = (42, *yield from x)
4+
x = (*lambda x: x,)
55
x = x := 1
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
_ = *[42]
2+
_ = *{42}
3+
_ = *list()
4+
_ = *(p + q)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
_ = 4
2+
_ = [4]
3+
_ = (*[1],)
4+
_ = *[1],

crates/ruff_python_parser/resources/valid/statement/assignment.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737

3838
foo = 42
3939

40-
[] = *data
41-
() = *data
40+
[] = (*data,)
41+
() = (*data,)
4242
a, b = ab
4343
a = b = c

crates/ruff_python_parser/src/parser/statement.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,10 +1127,10 @@ impl<'src> Parser<'src> {
11271127
// a + b
11281128

11291129
// test_err assign_stmt_invalid_value_expr
1130-
// x = *a and b
1131-
// x = *yield x
1132-
// x = *yield from x
1133-
// x = *lambda x: x
1130+
// x = (*a and b,)
1131+
// x = (42, *yield x)
1132+
// x = (42, *yield from x)
1133+
// x = (*lambda x: x,)
11341134
// x = x := 1
11351135

11361136
let mut value =

crates/ruff_python_parser/src/semantic_errors.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ impl SemanticSyntaxChecker {
9090
Self::duplicate_type_parameter_name(type_params, ctx);
9191
}
9292
}
93-
Stmt::Assign(ast::StmtAssign { targets, .. }) => {
93+
Stmt::Assign(ast::StmtAssign { targets, value, .. }) => {
9494
if let [Expr::Starred(ast::ExprStarred { range, .. })] = targets.as_slice() {
9595
// test_ok single_starred_assignment_target
9696
// (*a,) = (1,)
@@ -105,6 +105,19 @@ impl SemanticSyntaxChecker {
105105
*range,
106106
);
107107
}
108+
109+
// test_ok assign_stmt_starred_expr_value
110+
// _ = 4
111+
// _ = [4]
112+
// _ = (*[1],)
113+
// _ = *[1],
114+
115+
// test_err assign_stmt_starred_expr_value
116+
// _ = *[42]
117+
// _ = *{42}
118+
// _ = *list()
119+
// _ = *(p + q)
120+
Self::invalid_star_expression(value, ctx);
108121
}
109122
Stmt::Return(ast::StmtReturn { value, range }) => {
110123
if let Some(value) = value {

0 commit comments

Comments
 (0)