Skip to content

Commit a70807e

Browse files
Expand NamedExpr range to include full range of parenthesized value (#6632)
## Summary Given: ```python if ( x := ( # 4 y # 5 ) # 6 ): pass ``` It turns out the parser ended the range of the `NamedExpr` at the end of `y`, rather than the end of the parenthesis that encloses `y`. This just seems like a bug -- the range should be from the start of the name on the left, to the end of the parenthesized node on the right. ## Test Plan `cargo test`
1 parent d9bb51d commit a70807e

File tree

4 files changed

+9547
-9409
lines changed

4 files changed

+9547
-9409
lines changed

crates/ruff_python_parser/src/parser.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,13 @@ def func[T, U: str, *Ts, **P]():
603603
insta::assert_debug_snapshot!(parse_ast);
604604
}
605605

606+
#[test]
607+
fn test_named_expression() {
608+
let source = "(x := ( y * z ))";
609+
let parse_ast = parse_expression(source, "<test>").unwrap();
610+
insta::assert_debug_snapshot!(parse_ast);
611+
}
612+
606613
#[test]
607614
fn test_with_statement() {
608615
let source = "\

crates/ruff_python_parser/src/python.lalrpop

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1335,15 +1335,19 @@ NamedExpressionTest: ast::Expr = {
13351335
Test<"all">,
13361336
}
13371337

1338+
NamedExpressionName: ast::Expr = {
1339+
<location:@L> <id:Identifier> <end_location:@R> => ast::Expr::Name(
1340+
ast::ExprName { id: id.into(), ctx: ast::ExprContext::Store, range: (location..end_location).into() },
1341+
),
1342+
}
1343+
13381344
NamedExpression: ast::Expr = {
1339-
<location:@L> <id:Identifier> <end_location:@R> ":=" <value:Test<"all">> => {
1345+
<location:@L> <target:NamedExpressionName> ":=" <value:Test<"all">> <end_location:@R> => {
13401346
ast::Expr::NamedExpr(
13411347
ast::ExprNamedExpr {
1342-
target: Box::new(ast::Expr::Name(
1343-
ast::ExprName { id: id.into(), ctx: ast::ExprContext::Store, range: (location..end_location).into() },
1344-
)),
1345-
range: (location..value.end()).into(),
1348+
target: Box::new(target),
13461349
value: Box::new(value),
1350+
range: (location..end_location).into(),
13471351
}
13481352
)
13491353
},

0 commit comments

Comments
 (0)