[syntax-errors]: multiple-starred-expressions (F622) (#20243)

<!--
Thank you for contributing to Ruff/ty! To help us out with reviewing,
please consider the following:

- Does this pull request include a summary of the change? (See below.)
- Does this pull request include a descriptive title? (Please prefix
with `[ty]` for ty pull
  requests.)
- Does this pull request include references to any relevant issues?
-->

## Summary

This PR implements
https://docs.astral.sh/ruff/rules/multiple-starred-expressions/ as a
semantic syntax error

## Test Plan

 I have added inline tests as directed in #17412

---------

Signed-off-by: 11happy <soni5happy@gmail.com>
Co-authored-by: Brent Westbrook <36778786+ntBre@users.noreply.github.com>
This commit is contained in:
Bhuminjay Soni 2025-09-25 01:02:55 +05:30 committed by GitHub
parent 73b4b1ed17
commit e6073d0cca
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 783 additions and 20 deletions

View file

@ -389,6 +389,40 @@ impl SemanticSyntaxChecker {
}
}
fn multiple_star_expression<Ctx: SemanticSyntaxContext>(
ctx: &Ctx,
expr_ctx: ExprContext,
elts: &[Expr],
range: TextRange,
) {
if expr_ctx.is_store() {
let mut has_starred = false;
for elt in elts {
if elt.is_starred_expr() {
if has_starred {
// test_err multiple_starred_assignment_target
// (*a, *b) = (1, 2)
// [*a, *b] = (1, 2)
// (*a, *b, c) = (1, 2, 3)
// [*a, *b, c] = (1, 2, 3)
// (*a, *b, (*c, *d)) = (1, 2)
// test_ok multiple_starred_assignment_target
// (*a, b) = (1, 2)
// (*_, normed), *_ = [(1,), 2]
Self::add_error(
ctx,
SemanticSyntaxErrorKind::MultipleStarredExpressions,
range,
);
return;
}
has_starred = true;
}
}
}
}
/// Check for [`SemanticSyntaxErrorKind::WriteToDebug`] in `stmt`.
fn debug_shadowing<Ctx: SemanticSyntaxContext>(stmt: &ast::Stmt, ctx: &Ctx) {
match stmt {
@ -754,6 +788,20 @@ impl SemanticSyntaxChecker {
Self::yield_outside_function(ctx, expr, YieldOutsideFunctionKind::Await);
Self::await_outside_async_function(ctx, expr, AwaitOutsideAsyncFunctionKind::Await);
}
Expr::Tuple(ast::ExprTuple {
elts,
ctx: expr_ctx,
range,
..
})
| Expr::List(ast::ExprList {
elts,
ctx: expr_ctx,
range,
..
}) => {
Self::multiple_star_expression(ctx, *expr_ctx, elts, *range);
}
Expr::Lambda(ast::ExprLambda {
parameters: Some(parameters),
..
@ -1035,6 +1083,9 @@ impl Display for SemanticSyntaxError {
SemanticSyntaxErrorKind::NonModuleImportStar(name) => {
write!(f, "`from {name} import *` only allowed at module level")
}
SemanticSyntaxErrorKind::MultipleStarredExpressions => {
write!(f, "Two starred expressions in assignment")
}
}
}
}
@ -1398,6 +1449,13 @@ pub enum SemanticSyntaxErrorKind {
/// Represents the use of `from <module> import *` outside module scope.
NonModuleImportStar(String),
/// Represents the use of more than one starred expression in an assignment.
///
/// Python only allows a single starred target when unpacking values on the
/// left-hand side of an assignment. Using multiple starred expressions makes
/// the statement invalid and results in a `SyntaxError`.
MultipleStarredExpressions,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, get_size2::GetSize)]