Skip to content

Commit 11b1ffd

Browse files
committed
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.
1 parent 4431978 commit 11b1ffd

File tree

4 files changed

+117
-0
lines changed

4 files changed

+117
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# parse_options: { "target_version": "3.7" }
2+
if x := 1: ...
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# parse_options: { "target_version": "3.8" }
2+
if x := 1: ...
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
---
2+
source: crates/ruff_python_parser/tests/fixtures.rs
3+
input_file: crates/ruff_python_parser/resources/inline/err/walrus_before_py38.py
4+
---
5+
## AST
6+
7+
```
8+
Module(
9+
ModModule {
10+
range: 0..60,
11+
body: [
12+
If(
13+
StmtIf {
14+
range: 45..59,
15+
test: Named(
16+
ExprNamed {
17+
range: 48..54,
18+
target: Name(
19+
ExprName {
20+
range: 48..49,
21+
id: Name("x"),
22+
ctx: Store,
23+
},
24+
),
25+
value: NumberLiteral(
26+
ExprNumberLiteral {
27+
range: 53..54,
28+
value: Int(
29+
1,
30+
),
31+
},
32+
),
33+
},
34+
),
35+
body: [
36+
Expr(
37+
StmtExpr {
38+
range: 56..59,
39+
value: EllipsisLiteral(
40+
ExprEllipsisLiteral {
41+
range: 56..59,
42+
},
43+
),
44+
},
45+
),
46+
],
47+
elif_else_clauses: [],
48+
},
49+
),
50+
],
51+
},
52+
)
53+
```
54+
## Unsupported Syntax Errors
55+
56+
|
57+
1 | # parse_options: { "target_version": "3.7" }
58+
2 | if x := 1: ...
59+
| ^^^^^^ Syntax Error: Cannot use named assignment expression (`:=`) on Python 3.7 (syntax was added in Python 3.8)
60+
|
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
---
2+
source: crates/ruff_python_parser/tests/fixtures.rs
3+
input_file: crates/ruff_python_parser/resources/inline/ok/walrus_after_py38.py
4+
---
5+
## AST
6+
7+
```
8+
Module(
9+
ModModule {
10+
range: 0..60,
11+
body: [
12+
If(
13+
StmtIf {
14+
range: 45..59,
15+
test: Named(
16+
ExprNamed {
17+
range: 48..54,
18+
target: Name(
19+
ExprName {
20+
range: 48..49,
21+
id: Name("x"),
22+
ctx: Store,
23+
},
24+
),
25+
value: NumberLiteral(
26+
ExprNumberLiteral {
27+
range: 53..54,
28+
value: Int(
29+
1,
30+
),
31+
},
32+
),
33+
},
34+
),
35+
body: [
36+
Expr(
37+
StmtExpr {
38+
range: 56..59,
39+
value: EllipsisLiteral(
40+
ExprEllipsisLiteral {
41+
range: 56..59,
42+
},
43+
),
44+
},
45+
),
46+
],
47+
elif_else_clauses: [],
48+
},
49+
),
50+
],
51+
},
52+
)
53+
```

0 commit comments

Comments
 (0)