Reset FOR_TARGET context for all kinds of parentheses (#11009)

## Summary

This PR fixes a bug in the new parser which involves the parser context
w.r.t. for statement. This is specifically around the `in` keyword which
can be present in the target expression and shouldn't be considered to
be part of the `for` statement header. Ideally it should use a context
which is passed between functions, thus using a call stack to set /
unset a specific variant which will be done in a follow-up PR as it
requires some amount of refactor.

## Test Plan

Add test cases and update the snapshots.
This commit is contained in:
Dhruv Manilawala 2024-04-18 19:37:50 +05:30 committed by GitHub
parent 13ffb5bc19
commit 8020d486f6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 445 additions and 27 deletions

View file

@ -0,0 +1,89 @@
---
source: crates/ruff_python_parser/tests/fixtures.rs
input_file: crates/ruff_python_parser/resources/inline/err/for_in_target_postfix_expr.py
---
## AST
```
Module(
ModModule {
range: 0..29,
body: [
For(
StmtFor {
range: 0..28,
is_async: false,
target: Call(
ExprCall {
range: 4..13,
func: Name(
ExprName {
range: 4..5,
id: "d",
ctx: Load,
},
),
arguments: Arguments {
range: 5..13,
args: [
Compare(
ExprCompare {
range: 6..12,
left: Name(
ExprName {
range: 6..7,
id: "x",
ctx: Load,
},
),
ops: [
In,
],
comparators: [
Name(
ExprName {
range: 11..12,
id: "y",
ctx: Load,
},
),
],
},
),
],
keywords: [],
},
},
),
iter: Name(
ExprName {
range: 17..23,
id: "target",
ctx: Load,
},
),
body: [
Expr(
StmtExpr {
range: 25..28,
value: EllipsisLiteral(
ExprEllipsisLiteral {
range: 25..28,
},
),
},
),
],
orelse: [],
},
),
],
},
)
```
## Errors
|
1 | for d(x in y) in target: ...
| ^^^^^^^^^ Syntax Error: Invalid assignment target
|

View file

@ -7,7 +7,7 @@ input_file: crates/ruff_python_parser/resources/inline/err/parenthesized_compare
```
Module(
ModModule {
range: 0..54,
range: 0..141,
body: [
For(
StmtFor {
@ -119,6 +119,201 @@ Module(
orelse: [],
},
),
For(
StmtFor {
range: 54..82,
is_async: false,
target: Tuple(
ExprTuple {
range: 58..69,
elts: [
Compare(
ExprCompare {
range: 59..65,
left: Name(
ExprName {
range: 59..60,
id: "x",
ctx: Load,
},
),
ops: [
In,
],
comparators: [
Name(
ExprName {
range: 64..65,
id: "y",
ctx: Load,
},
),
],
},
),
Name(
ExprName {
range: 67..68,
id: "z",
ctx: Store,
},
),
],
ctx: Store,
parenthesized: true,
},
),
iter: Name(
ExprName {
range: 73..77,
id: "iter",
ctx: Load,
},
),
body: [
Expr(
StmtExpr {
range: 79..82,
value: EllipsisLiteral(
ExprEllipsisLiteral {
range: 79..82,
},
),
},
),
],
orelse: [],
},
),
For(
StmtFor {
range: 83..111,
is_async: false,
target: List(
ExprList {
range: 87..98,
elts: [
Compare(
ExprCompare {
range: 88..94,
left: Name(
ExprName {
range: 88..89,
id: "x",
ctx: Load,
},
),
ops: [
In,
],
comparators: [
Name(
ExprName {
range: 93..94,
id: "y",
ctx: Load,
},
),
],
},
),
Name(
ExprName {
range: 96..97,
id: "z",
ctx: Store,
},
),
],
ctx: Store,
},
),
iter: Name(
ExprName {
range: 102..106,
id: "iter",
ctx: Load,
},
),
body: [
Expr(
StmtExpr {
range: 108..111,
value: EllipsisLiteral(
ExprEllipsisLiteral {
range: 108..111,
},
),
},
),
],
orelse: [],
},
),
For(
StmtFor {
range: 112..140,
is_async: false,
target: Set(
ExprSet {
range: 116..127,
elts: [
Compare(
ExprCompare {
range: 117..123,
left: Name(
ExprName {
range: 117..118,
id: "x",
ctx: Load,
},
),
ops: [
In,
],
comparators: [
Name(
ExprName {
range: 122..123,
id: "y",
ctx: Load,
},
),
],
},
),
Name(
ExprName {
range: 125..126,
id: "z",
ctx: Load,
},
),
],
},
),
iter: Name(
ExprName {
range: 131..135,
id: "iter",
ctx: Load,
},
),
body: [
Expr(
StmtExpr {
range: 137..140,
value: EllipsisLiteral(
ExprEllipsisLiteral {
range: 137..140,
},
),
},
),
],
orelse: [],
},
),
],
},
)
@ -129,6 +324,7 @@ Module(
1 | for (x in y)() in iter: ...
| ^^^^^^^^^^ Syntax Error: Invalid assignment target
2 | for (x in y) in iter: ...
3 | for (x in y, z) in iter: ...
|
@ -136,4 +332,33 @@ Module(
1 | for (x in y)() in iter: ...
2 | for (x in y) in iter: ...
| ^^^^^^ Syntax Error: Invalid assignment target
3 | for (x in y, z) in iter: ...
4 | for [x in y, z] in iter: ...
|
|
1 | for (x in y)() in iter: ...
2 | for (x in y) in iter: ...
3 | for (x in y, z) in iter: ...
| ^^^^^^ Syntax Error: Invalid assignment target
4 | for [x in y, z] in iter: ...
5 | for {x in y, z} in iter: ...
|
|
2 | for (x in y) in iter: ...
3 | for (x in y, z) in iter: ...
4 | for [x in y, z] in iter: ...
| ^^^^^^ Syntax Error: Invalid assignment target
5 | for {x in y, z} in iter: ...
|
|
3 | for (x in y, z) in iter: ...
4 | for [x in y, z] in iter: ...
5 | for {x in y, z} in iter: ...
| ^^^^^^^^^^^ Syntax Error: Invalid assignment target
|

View file

@ -0,0 +1,78 @@
---
source: crates/ruff_python_parser/tests/fixtures.rs
input_file: crates/ruff_python_parser/resources/inline/ok/for_in_target_postfix_expr.py
---
## AST
```
Module(
ModModule {
range: 0..29,
body: [
For(
StmtFor {
range: 0..28,
is_async: false,
target: Subscript(
ExprSubscript {
range: 4..13,
value: Name(
ExprName {
range: 4..5,
id: "d",
ctx: Load,
},
),
slice: Compare(
ExprCompare {
range: 6..12,
left: Name(
ExprName {
range: 6..7,
id: "x",
ctx: Load,
},
),
ops: [
In,
],
comparators: [
Name(
ExprName {
range: 11..12,
id: "y",
ctx: Load,
},
),
],
},
),
ctx: Store,
},
),
iter: Name(
ExprName {
range: 17..23,
id: "target",
ctx: Load,
},
),
body: [
Expr(
StmtExpr {
range: 25..28,
value: EllipsisLiteral(
ExprEllipsisLiteral {
range: 25..28,
},
),
},
),
],
orelse: [],
},
),
],
},
)
```