-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
1 parent
13ffb5b
commit 8020d48
Showing
7 changed files
with
445 additions
and
27 deletions.
There are no files selected for viewing
1 change: 1 addition & 0 deletions
1
crates/ruff_python_parser/resources/inline/err/for_in_target_postfix_expr.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
for d(x in y) in target: ... |
3 changes: 3 additions & 0 deletions
3
crates/ruff_python_parser/resources/inline/err/parenthesized_compare_expr_in_for.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
for (x in y)() in iter: ... | ||
for (x in y) in iter: ... | ||
for (x in y, z) in iter: ... | ||
for [x in y, z] in iter: ... | ||
for {x in y, z} in iter: ... |
1 change: 1 addition & 0 deletions
1
crates/ruff_python_parser/resources/inline/ok/for_in_target_postfix_expr.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
for d[x in y] in target: ... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
crates/ruff_python_parser/tests/snapshots/invalid_syntax@for_in_target_postfix_expr.py.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
| |
Oops, something went wrong.