-
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.
Detect assignment expressions before Python 3.8
This PR is the first in a series derived from #16308, each of which add support for detecting one version-related syntax error from #6591. This one should be the largest because it also includes a couple of additional changes: 1. the `syntax_errors!` macro, which makes adding more variants a bit easier 2. the `Parser::add_unsupported_syntax_error` method Otherwise I think the general structure will be the same for each syntax error: * Detecting the error in the parser * Inline parser tests for the new error * New ruff CLI tests for the new error Because of the second point here, this PR is currently stacked on #16357. As noted above, there are new inline parser tests, as well as new ruff CLI tests. Once #16379 is resolved, there should also be new mdtests for red-knot, but this PR does not currently include those.
- Loading branch information
Showing
7 changed files
with
176 additions
and
3 deletions.
There are no files selected for viewing
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
2 changes: 2 additions & 0 deletions
2
crates/ruff_python_parser/resources/inline/err/walrus_before_py38.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,2 @@ | ||
# parse_options: { "target_version": "3.7" } | ||
if x := 1: ... |
2 changes: 2 additions & 0 deletions
2
crates/ruff_python_parser/resources/inline/ok/walrus_after_py38.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,2 @@ | ||
# parse_options: { "target_version": "3.8" } | ||
if x := 1: ... |
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
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
60 changes: 60 additions & 0 deletions
60
crates/ruff_python_parser/tests/snapshots/invalid_syntax@walrus_before_py38.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,60 @@ | ||
--- | ||
source: crates/ruff_python_parser/tests/fixtures.rs | ||
input_file: crates/ruff_python_parser/resources/inline/err/walrus_before_py38.py | ||
--- | ||
## AST | ||
|
||
``` | ||
Module( | ||
ModModule { | ||
range: 0..60, | ||
body: [ | ||
If( | ||
StmtIf { | ||
range: 45..59, | ||
test: Named( | ||
ExprNamed { | ||
range: 48..54, | ||
target: Name( | ||
ExprName { | ||
range: 48..49, | ||
id: Name("x"), | ||
ctx: Store, | ||
}, | ||
), | ||
value: NumberLiteral( | ||
ExprNumberLiteral { | ||
range: 53..54, | ||
value: Int( | ||
1, | ||
), | ||
}, | ||
), | ||
}, | ||
), | ||
body: [ | ||
Expr( | ||
StmtExpr { | ||
range: 56..59, | ||
value: EllipsisLiteral( | ||
ExprEllipsisLiteral { | ||
range: 56..59, | ||
}, | ||
), | ||
}, | ||
), | ||
], | ||
elif_else_clauses: [], | ||
}, | ||
), | ||
], | ||
}, | ||
) | ||
``` | ||
## Unsupported Syntax Errors | ||
|
||
| | ||
1 | # parse_options: { "target_version": "3.7" } | ||
2 | if x := 1: ... | ||
| ^^^^^^ Syntax Error: Cannot use named assignment expression (`:=`) on Python 3.7 (syntax was added in Python 3.8) | ||
| |
53 changes: 53 additions & 0 deletions
53
crates/ruff_python_parser/tests/snapshots/valid_syntax@walrus_after_py38.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,53 @@ | ||
--- | ||
source: crates/ruff_python_parser/tests/fixtures.rs | ||
input_file: crates/ruff_python_parser/resources/inline/ok/walrus_after_py38.py | ||
--- | ||
## AST | ||
|
||
``` | ||
Module( | ||
ModModule { | ||
range: 0..60, | ||
body: [ | ||
If( | ||
StmtIf { | ||
range: 45..59, | ||
test: Named( | ||
ExprNamed { | ||
range: 48..54, | ||
target: Name( | ||
ExprName { | ||
range: 48..49, | ||
id: Name("x"), | ||
ctx: Store, | ||
}, | ||
), | ||
value: NumberLiteral( | ||
ExprNumberLiteral { | ||
range: 53..54, | ||
value: Int( | ||
1, | ||
), | ||
}, | ||
), | ||
}, | ||
), | ||
body: [ | ||
Expr( | ||
StmtExpr { | ||
range: 56..59, | ||
value: EllipsisLiteral( | ||
ExprEllipsisLiteral { | ||
range: 56..59, | ||
}, | ||
), | ||
}, | ||
), | ||
], | ||
elif_else_clauses: [], | ||
}, | ||
), | ||
], | ||
}, | ||
) | ||
``` |