[syntax-errors] Single starred assignment target (#17024)

Summary
--

Detects starred assignment targets outside of tuples and lists like `*a
= (1,)`.

This PR only considers assignment statements. I also checked annotated
assigment statements, but these give a separate error that we already
catch, so I think they're okay not to consider:

```pycon
>>> *a: list[int] = []
  File "<python-input-72>", line 1
    *a: list[int] = []
      ^
SyntaxError: invalid syntax
```

Fixes #13759

Test Plan
--

New inline tests, plus a new `SemanticSyntaxError` for an existing
parser test. I also removed a now-invalid case from an otherwise-valid
test fixture.

The new semantic error leads to two errors for the case below:

```python
*foo() = 42
```

but this matches [pyright] too.

[pyright]: https://pyright-play.net/?code=FQMw9mAUCUAEC8sAsAmAUEA
This commit is contained in:
Brent Westbrook 2025-03-29 12:35:47 -04:00 committed by GitHub
parent a0819f0c51
commit ab1011ce70
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 365 additions and 138 deletions

View file

@ -551,16 +551,12 @@ impl SemanticSyntaxContext for Checker<'_> {
| SemanticSyntaxErrorKind::DuplicateTypeParameter | SemanticSyntaxErrorKind::DuplicateTypeParameter
| SemanticSyntaxErrorKind::MultipleCaseAssignment(_) | SemanticSyntaxErrorKind::MultipleCaseAssignment(_)
| SemanticSyntaxErrorKind::IrrefutableCasePattern(_) | SemanticSyntaxErrorKind::IrrefutableCasePattern(_)
| SemanticSyntaxErrorKind::WriteToDebug(_) | SemanticSyntaxErrorKind::SingleStarredAssignment
if self.settings.preview.is_enabled() => | SemanticSyntaxErrorKind::WriteToDebug(_) => {
{ if self.settings.preview.is_enabled() {
self.semantic_errors.borrow_mut().push(error); self.semantic_errors.borrow_mut().push(error);
} }
SemanticSyntaxErrorKind::ReboundComprehensionVariable }
| SemanticSyntaxErrorKind::DuplicateTypeParameter
| SemanticSyntaxErrorKind::MultipleCaseAssignment(_)
| SemanticSyntaxErrorKind::IrrefutableCasePattern(_)
| SemanticSyntaxErrorKind::WriteToDebug(_) => {}
} }
} }
} }

View file

@ -0,0 +1,3 @@
(*a,) = (1,)
*a, = (1,)
[*a] = (1,)

View file

@ -14,8 +14,6 @@ x[y] = (1, 2, 3)
# This last group of tests checks that assignments we expect to be parsed # This last group of tests checks that assignments we expect to be parsed
# (including some interesting ones) continue to be parsed successfully. # (including some interesting ones) continue to be parsed successfully.
*foo = 42
[x, y, z] = [1, 2, 3] [x, y, z] = [1, 2, 3]
(x, y, z) = (1, 2, 3) (x, y, z) = (1, 2, 3)

View file

