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`
This commit is contained in:
Charlie Marsh 2023-08-17 10:34:05 -04:00 committed by GitHub
parent d9bb51dee4
commit a70807e1e1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 18803 additions and 18665 deletions

View file

@ -603,6 +603,13 @@ def func[T, U: str, *Ts, **P]():
insta::assert_debug_snapshot!(parse_ast);
}
#[test]
fn test_named_expression() {
let source = "(x := ( y * z ))";
let parse_ast = parse_expression(source, "<test>").unwrap();
insta::assert_debug_snapshot!(parse_ast);
}
#[test]
fn test_with_statement() {
let source = "\

View file

@ -1335,15 +1335,19 @@ NamedExpressionTest: ast::Expr = {
Test<"all">,
}
NamedExpressionName: ast::Expr = {
<location:@L> <id:Identifier> <end_location:@R> => ast::Expr::Name(
ast::ExprName { id: id.into(), ctx: ast::ExprContext::Store, range: (location..end_location).into() },
),
}
NamedExpression: ast::Expr = {
<location:@L> <id:Identifier> <end_location:@R> ":=" <value:Test<"all">> => {
<location:@L> <target:NamedExpressionName> ":=" <value:Test<"all">> <end_location:@R> => {
ast::Expr::NamedExpr(
ast::ExprNamedExpr {
target: Box::new(ast::Expr::Name(
ast::ExprName { id: id.into(), ctx: ast::ExprContext::Store, range: (location..end_location).into() },
)),
range: (location..value.end()).into(),
target: Box::new(target),
value: Box::new(value),
range: (location..end_location).into(),
}
)
},

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,36 @@
---
source: crates/ruff_python_parser/src/parser.rs
expression: parse_ast
---
NamedExpr(
ExprNamedExpr {
range: 1..15,
target: Name(
ExprName {
range: 1..2,
id: "x",
ctx: Store,
},
),
value: BinOp(
ExprBinOp {
range: 8..13,
left: Name(
ExprName {
range: 8..9,
id: "y",
ctx: Load,
},
),
op: Mult,
right: Name(
ExprName {
range: 12..13,
id: "z",
ctx: Load,
},
),
},
),
},
)