Skip to content

Commit 1890778

Browse files
committed
Add regression test for match-like annotated assignments (#22528)
1 parent 5daf99a commit 1890778

3 files changed

Lines changed: 125 additions & 0 deletions

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
match[0]: int
2+
match [x, y, z]: dict

crates/ruff_python_parser/src/parser/statement.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2448,6 +2448,9 @@ impl<'src> Parser<'src> {
24482448
let subject = self.parse_match_subject_expression();
24492449

24502450
match self.current_token_kind() {
2451+
// test_ok match_annotated_assignment
2452+
// match[0]: int
2453+
// match [x, y, z]: dict
24512454
TokenKind::Colon if self.peek() == TokenKind::Newline => {
24522455
// `match` is a keyword — colon followed by newline confirms
24532456
// this is a match statement, not an annotated assignment like
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
---
2+
source: crates/ruff_python_parser/tests/fixtures.rs
3+
---
4+
## AST
5+
6+
```
7+
Module(
8+
ModModule {
9+
node_index: NodeIndex(None),
10+
range: 0..36,
11+
body: [
12+
AnnAssign(
13+
StmtAnnAssign {
14+
node_index: NodeIndex(None),
15+
range: 0..13,
16+
target: Subscript(
17+
ExprSubscript {
18+
node_index: NodeIndex(None),
19+
range: 0..8,
20+
value: Name(
21+
ExprName {
22+
node_index: NodeIndex(None),
23+
range: 0..5,
24+
id: Name("match"),
25+
ctx: Load,
26+
},
27+
),
28+
slice: NumberLiteral(
29+
ExprNumberLiteral {
30+
node_index: NodeIndex(None),
31+
range: 6..7,
32+
value: Int(
33+
0,
34+
),
35+
},
36+
),
37+
ctx: Store,
38+
},
39+
),
40+
annotation: Name(
41+
ExprName {
42+
node_index: NodeIndex(None),
43+
range: 10..13,
44+
id: Name("int"),
45+
ctx: Load,
46+
},
47+
),
48+
value: None,
49+
simple: false,
50+
},
51+
),
52+
AnnAssign(
53+
StmtAnnAssign {
54+
node_index: NodeIndex(None),
55+
range: 14..35,
56+
target: Subscript(
57+
ExprSubscript {
58+
node_index: NodeIndex(None),
59+
range: 14..29,
60+
value: Name(
61+
ExprName {
62+
node_index: NodeIndex(None),
63+
range: 14..19,
64+
id: Name("match"),
65+
ctx: Load,
66+
},
67+
),
68+
slice: Tuple(
69+
ExprTuple {
70+
node_index: NodeIndex(None),
71+
range: 21..28,
72+
elts: [
73+
Name(
74+
ExprName {
75+
node_index: NodeIndex(None),
76+
range: 21..22,
77+
id: Name("x"),
78+
ctx: Load,
79+
},
80+
),
81+
Name(
82+
ExprName {
83+
node_index: NodeIndex(None),
84+
range: 24..25,
85+
id: Name("y"),
86+
ctx: Load,
87+
},
88+
),
89+
Name(
90+
ExprName {
91+
node_index: NodeIndex(None),
92+
range: 27..28,
93+
id: Name("z"),
94+
ctx: Load,
95+
},
96+
),
97+
],
98+
ctx: Load,
99+
parenthesized: false,
100+
},
101+
),
102+
ctx: Store,
103+
},
104+
),
105+
annotation: Name(
106+
ExprName {
107+
node_index: NodeIndex(None),
108+
range: 31..35,
109+
id: Name("dict"),
110+
ctx: Load,
111+
},
112+
),
113+
value: None,
114+
simple: false,
115+
},
116+
),
117+
],
118+
},
119+
)
120+
```

0 commit comments

Comments
 (0)