@ -73,6 +73,22 @@ impl SemanticSyntaxChecker {
Self::duplicate_type_parameter_name(type_params, ctx); Self::duplicate_type_parameter_name(type_params, ctx);
} }
} }
Stmt::Assign(ast::StmtAssign { targets, .. }) => {
if let [Expr::Starred(ast::ExprStarred { range, .. })] = targets.as_slice() {
// test_ok single_starred_assignment_target
// (*a,) = (1,)
// *a, = (1,)
// [*a] = (1,)
// test_err single_starred_assignment_target
// *a = (1,)
Self::add_error(
ctx,
SemanticSyntaxErrorKind::SingleStarredAssignment,
*range,
);
}
}
_ => {} _ => {}
} }
@ -437,6 +453,9 @@ impl Display for SemanticSyntaxError {
f.write_str("wildcard makes remaining patterns unreachable") f.write_str("wildcard makes remaining patterns unreachable")
} }
}, },
SemanticSyntaxErrorKind::SingleStarredAssignment => {
f.write_str("starred assignment target must be in a list or tuple")
}
SemanticSyntaxErrorKind::WriteToDebug(kind) => match kind { SemanticSyntaxErrorKind::WriteToDebug(kind) => match kind {
WriteToDebugKind::Store => f.write_str("cannot assign to `__debug__`"), WriteToDebugKind::Store => f.write_str("cannot assign to `__debug__`"),
WriteToDebugKind::Delete(python_version) => { WriteToDebugKind::Delete(python_version) => {
@ -522,6 +541,23 @@ pub enum SemanticSyntaxErrorKind {
/// [Python reference]: https://docs.python.org/3/reference/compound_stmts.html#irrefutable-case-blocks /// [Python reference]: https://docs.python.org/3/reference/compound_stmts.html#irrefutable-case-blocks
IrrefutableCasePattern(IrrefutablePatternKind), IrrefutableCasePattern(IrrefutablePatternKind),
/// Represents a single starred assignment target outside of a tuple or list.
///
/// ## Examples
///
/// ```python
/// *a = (1,) # SyntaxError
/// ```
///
/// A starred assignment target can only occur within a tuple or list:
///
/// ```python
/// b, *a = 1, 2, 3
/// (*a,) = 1, 2, 3
/// [*a] = 1, 2, 3
/// ```
SingleStarredAssignment,
/// Represents a write to `__debug__`. This includes simple assignments and deletions as well /// Represents a write to `__debug__`. This includes simple assignments and deletions as well
/// other kinds of statements that can introduce bindings, such as type parameters in functions, /// other kinds of statements that can introduce bindings, such as type parameters in functions,
/// classes, and aliases, `match` arms, and imports, among others. /// classes, and aliases, `match` arms, and imports, among others.

View file

@ -0,0 +1,58 @@
---
source: crates/ruff_python_parser/tests/fixtures.rs
input_file: crates/ruff_python_parser/resources/inline/err/single_starred_assignment_target.py
---
## AST
```
Module(
ModModule {
range: 0..10,
body: [
Assign(
StmtAssign {
range: 0..9,
targets: [
Starred(
ExprStarred {
range: 0..2,
value: Name(
ExprName {
range: 1..2,
id: Name("a"),
ctx: Store,
},
),
ctx: Store,
},
),
],
value: Tuple(
ExprTuple {
range: 5..9,
elts: [
NumberLiteral(
ExprNumberLiteral {
range: 6..7,
value: Int(
1,
),
},
),
],
ctx: Load,
parenthesized: true,
},
),
},
),
],
},
)
```
## Semantic Syntax Errors
|
1 | *a = (1,)
| ^^ Syntax Error: starred assignment target must be in a list or tuple
|

View file

@ -1690,3 +1690,15 @@ Module(
42 | (x, foo(), y) = (42, 42, 42) 42 | (x, foo(), y) = (42, 42, 42)
| ^^^^^ Syntax Error: Invalid assignment target | ^^^^^ Syntax Error: Invalid assignment target
| |
## Semantic Syntax Errors
|
37 | None = 42
38 | ... = 42
39 | *foo() = 42
| ^^^^^^ Syntax Error: starred assignment target must be in a list or tuple
40 | [x, foo(), y] = [42, 42, 42]
41 | [[a, b], [[42]], d] = [[1, 2], [[3]], 4]
|

View file

@ -0,0 +1,152 @@
---
source: crates/ruff_python_parser/tests/fixtures.rs
input_file: crates/ruff_python_parser/resources/inline/ok/single_starred_assignment_target.py
---
## AST
```
Module(
ModModule {
range: 0..36,
body: [
Assign(
StmtAssign {
range: 0..12,
targets: [
Tuple(
ExprTuple {
range: 0..5,
elts: [
Starred(
ExprStarred {
range: 1..3,
value: Name(
ExprName {
range: 2..3,
id: Name("a"),
ctx: Store,
},
),
ctx: Store,
},
),
],
ctx: Store,
parenthesized: true,
},
),
],
value: Tuple(
ExprTuple {
range: 8..12,
elts: [
NumberLiteral(
ExprNumberLiteral {
range: 9..10,
value: Int(
1,
),
},
),
],
ctx: Load,
parenthesized: true,
},
),
},
),
Assign(
StmtAssign {
range: 13..23,
targets: [
Tuple(
ExprTuple {
range: 13..16,
elts: [
Starred(
ExprStarred {
range: 13..15,
value: Name(
ExprName {
range: 14..15,
id: Name("a"),
ctx: Store,
},
),
ctx: Store,
},
),
],
ctx: Store,
parenthesized: false,
},
),
],
value: Tuple(
ExprTuple {
range: 19..23,
elts: [
NumberLiteral(
ExprNumberLiteral {
range: 20..21,
value: Int(
1,
),
},
),
],
ctx: Load,
parenthesized: true,
},
),
},
),
Assign(
StmtAssign {
range: 24..35,
targets: [
List(
ExprList {
range: 24..28,
elts: [
Starred(
ExprStarred {
range: 25..27,
value: Name(
ExprName {
range: 26..27,
id: Name("a"),
ctx: Store,
},
),
ctx: Store,
},
),
],
ctx: Store,
},
),
],
value: Tuple(
ExprTuple {
range: 31..35,
elts: [
NumberLiteral(
ExprNumberLiteral {
range: 32..33,
value: Int(
1,
),
},
),
],
ctx: Load,
parenthesized: true,
},
),
},
),
],
},
)
```

View file

@ -1,14 +1,13 @@
--- ---
source: crates/ruff_python_parser/tests/fixtures.rs source: crates/ruff_python_parser/tests/fixtures.rs
input_file: crates/ruff_python_parser/resources/valid/statement/assignment.py input_file: crates/ruff_python_parser/resources/valid/statement/assignment.py
snapshot_kind: text
--- ---
## AST ## AST
``` ```
Module( Module(
ModModule { ModModule {
range: 0..734, range: 0..723,
body: [ body: [
Assign( Assign(
StmtAssign { StmtAssign {
@ -370,57 +369,29 @@ Module(
), ),
Assign( Assign(
StmtAssign { StmtAssign {
range: 259..268, range: 259..280,
targets: [
Starred(
ExprStarred {
range: 259..263,
value: Name(
ExprName {
range: 260..263,
id: Name("foo"),
ctx: Store,
},
),
ctx: Store,
},
),
],
value: NumberLiteral(
ExprNumberLiteral {
range: 266..268,
value: Int(
42,
),
},
),
},
),
Assign(
StmtAssign {
range: 270..291,
targets: [ targets: [
List( List(
ExprList { ExprList {
range: 270..279, range: 259..268,
elts: [ elts: [
Name( Name(
ExprName { ExprName {
range: 271..272, range: 260..261,
id: Name("x"), id: Name("x"),
ctx: Store, ctx: Store,
}, },
), ),
Name( Name(
ExprName { ExprName {
range: 274..275, range: 263..264,
id: Name("y"), id: Name("y"),
ctx: Store, ctx: Store,
}, },
), ),
Name( Name(
ExprName { ExprName {
range: 277..278, range: 266..267,
id: Name("z"), id: Name("z"),
ctx: Store, ctx: Store,
}, },
@ -432,11 +403,11 @@ Module(
], ],
value: List( value: List(
ExprList { ExprList {
range: 282..291, range: 271..280,
elts: [ elts: [
NumberLiteral( NumberLiteral(
ExprNumberLiteral { ExprNumberLiteral {
range: 283..284, range: 272..273,
value: Int( value: Int(
1, 1,
), ),
@ -444,7 +415,7 @@ Module(
), ),
NumberLiteral( NumberLiteral(
ExprNumberLiteral { ExprNumberLiteral {
range: 286..287, range: 275..276,
value: Int( value: Int(
2, 2,
), ),
@ -452,7 +423,7 @@ Module(
), ),
NumberLiteral( NumberLiteral(
ExprNumberLiteral { ExprNumberLiteral {
range: 289..290, range: 278..279,
value: Int( value: Int(
3, 3,
), ),
@ -466,29 +437,29 @@ Module(
), ),
Assign( Assign(
StmtAssign { StmtAssign {
range: 293..314, range: 282..303,
targets: [ targets: [
Tuple( Tuple(
ExprTuple { ExprTuple {
range: 293..302, range: 282..291,
elts: [ elts: [
Name( Name(
ExprName { ExprName {
range: 294..295, range: 283..284,
id: Name("x"), id: Name("x"),
ctx: Store, ctx: Store,
}, },
), ),
Name( Name(
ExprName { ExprName {
range: 297..298, range: 286..287,
id: Name("y"), id: Name("y"),
ctx: Store, ctx: Store,
}, },
), ),
Name( Name(
ExprName { ExprName {
range: 300..301, range: 289..290,
id: Name("z"), id: Name("z"),
ctx: Store, ctx: Store,
}, },
@ -501,11 +472,11 @@ Module(
], ],
value: Tuple( value: Tuple(
ExprTuple { ExprTuple {
range: 305..314, range: 294..303,
elts: [ elts: [
NumberLiteral( NumberLiteral(
ExprNumberLiteral { ExprNumberLiteral {
range: 306..307, range: 295..296,
value: Int( value: Int(
1, 1,
), ),
@ -513,7 +484,7 @@ Module(
), ),
NumberLiteral( NumberLiteral(
ExprNumberLiteral { ExprNumberLiteral {
range: 309..310, range: 298..299,
value: Int( value: Int(
2, 2,
), ),
@ -521,7 +492,7 @@ Module(
), ),
NumberLiteral( NumberLiteral(
ExprNumberLiteral { ExprNumberLiteral {
range: 312..313, range: 301..302,
value: Int( value: Int(
3, 3,
), ),
@ -536,21 +507,21 @@ Module(
), ),
Assign( Assign(
StmtAssign { StmtAssign {
range: 315..324, range: 304..313,
targets: [ targets: [
Subscript( Subscript(
ExprSubscript { ExprSubscript {
range: 315..319, range: 304..308,
value: Name( value: Name(
ExprName { ExprName {
range: 315..316, range: 304..305,
id: Name("x"), id: Name("x"),
ctx: Load, ctx: Load,
}, },
), ),
slice: NumberLiteral( slice: NumberLiteral(
ExprNumberLiteral { ExprNumberLiteral {
range: 317..318, range: 306..307,
value: Int( value: Int(
0, 0,
), ),
@ -562,7 +533,7 @@ Module(
], ],
value: NumberLiteral( value: NumberLiteral(
ExprNumberLiteral { ExprNumberLiteral {
range: 322..324, range: 311..313,
value: Int( value: Int(
42, 42,
), ),
@ -572,14 +543,14 @@ Module(
), ),
Assign( Assign(
StmtAssign { StmtAssign {
range: 421..430, range: 410..419,
targets: [ targets: [
Subscript( Subscript(
ExprSubscript { ExprSubscript {
range: 421..425, range: 410..414,
value: NumberLiteral( value: NumberLiteral(
ExprNumberLiteral { ExprNumberLiteral {
range: 421..422, range: 410..411,
value: Int( value: Int(
5, 5,
), ),
@ -587,7 +558,7 @@ Module(
), ),
slice: NumberLiteral( slice: NumberLiteral(
ExprNumberLiteral { ExprNumberLiteral {
range: 423..424, range: 412..413,
value: Int( value: Int(
0, 0,
), ),
@ -599,7 +570,7 @@ Module(
], ],
value: NumberLiteral( value: NumberLiteral(
ExprNumberLiteral { ExprNumberLiteral {
range: 428..430, range: 417..419,
value: Int( value: Int(
42, 42,
), ),
@ -609,25 +580,25 @@ Module(
), ),
Assign( Assign(
StmtAssign { StmtAssign {
range: 431..444, range: 420..433,
targets: [ targets: [
Subscript( Subscript(
ExprSubscript { ExprSubscript {
range: 431..437, range: 420..426,
value: Name( value: Name(
ExprName { ExprName {
range: 431..432, range: 420..421,
id: Name("x"), id: Name("x"),
ctx: Load, ctx: Load,
}, },
), ),
slice: Slice( slice: Slice(
ExprSlice { ExprSlice {
range: 433..436, range: 422..425,
lower: Some( lower: Some(
NumberLiteral( NumberLiteral(
ExprNumberLiteral { ExprNumberLiteral {
range: 433..434, range: 422..423,
value: Int( value: Int(
1, 1,
), ),
@ -637,7 +608,7 @@ Module(
upper: Some( upper: Some(
NumberLiteral( NumberLiteral(
ExprNumberLiteral { ExprNumberLiteral {
range: 435..436, range: 424..425,
value: Int( value: Int(
2, 2,
), ),
@ -653,11 +624,11 @@ Module(
], ],
value: List( value: List(
ExprList { ExprList {
range: 440..444, range: 429..433,
elts: [ elts: [
NumberLiteral( NumberLiteral(
ExprNumberLiteral { ExprNumberLiteral {
range: 441..443, range: 430..432,
value: Int( value: Int(
42, 42,
), ),
@ -671,14 +642,14 @@ Module(
), ),
Assign( Assign(
StmtAssign { StmtAssign {
range: 540..553, range: 529..542,
targets: [ targets: [
Subscript( Subscript(
ExprSubscript { ExprSubscript {
range: 540..546, range: 529..535,
value: NumberLiteral( value: NumberLiteral(
ExprNumberLiteral { ExprNumberLiteral {
range: 540..541, range: 529..530,
value: Int( value: Int(
5, 5,
), ),
@ -686,11 +657,11 @@ Module(
), ),
slice: Slice( slice: Slice(
ExprSlice { ExprSlice {
range: 542..545, range: 531..534,
lower: Some( lower: Some(
NumberLiteral( NumberLiteral(
ExprNumberLiteral { ExprNumberLiteral {
range: 542..543, range: 531..532,
value: Int( value: Int(
1, 1,
), ),
@ -700,7 +671,7 @@ Module(
upper: Some( upper: Some(
NumberLiteral( NumberLiteral(
ExprNumberLiteral { ExprNumberLiteral {
range: 544..545, range: 533..534,
value: Int( value: Int(
2, 2,
), ),
@ -716,11 +687,11 @@ Module(
], ],
value: List( value: List(
ExprList { ExprList {
range: 549..553, range: 538..542,
elts: [ elts: [
NumberLiteral( NumberLiteral(
ExprNumberLiteral { ExprNumberLiteral {
range: 550..552, range: 539..541,
value: Int( value: Int(
42, 42,
), ),
@ -734,21 +705,21 @@ Module(
), ),
Assign( Assign(
StmtAssign { StmtAssign {
range: 555..567, range: 544..556,
targets: [ targets: [
Attribute( Attribute(
ExprAttribute { ExprAttribute {
range: 555..562, range: 544..551,
value: Name( value: Name(
ExprName { ExprName {
range: 555..558, range: 544..547,
id: Name("foo"), id: Name("foo"),
ctx: Load, ctx: Load,
}, },
), ),
attr: Identifier { attr: Identifier {
id: Name("bar"), id: Name("bar"),
range: 559..562, range: 548..551,
}, },
ctx: Store, ctx: Store,
}, },
@ -756,7 +727,7 @@ Module(
], ],
value: NumberLiteral( value: NumberLiteral(
ExprNumberLiteral { ExprNumberLiteral {
range: 565..567, range: 554..556,
value: Int( value: Int(
42, 42,
), ),
@ -766,18 +737,18 @@ Module(
), ),
Assign( Assign(
StmtAssign { StmtAssign {
range: 669..681, range: 658..670,
targets: [ targets: [
Attribute( Attribute(
ExprAttribute { ExprAttribute {
range: 669..676, range: 658..665,
value: StringLiteral( value: StringLiteral(
ExprStringLiteral { ExprStringLiteral {
range: 669..674, range: 658..663,
value: StringLiteralValue { value: StringLiteralValue {
inner: Single( inner: Single(
StringLiteral { StringLiteral {
range: 669..674, range: 658..663,
value: "foo", value: "foo",
flags: StringLiteralFlags { flags: StringLiteralFlags {
quote_style: Double, quote_style: Double,
@ -791,7 +762,7 @@ Module(
), ),
attr: Identifier { attr: Identifier {
id: Name("y"), id: Name("y"),
range: 675..676, range: 664..665,
}, },
ctx: Store, ctx: Store,
}, },
@ -799,7 +770,7 @@ Module(
], ],
value: NumberLiteral( value: NumberLiteral(
ExprNumberLiteral { ExprNumberLiteral {
range: 679..681, range: 668..670,
value: Int( value: Int(
42, 42,
), ),
@ -809,11 +780,11 @@ Module(
), ),
Assign( Assign(
StmtAssign { StmtAssign {
range: 683..691, range: 672..680,
targets: [ targets: [
Name( Name(
ExprName { ExprName {
range: 683..686, range: 672..675,
id: Name("foo"), id: Name("foo"),
ctx: Store, ctx: Store,
}, },
@ -821,7 +792,7 @@ Module(
], ],
value: NumberLiteral( value: NumberLiteral(
ExprNumberLiteral { ExprNumberLiteral {
range: 689..691, range: 678..680,
value: Int( value: Int(
42, 42,
), ),
@ -829,15 +800,43 @@ Module(
), ),
}, },
), ),
Assign(
StmtAssign {
range: 682..692,
targets: [
List(
ExprList {
range: 682..684,
elts: [],
ctx: Store,
},
),
],
value: Starred(
ExprStarred {
range: 687..692,
value: Name(
ExprName {
range: 688..692,
id: Name("data"),
ctx: Load,
},
),
ctx: Load,
},
),
},
),
Assign( Assign(
StmtAssign { StmtAssign {
range: 693..703, range: 693..703,
targets: [ targets: [
List( Tuple(
ExprList { ExprTuple {
range: 693..695, range: 693..695,
elts: [], elts: [],
ctx: Store, ctx: Store,
parenthesized: true,
}, },
), ),
], ],
@ -858,50 +857,22 @@ Module(
), ),
Assign( Assign(
StmtAssign { StmtAssign {
range: 704..714, range: 704..713,
targets: [ targets: [
Tuple( Tuple(
ExprTuple { ExprTuple {
range: 704..706, range: 704..708,
elts: [],
ctx: Store,
parenthesized: true,
},
),
],
value: Starred(
ExprStarred {
range: 709..714,
value: Name(
ExprName {
range: 710..714,
id: Name("data"),
ctx: Load,
},
),
ctx: Load,
},
),
},
),
Assign(
StmtAssign {
range: 715..724,
targets: [
Tuple(
ExprTuple {
range: 715..719,
elts: [ elts: [
Name( Name(
ExprName { ExprName {
range: 715..716, range: 704..705,
id: Name("a"), id: Name("a"),
ctx: Store, ctx: Store,
}, },
), ),
Name( Name(
ExprName { ExprName {
range: 718..719, range: 707..708,
id: Name("b"), id: Name("b"),
ctx: Store, ctx: Store,
}, },
@ -914,7 +885,7 @@ Module(
], ],
value: Name( value: Name(
ExprName { ExprName {
range: 722..724, range: 711..713,
id: Name("ab"), id: Name("ab"),
ctx: Load, ctx: Load,
}, },
@ -923,18 +894,18 @@ Module(
), ),
Assign( Assign(
StmtAssign { StmtAssign {
range: 725..734, range: 714..723,
targets: [ targets: [
Name( Name(
ExprName { ExprName {
range: 725..726, range: 714..715,
id: Name("a"), id: Name("a"),
ctx: Store, ctx: Store,
}, },
), ),
Name( Name(
ExprName { ExprName {
range: 729..730, range: 718..719,
id: Name("b"), id: Name("b"),
ctx: Store, ctx: Store,
}, },
@ -942,7 +913,7 @@ Module(
], ],
value: Name( value: Name(
ExprName { ExprName {
range: 733..734, range: 722..723,
id: Name("c"), id: Name("c"),
ctx: Load, ctx: Load,
}, },