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

Summary
--

Fixes https://github.com/astral-sh/ruff/issues/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.
This commit is contained in:
Brent Westbrook 2025-04-02 08:38:25 -04:00 committed by GitHub
parent 2ae39edccf
commit d45593288f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
20 changed files with 679 additions and 435 deletions

View file

@ -552,7 +552,8 @@ impl SemanticSyntaxContext for Checker<'_> {
| SemanticSyntaxErrorKind::MultipleCaseAssignment(_) | SemanticSyntaxErrorKind::MultipleCaseAssignment(_)
| SemanticSyntaxErrorKind::IrrefutableCasePattern(_) | SemanticSyntaxErrorKind::IrrefutableCasePattern(_)
| SemanticSyntaxErrorKind::SingleStarredAssignment | SemanticSyntaxErrorKind::SingleStarredAssignment
| SemanticSyntaxErrorKind::WriteToDebug(_) => { | SemanticSyntaxErrorKind::WriteToDebug(_)
| SemanticSyntaxErrorKind::InvalidStarExpression => {
if self.settings.preview.is_enabled() { if self.settings.preview.is_enabled() {
self.semantic_errors.borrow_mut().push(error); self.semantic_errors.borrow_mut().push(error);
} }

View file

@ -0,0 +1,2 @@
for _ in *x: ...
for *x in xs: ...

View file

@ -0,0 +1 @@
def f(): return *x

View file

@ -0,0 +1 @@
def f(): yield *x

View file

@ -0,0 +1,4 @@
def f(): yield (*x,)
def f(): return (*x,)
for _ in (*x,): ...
for (*x,) in xs: ...

View file

@ -10,7 +10,5 @@ yield x, y
yield (x, y) yield (x, y)
yield x == y yield x == y
yield (x := 1) yield (x := 1)
yield *y
yield x, *y yield x, *y
yield *x, yield *x,
yield *x | y

View file

@ -22,19 +22,9 @@ for a, b, c, in iter:
for (a, b) in iter: for (a, b) in iter:
pass pass
for target in *x.attr:
pass
for target in [1, 2]: for target in [1, 2]:
pass 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 await x: ...
for target in lambda x: x: ... for target in lambda x: x: ...
for target in x if True else y: ... for target in x if True else y: ...

View file

@ -1,7 +1,5 @@
return return
return x return x
return *x
return *x | y
return *x, *y return *x, *y
return (x := 1) return (x := 1)
return None return None

View file

@ -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); 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`. /// Check for [`SemanticSyntaxErrorKind::WriteToDebug`] in `stmt`.
fn debug_shadowing<Ctx: SemanticSyntaxContext>(stmt: &ast::Stmt, ctx: &Ctx) { fn debug_shadowing<Ctx: SemanticSyntaxContext>(stmt: &ast::Stmt, ctx: &Ctx) {
match stmt { match stmt {
@ -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);
}
_ => {} _ => {}
} }
} }
@ -462,6 +499,9 @@ impl Display for SemanticSyntaxError {
write!(f, "cannot delete `__debug__` on Python {python_version} (syntax was removed in 3.9)") 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")
}
} }
} }
} }
@ -575,6 +615,19 @@ pub enum SemanticSyntaxErrorKind {
/// ///
/// [BPO 45000]: https://github.com/python/cpython/issues/89163 /// [BPO 45000]: https://github.com/python/cpython/issues/89163
WriteToDebug(WriteToDebugKind), 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)] #[derive(Debug, Clone, PartialEq, Eq, Hash)]

View file

@ -111,3 +111,14 @@ Module(
4 | yield *x and y, z 4 | yield *x and y, z
| ^^^^^^^ Syntax Error: Boolean expression cannot be used here | ^^^^^^^ 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
|

View file

@ -1,7 +1,6 @@
--- ---
source: crates/ruff_python_parser/tests/fixtures.rs source: crates/ruff_python_parser/tests/fixtures.rs
input_file: crates/ruff_python_parser/resources/inline/err/for_stmt_invalid_iter_expr.py input_file: crates/ruff_python_parser/resources/inline/err/for_stmt_invalid_iter_expr.py
snapshot_kind: text
--- ---
## AST ## AST
@ -182,3 +181,13 @@ Module(
3 | for target in x := 1: ... 3 | for target in x := 1: ...
| ^ Syntax Error: Invalid annotated assignment target | ^ 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: ...
|

View file

@ -1,7 +1,6 @@
--- ---
source: crates/ruff_python_parser/tests/fixtures.rs source: crates/ruff_python_parser/tests/fixtures.rs
input_file: crates/ruff_python_parser/resources/inline/err/for_stmt_invalid_target.py input_file: crates/ruff_python_parser/resources/inline/err/for_stmt_invalid_target.py
snapshot_kind: text
--- ---
## AST ## AST
@ -462,3 +461,25 @@ Module(
7 | for [x, 1, y, *["a"]] in z: ... 7 | for [x, 1, y, *["a"]] in z: ...
| ^^^ Syntax Error: Invalid assignment target | ^^^ 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: ...
|

View file

@ -1,7 +1,6 @@
--- ---
source: crates/ruff_python_parser/tests/fixtures.rs source: crates/ruff_python_parser/tests/fixtures.rs
input_file: crates/ruff_python_parser/resources/inline/err/return_stmt_invalid_expr.py input_file: crates/ruff_python_parser/resources/inline/err/return_stmt_invalid_expr.py
snapshot_kind: text
--- ---
## AST ## AST
@ -181,3 +180,21 @@ Module(
5 | return *x and y 5 | return *x and y
| ^^^^^^^ Syntax Error: Boolean expression cannot be used here | ^^^^^^^ 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
|

View file

@ -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
|

View file

@ -0,0 +1,64 @@
---
source: crates/ruff_python_parser/tests/fixtures.rs
input_file: crates/ruff_python_parser/resources/inline/err/single_star_return.py
---
## AST
```
Module(
ModModule {
range: 0..19,
body: [
FunctionDef(
StmtFunctionDef {
range: 0..18,
is_async: false,
decorator_list: [],
name: Identifier {
id: Name("f"),
range: 4..5,
},
type_params: None,
parameters: Parameters {
range: 5..7,
posonlyargs: [],
args: [],
vararg: None,
kwonlyargs: [],
kwarg: None,
},
returns: None,
body: [
Return(
StmtReturn {
range: 9..18,
value: Some(
Starred(
ExprStarred {
range: 16..18,
value: Name(
ExprName {
range: 17..18,
id: Name("x"),
ctx: Load,
},
),
ctx: Load,
},
),
),
},
),
],
},
),
],
},
)
```
## Semantic Syntax Errors
|
1 | def f(): return *x
| ^^ Syntax Error: can't use starred expression here
|

View file

@ -0,0 +1,69 @@
---
source: crates/ruff_python_parser/tests/fixtures.rs
input_file: crates/ruff_python_parser/resources/inline/err/single_star_yield.py
---
## AST
```
Module(
ModModule {
range: 0..18,
body: [
FunctionDef(
StmtFunctionDef {
range: 0..17,
is_async: false,
decorator_list: [],
name: Identifier {
id: Name("f"),
range: 4..5,
},
type_params: None,
parameters: Parameters {
range: 5..7,
posonlyargs: [],
args: [],
vararg: None,
kwonlyargs: [],
kwarg: None,
},
returns: None,
body: [
Expr(
StmtExpr {
range: 9..17,
value: Yield(
ExprYield {
range: 9..17,
value: Some(
Starred(
ExprStarred {
range: 15..17,
value: Name(
ExprName {
range: 16..17,
id: Name("x"),
ctx: Load,
},
),
ctx: Load,
},
),
),
},
),
},
),
],
},
),
],
},
)
```
## Semantic Syntax Errors
|
1 | def f(): yield *x
| ^^ Syntax Error: can't use starred expression here
|

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/expressions/yield.py input_file: crates/ruff_python_parser/resources/valid/expressions/yield.py
snapshot_kind: text
--- ---
## AST ## AST
``` ```
Module( Module(
ModModule { ModModule {
range: 0..188, range: 0..166,
body: [ body: [
Expr( Expr(
StmtExpr { StmtExpr {
@ -385,53 +384,28 @@ Module(
), ),
Expr( Expr(
StmtExpr { StmtExpr {
range: 144..152, range: 144..155,
value: Yield( value: Yield(
ExprYield { ExprYield {
range: 144..152, range: 144..155,
value: Some(
Starred(
ExprStarred {
range: 150..152,
value: Name(
ExprName {
range: 151..152,
id: Name("y"),
ctx: Load,
},
),
ctx: Load,
},
),
),
},
),
},
),
Expr(
StmtExpr {
range: 153..164,
value: Yield(
ExprYield {
range: 153..164,
value: Some( value: Some(
Tuple( Tuple(
ExprTuple { ExprTuple {
range: 159..164, range: 150..155,
elts: [ elts: [
Name( Name(
ExprName { ExprName {
range: 159..160, range: 150..151,
id: Name("x"), id: Name("x"),
ctx: Load, ctx: Load,
}, },
), ),
Starred( Starred(
ExprStarred { ExprStarred {
range: 162..164, range: 153..155,
value: Name( value: Name(
ExprName { ExprName {
range: 163..164, range: 154..155,
id: Name("y"), id: Name("y"),
ctx: Load, ctx: Load,
}, },
@ -451,21 +425,21 @@ Module(
), ),
Expr( Expr(
StmtExpr { StmtExpr {
range: 165..174, range: 156..165,
value: Yield( value: Yield(
ExprYield { ExprYield {
range: 165..174, range: 156..165,
value: Some( value: Some(
Tuple( Tuple(
ExprTuple { ExprTuple {
range: 171..174, range: 162..165,
elts: [ elts: [
Starred( Starred(
ExprStarred { ExprStarred {
range: 171..173, range: 162..164,
value: Name( value: Name(
ExprName { ExprName {
range: 172..173, range: 163..164,
id: Name("x"), id: Name("x"),
ctx: Load, ctx: Load,
}, },
@ -483,44 +457,6 @@ Module(
), ),
}, },
), ),
Expr(
StmtExpr {
range: 175..187,
value: Yield(
ExprYield {
range: 175..187,
value: Some(
Starred(
ExprStarred {
range: 181..187,
value: BinOp(
ExprBinOp {
range: 182..187,
left: Name(
ExprName {
range: 182..183,
id: Name("x"),
ctx: Load,
},
),
op: BitOr,
right: Name(
ExprName {
range: 186..187,
id: Name("y"),
ctx: Load,
},
),
},
),
ctx: Load,
},
),
),
},
),
},
),
], ],
}, },
) )

View file

@ -0,0 +1,220 @@
---
source: crates/ruff_python_parser/tests/fixtures.rs
input_file: crates/ruff_python_parser/resources/inline/ok/single_star_in_tuple.py
---
## AST
```
Module(
ModModule {
range: 0..84,
body: [
FunctionDef(
StmtFunctionDef {
range: 0..20,
is_async: false,
decorator_list: [],
name: Identifier {
id: Name("f"),
range: 4..5,
},
type_params: None,
parameters: Parameters {
range: 5..7,
posonlyargs: [],
args: [],
vararg: None,
kwonlyargs: [],
kwarg: None,
},
returns: None,
body: [
Expr(
StmtExpr {
range: 9..20,
value: Yield(
ExprYield {
range: 9..20,
value: Some(
Tuple(
ExprTuple {
range: 15..20,
elts: [
Starred(
ExprStarred {
range: 16..18,
value: Name(
ExprName {
range: 17..18,
id: Name("x"),
ctx: Load,
},
),
ctx: Load,
},
),
],
ctx: Load,
parenthesized: true,
},
),
),
},
),
},
),
],
},
),
FunctionDef(
StmtFunctionDef {
range: 21..42,
is_async: false,
decorator_list: [],
name: Identifier {
id: Name("f"),
range: 25..26,
},
type_params: None,
parameters: Parameters {
range: 26..28,
posonlyargs: [],
args: [],
vararg: None,
kwonlyargs: [],
kwarg: None,
},
returns: None,
body: [
Return(
StmtReturn {
range: 30..42,
value: Some(
Tuple(
ExprTuple {
range: 37..42,
elts: [
Starred(
ExprStarred {
range: 38..40,
value: Name(
ExprName {
range: 39..40,
id: Name("x"),
ctx: Load,
},
),
ctx: Load,
},
),
],
ctx: Load,
parenthesized: true,
},
),
),
},
),
],
},
),
For(
StmtFor {
range: 43..62,
is_async: false,
target: Name(
ExprName {
range: 47..48,
id: Name("_"),
ctx: Store,
},
),
iter: Tuple(
ExprTuple {
range: 52..57,
elts: [
Starred(
ExprStarred {
range: 53..55,
value: Name(
ExprName {
range: 54..55,
id: Name("x"),
ctx: Load,
},
),
ctx: Load,
},
),
],
ctx: Load,
parenthesized: true,
},
),
body: [
Expr(
StmtExpr {
range: 59..62,
value: EllipsisLiteral(
ExprEllipsisLiteral {
range: 59..62,
},
),
},
),
],
orelse: [],
},
),
For(
StmtFor {
range: 63..83,
is_async: false,
target: Tuple(
ExprTuple {
range: 67..72,
elts: [
Starred(
ExprStarred {
range: 68..70,
value: Name(
ExprName {
range: 69..70,
id: Name("x"),
ctx: Store,
},
),
ctx: Store,
},
),
],
ctx: Store,
parenthesized: true,
},
),
iter: Name(
ExprName {
range: 76..78,
id: Name("xs"),
ctx: Load,
},
),
body: [
Expr(
StmtExpr {
range: 80..83,
value: EllipsisLiteral(
ExprEllipsisLiteral {
range: 80..83,
},
),
},
),
],
orelse: [],
},
),
],
},
)
```

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/for.py input_file: crates/ruff_python_parser/resources/valid/statement/for.py
snapshot_kind: text
--- ---
## AST ## AST
``` ```
Module( Module(
ModModule { ModModule {
range: 0..660, range: 0..523,
body: [ body: [
For( For(
StmtFor { StmtFor {
@ -377,7 +376,7 @@ Module(
), ),
For( For(
StmtFor { StmtFor {
range: 264..295, range: 264..294,
is_async: false, is_async: false,
target: Name( target: Name(
ExprName { ExprName {
@ -386,57 +385,13 @@ Module(
ctx: Store, ctx: Store,
}, },
), ),
iter: Starred(
ExprStarred {
range: 278..285,
value: Attribute(
ExprAttribute {
range: 279..285,
value: Name(
ExprName {
range: 279..280,
id: Name("x"),
ctx: Load,
},
),
attr: Identifier {
id: Name("attr"),
range: 281..285,
},
ctx: Load,
},
),
ctx: Load,
},
),
body: [
Pass(
StmtPass {
range: 291..295,
},
),
],
orelse: [],
},
),
For(
StmtFor {
range: 297..327,
is_async: false,
target: Name(
ExprName {
range: 301..307,
id: Name("target"),
ctx: Store,
},
),
iter: List( iter: List(
ExprList { ExprList {
range: 311..317, range: 278..284,
elts: [ elts: [
NumberLiteral( NumberLiteral(
ExprNumberLiteral { ExprNumberLiteral {
range: 312..313, range: 279..280,
value: Int( value: Int(
1, 1,
), ),
@ -444,7 +399,7 @@ Module(
), ),
NumberLiteral( NumberLiteral(
ExprNumberLiteral { ExprNumberLiteral {
range: 315..316, range: 282..283,
value: Int( value: Int(
2, 2,
), ),
@ -457,7 +412,7 @@ Module(
body: [ body: [
Pass( Pass(
StmtPass { StmtPass {
range: 323..327, range: 290..294,
}, },
), ),
], ],
@ -466,180 +421,21 @@ Module(
), ),
For( For(
StmtFor { StmtFor {
range: 329..377, range: 296..322,
is_async: false,
target: Starred(
ExprStarred {
range: 333..340,
value: Name(
ExprName {
range: 334..340,
id: Name("target"),
ctx: Store,
},
),
ctx: Store,
},
),
iter: Tuple(
ExprTuple {
range: 344..352,
elts: [
Name(
ExprName {
range: 344..345,
id: Name("a"),
ctx: Load,
},
),
Name(
ExprName {
range: 347..348,
id: Name("b"),
ctx: Load,
},
),
Name(
ExprName {
range: 350..351,
id: Name("c"),
ctx: Load,
},
),
],
ctx: Load,
parenthesized: false,
},
),
body: [
Pass(
StmtPass {
range: 358..362,
},
),
],
orelse: [
Pass(
StmtPass {
range: 373..377,
},
),
],
},
),
For(
StmtFor {
range: 379..404,
is_async: false, is_async: false,
target: Name( target: Name(
ExprName { ExprName {
range: 383..389, range: 300..306,
id: Name("target"),
ctx: Store,
},
),
iter: Starred(
ExprStarred {
range: 393..399,
value: BinOp(
ExprBinOp {
range: 394..399,
left: Name(
ExprName {
range: 394..395,
id: Name("x"),
ctx: Load,
},
),
op: BitOr,
right: Name(
ExprName {
range: 398..399,
id: Name("y"),
ctx: Load,
},
),
},
),
ctx: Load,
},
),
body: [
Expr(
StmtExpr {
range: 401..404,
value: EllipsisLiteral(
ExprEllipsisLiteral {
range: 401..404,
},
),
},
),
],
orelse: [],
},
),
For(
StmtFor {
range: 405..432,
is_async: false,
target: Name(
ExprName {
range: 409..415,
id: Name("target"),
ctx: Store,
},
),
iter: Starred(
ExprStarred {
range: 419..427,
value: Await(
ExprAwait {
range: 420..427,
value: Name(
ExprName {
range: 426..427,
id: Name("x"),
ctx: Load,
},
),
},
),
ctx: Load,
},
),
body: [
Expr(
StmtExpr {
range: 429..432,
value: EllipsisLiteral(
ExprEllipsisLiteral {
range: 429..432,
},
),
},
),
],
orelse: [],
},
),
For(
StmtFor {
range: 433..459,
is_async: false,
target: Name(
ExprName {
range: 437..443,
id: Name("target"), id: Name("target"),
ctx: Store, ctx: Store,
}, },
), ),
iter: Await( iter: Await(
ExprAwait { ExprAwait {
range: 447..454, range: 310..317,
value: Name( value: Name(
ExprName { ExprName {
range: 453..454, range: 316..317,
id: Name("x"), id: Name("x"),
ctx: Load, ctx: Load,
}, },
@ -649,10 +445,10 @@ Module(
body: [ body: [
Expr( Expr(
StmtExpr { StmtExpr {
range: 456..459, range: 319..322,
value: EllipsisLiteral( value: EllipsisLiteral(
ExprEllipsisLiteral { ExprEllipsisLiteral {
range: 456..459, range: 319..322,
}, },
), ),
}, },
@ -663,30 +459,30 @@ Module(
), ),
For( For(
StmtFor { StmtFor {
range: 460..490, range: 323..353,
is_async: false, is_async: false,
target: Name( target: Name(
ExprName { ExprName {
range: 464..470, range: 327..333,
id: Name("target"), id: Name("target"),
ctx: Store, ctx: Store,
}, },
), ),
iter: Lambda( iter: Lambda(
ExprLambda { ExprLambda {
range: 474..485, range: 337..348,
parameters: Some( parameters: Some(
Parameters { Parameters {
range: 481..482, range: 344..345,
posonlyargs: [], posonlyargs: [],
args: [ args: [
ParameterWithDefault { ParameterWithDefault {
range: 481..482, range: 344..345,
parameter: Parameter { parameter: Parameter {
range: 481..482, range: 344..345,
name: Identifier { name: Identifier {
id: Name("x"), id: Name("x"),
range: 481..482, range: 344..345,
}, },
annotation: None, annotation: None,
}, },
@ -700,7 +496,7 @@ Module(
), ),
body: Name( body: Name(
ExprName { ExprName {
range: 484..485, range: 347..348,
id: Name("x"), id: Name("x"),
ctx: Load, ctx: Load,
}, },
@ -710,10 +506,10 @@ Module(
body: [ body: [
Expr( Expr(
StmtExpr { StmtExpr {
range: 487..490, range: 350..353,
value: EllipsisLiteral( value: EllipsisLiteral(
ExprEllipsisLiteral { ExprEllipsisLiteral {
range: 487..490, range: 350..353,
}, },
), ),
}, },
@ -724,34 +520,34 @@ Module(
), ),
For( For(
StmtFor { StmtFor {
range: 491..526, range: 354..389,
is_async: false, is_async: false,
target: Name( target: Name(
ExprName { ExprName {
range: 495..501, range: 358..364,
id: Name("target"), id: Name("target"),
ctx: Store, ctx: Store,
}, },
), ),
iter: If( iter: If(
ExprIf { ExprIf {
range: 505..521, range: 368..384,
test: BooleanLiteral( test: BooleanLiteral(
ExprBooleanLiteral { ExprBooleanLiteral {
range: 510..514, range: 373..377,
value: true, value: true,
}, },
), ),
body: Name( body: Name(
ExprName { ExprName {
range: 505..506, range: 368..369,
id: Name("x"), id: Name("x"),
ctx: Load, ctx: Load,
}, },
), ),
orelse: Name( orelse: Name(
ExprName { ExprName {
range: 520..521, range: 383..384,
id: Name("y"), id: Name("y"),
ctx: Load, ctx: Load,
}, },
@ -761,10 +557,10 @@ Module(
body: [ body: [
Expr( Expr(
StmtExpr { StmtExpr {
range: 523..526, range: 386..389,
value: EllipsisLiteral( value: EllipsisLiteral(
ExprEllipsisLiteral { ExprEllipsisLiteral {
range: 523..526, range: 386..389,
}, },
), ),
}, },
@ -775,10 +571,10 @@ Module(
), ),
If( If(
StmtIf { StmtIf {
range: 528..659, range: 391..522,
test: Name( test: Name(
ExprName { ExprName {
range: 531..532, range: 394..395,
id: Name("x"), id: Name("x"),
ctx: Load, ctx: Load,
}, },
@ -786,18 +582,18 @@ Module(
body: [ body: [
For( For(
StmtFor { StmtFor {
range: 538..570, range: 401..433,
is_async: false, is_async: false,
target: Name( target: Name(
ExprName { ExprName {
range: 542..548, range: 405..411,
id: Name("target"), id: Name("target"),
ctx: Store, ctx: Store,
}, },
), ),
iter: Name( iter: Name(
ExprName { ExprName {
range: 552..556, range: 415..419,
id: Name("iter"), id: Name("iter"),
ctx: Load, ctx: Load,
}, },
@ -805,7 +601,7 @@ Module(
body: [ body: [
Pass( Pass(
StmtPass { StmtPass {
range: 566..570, range: 429..433,
}, },
), ),
], ],
@ -815,12 +611,12 @@ Module(
], ],
elif_else_clauses: [ elif_else_clauses: [
ElifElseClause { ElifElseClause {
range: 645..659, range: 508..522,
test: None, test: None,
body: [ body: [
Pass( Pass(
StmtPass { StmtPass {
range: 655..659, range: 518..522,
}, },
), ),
], ],

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/return.py input_file: crates/ruff_python_parser/resources/valid/statement/return.py
snapshot_kind: text
--- ---
## AST ## AST
``` ```
Module( Module(
ModModule { ModModule {
range: 0..191, range: 0..167,
body: [ body: [
Return( Return(
StmtReturn { StmtReturn {
@ -32,71 +31,18 @@ Module(
), ),
Return( Return(
StmtReturn { StmtReturn {
range: 16..25, range: 16..29,
value: Some(
Starred(
ExprStarred {
range: 23..25,
value: Name(
ExprName {
range: 24..25,
id: Name("x"),
ctx: Load,
},
),
ctx: Load,
},
),
),
},
),
Return(
StmtReturn {
range: 26..39,
value: Some(
Starred(
ExprStarred {
range: 33..39,
value: BinOp(
ExprBinOp {
range: 34..39,
left: Name(
ExprName {
range: 34..35,
id: Name("x"),
ctx: Load,
},
),
op: BitOr,
right: Name(
ExprName {
range: 38..39,
id: Name("y"),
ctx: Load,
},
),
},
),
ctx: Load,
},
),
),
},
),
Return(
StmtReturn {
range: 40..53,
value: Some( value: Some(
Tuple( Tuple(
ExprTuple { ExprTuple {
range: 47..53, range: 23..29,
elts: [ elts: [
Starred( Starred(
ExprStarred { ExprStarred {
range: 47..49, range: 23..25,
value: Name( value: Name(
ExprName { ExprName {
range: 48..49, range: 24..25,
id: Name("x"), id: Name("x"),
ctx: Load, ctx: Load,
}, },
@ -106,10 +52,10 @@ Module(
), ),
Starred( Starred(
ExprStarred { ExprStarred {
range: 51..53, range: 27..29,
value: Name( value: Name(
ExprName { ExprName {
range: 52..53, range: 28..29,
id: Name("y"), id: Name("y"),
ctx: Load, ctx: Load,
}, },
@ -127,21 +73,21 @@ Module(
), ),
Return( Return(
StmtReturn { StmtReturn {
range: 54..69, range: 30..45,
value: Some( value: Some(
Named( Named(
ExprNamed { ExprNamed {
range: 62..68, range: 38..44,
target: Name( target: Name(
ExprName { ExprName {
range: 62..63, range: 38..39,
id: Name("x"), id: Name("x"),
ctx: Store, ctx: Store,
}, },
), ),
value: NumberLiteral( value: NumberLiteral(
ExprNumberLiteral { ExprNumberLiteral {
range: 67..68, range: 43..44,
value: Int( value: Int(
1, 1,
), ),
@ -154,11 +100,11 @@ Module(
), ),
Return( Return(
StmtReturn { StmtReturn {
range: 70..81, range: 46..57,
value: Some( value: Some(
NoneLiteral( NoneLiteral(
ExprNoneLiteral { ExprNoneLiteral {
range: 77..81, range: 53..57,
}, },
), ),
), ),
@ -166,23 +112,23 @@ Module(
), ),
Return( Return(
StmtReturn { StmtReturn {
range: 82..96, range: 58..72,
value: Some( value: Some(
BoolOp( BoolOp(
ExprBoolOp { ExprBoolOp {
range: 89..96, range: 65..72,
op: And, op: And,
values: [ values: [
Name( Name(
ExprName { ExprName {
range: 89..90, range: 65..66,
id: Name("x"), id: Name("x"),
ctx: Load, ctx: Load,
}, },
), ),
Name( Name(
ExprName { ExprName {
range: 95..96, range: 71..72,
id: Name("y"), id: Name("y"),
ctx: Load, ctx: Load,
}, },
@ -195,14 +141,14 @@ Module(
), ),
Return( Return(
StmtReturn { StmtReturn {
range: 97..109, range: 73..85,
value: Some( value: Some(
Compare( Compare(
ExprCompare { ExprCompare {
range: 104..109, range: 80..85,
left: NumberLiteral( left: NumberLiteral(
ExprNumberLiteral { ExprNumberLiteral {
range: 104..105, range: 80..81,
value: Int( value: Int(
1, 1,
), ),
@ -214,7 +160,7 @@ Module(
comparators: [ comparators: [
NumberLiteral( NumberLiteral(
ExprNumberLiteral { ExprNumberLiteral {
range: 108..109, range: 84..85,
value: Int( value: Int(
2, 2,
), ),
@ -228,15 +174,15 @@ Module(
), ),
Return( Return(
StmtReturn { StmtReturn {
range: 110..122, range: 86..98,
value: Some( value: Some(
Tuple( Tuple(
ExprTuple { ExprTuple {
range: 117..122, range: 93..98,
elts: [ elts: [
NumberLiteral( NumberLiteral(
ExprNumberLiteral { ExprNumberLiteral {
range: 117..118, range: 93..94,
value: Int( value: Int(
1, 1,
), ),
@ -244,7 +190,7 @@ Module(
), ),
NumberLiteral( NumberLiteral(
ExprNumberLiteral { ExprNumberLiteral {
range: 120..121, range: 96..97,
value: Int( value: Int(
2, 2,
), ),
@ -260,20 +206,20 @@ Module(
), ),
Return( Return(
StmtReturn { StmtReturn {
range: 123..136, range: 99..112,
value: Some( value: Some(
Call( Call(
ExprCall { ExprCall {
range: 130..136, range: 106..112,
func: Name( func: Name(
ExprName { ExprName {
range: 130..134, range: 106..110,
id: Name("call"), id: Name("call"),
ctx: Load, ctx: Load,
}, },
), ),
arguments: Arguments { arguments: Arguments {
range: 134..136, range: 110..112,
args: [], args: [],
keywords: [], keywords: [],
}, },
@ -284,30 +230,30 @@ Module(
), ),
Return( Return(
StmtReturn { StmtReturn {
range: 137..156, range: 113..132,
value: Some( value: Some(
Call( Call(
ExprCall { ExprCall {
range: 144..156, range: 120..132,
func: Attribute( func: Attribute(
ExprAttribute { ExprAttribute {
range: 144..154, range: 120..130,
value: Name( value: Name(
ExprName { ExprName {
range: 144..148, range: 120..124,
id: Name("attr"), id: Name("attr"),
ctx: Load, ctx: Load,
}, },
), ),
attr: Identifier { attr: Identifier {
id: Name("value"), id: Name("value"),
range: 149..154, range: 125..130,
}, },
ctx: Load, ctx: Load,
}, },
), ),
arguments: Arguments { arguments: Arguments {
range: 154..156, range: 130..132,
args: [], args: [],
keywords: [], keywords: [],
}, },
@ -318,14 +264,14 @@ Module(
), ),
Return( Return(
StmtReturn { StmtReturn {
range: 157..171, range: 133..147,
value: Some( value: Some(
Await( Await(
ExprAwait { ExprAwait {
range: 164..171, range: 140..147,
value: Name( value: Name(
ExprName { ExprName {
range: 170..171, range: 146..147,
id: Name("x"), id: Name("x"),
ctx: Load, ctx: Load,
}, },
@ -337,23 +283,23 @@ Module(
), ),
Return( Return(
StmtReturn { StmtReturn {
range: 172..190, range: 148..166,
value: Some( value: Some(
Lambda( Lambda(
ExprLambda { ExprLambda {
range: 179..190, range: 155..166,
parameters: Some( parameters: Some(
Parameters { Parameters {
range: 186..187, range: 162..163,
posonlyargs: [], posonlyargs: [],
args: [ args: [
ParameterWithDefault { ParameterWithDefault {
range: 186..187, range: 162..163,
parameter: Parameter { parameter: Parameter {
range: 186..187, range: 162..163,
name: Identifier { name: Identifier {
id: Name("x"), id: Name("x"),
range: 186..187, range: 162..163,
}, },
annotation: None, annotation: None,
}, },
@ -367,7 +313,7 @@ Module(
), ),
body: Name( body: Name(
ExprName { ExprName {
range: 189..190, range: 165..166,
id: Name("y"), id: Name("y"),
ctx: Load, ctx: Load,
}, },