Debian Codegraph @

test-data/unit/parse-python310.test

raw
1 -- Test cases for parser -- Python 3.10 syntax (match statement)
2 --
3 -- See parse.test for a description of this file format.
4
5 [case testSimpleMatch]
6 match a:
7 case 1:
8 pass
9 [out]
10 MypyFile:1(
11 MatchStmt:1(
12 NameExpr(a)
13 Pattern(
14 ValuePattern:2(
15 IntExpr(1)))
16 Body(
17 PassStmt:3())))
18
19
20 [case testTupleMatch]
21 match a, b:
22 case 1:
23 pass
24 [out]
25 MypyFile:1(
26 MatchStmt:1(
27 TupleExpr:1(
28 NameExpr(a)
29 NameExpr(b))
30 Pattern(
31 ValuePattern:2(
32 IntExpr(1)))
33 Body(
34 PassStmt:3())))
35
36 [case testMatchWithGuard]
37 match a:
38 case 1 if f():
39 pass
40 case d if d > 5:
41 pass
42 [out]
43 MypyFile:1(
44 MatchStmt:1(
45 NameExpr(a)
46 Pattern(
47 ValuePattern:2(
48 IntExpr(1)))
49 Guard(
50 CallExpr:2(
51 NameExpr(f)
52 Args()))
53 Body(
54 PassStmt:3())
55 Pattern(
56 AsPattern:4(
57 NameExpr(d)))
58 Guard(
59 ComparisonExpr:4(
60 >
61 NameExpr(d)
62 IntExpr(5)))
63 Body(
64 PassStmt:5())))
65
66 [case testAsPattern]
67 match a:
68 case 1 as b:
69 pass
70 [out]
71 MypyFile:1(
72 MatchStmt:1(
73 NameExpr(a)
74 Pattern(
75 AsPattern:2(
76 ValuePattern:2(
77 IntExpr(1))
78 NameExpr(b)))
79 Body(
80 PassStmt:3())))
81
82
83 [case testLiteralPattern]
84 match a:
85 case 1:
86 pass
87 case -1:
88 pass
89 case 1+2j:
90 pass
91 case -1+2j:
92 pass
93 case 1-2j:
94 pass
95 case -1-2j:
96 pass
97 case "str":
98 pass
99 case b"bytes":
100 pass
101 case r"raw_string":
102 pass
103 case None:
104 pass
105 case True:
106 pass
107 case False:
108 pass
109 [out]
110 MypyFile:1(
111 MatchStmt:1(
112 NameExpr(a)
113 Pattern(
114 ValuePattern:2(
115 IntExpr(1)))
116 Body(
117 PassStmt:3())
118 Pattern(
119 ValuePattern:4(
120 UnaryExpr:4(
121 -
122 IntExpr(1))))
123 Body(
124 PassStmt:5())
125 Pattern(
126 ValuePattern:6(
127 OpExpr:6(
128 +
129 IntExpr(1)
130 ComplexExpr(2j))))
131 Body(
132 PassStmt:7())
133 Pattern(
134 ValuePattern:8(
135 OpExpr:8(
136 +
137 UnaryExpr:8(
138 -
139 IntExpr(1))
140 ComplexExpr(2j))))
141 Body(
142 PassStmt:9())
143 Pattern(
144 ValuePattern:10(
145 OpExpr:10(
146 -
147 IntExpr(1)
148 ComplexExpr(2j))))
149 Body(
150 PassStmt:11())
151 Pattern(
152 ValuePattern:12(
153 OpExpr:12(
154 -
155 UnaryExpr:12(
156 -
157 IntExpr(1))
158 ComplexExpr(2j))))
159 Body(
160 PassStmt:13())
161 Pattern(
162 ValuePattern:14(
163 StrExpr(str)))
164 Body(
165 PassStmt:15())
166 Pattern(
167 ValuePattern:16(
168 BytesExpr(bytes)))
169 Body(
170 PassStmt:17())
171 Pattern(
172 ValuePattern:18(
173 StrExpr(raw_string)))
174 Body(
175 PassStmt:19())
176 Pattern(
177 SingletonPattern:20())
178 Body(
179 PassStmt:21())
180 Pattern(
181 SingletonPattern:22(
182 True))
183 Body(
184 PassStmt:23())
185 Pattern(
186 SingletonPattern:24(
187 False))
188 Body(
189 PassStmt:25())))
190
191 [case testCapturePattern]
192 match a:
193 case x:
194 pass
195 case longName:
196 pass
197 [out]
198 MypyFile:1(
199 MatchStmt:1(
200 NameExpr(a)
201 Pattern(
202 AsPattern:2(
203 NameExpr(x)))
204 Body(
205 PassStmt:3())
206 Pattern(
207 AsPattern:4(
208 NameExpr(longName)))
209 Body(
210 PassStmt:5())))
211
212 [case testWildcardPattern]
213 match a:
214 case _:
215 pass
216 [out]
217 MypyFile:1(
218 MatchStmt:1(
219 NameExpr(a)
220 Pattern(
221 AsPattern:2())
222 Body(
223 PassStmt:3())))
224
225 [case testValuePattern]
226 match a:
227 case b.c:
228 pass
229 case b.c.d.e.f:
230 pass
231 [out]
232 MypyFile:1(
233 MatchStmt:1(
234 NameExpr(a)
235 Pattern(
236 ValuePattern:2(
237 MemberExpr:2(
238 NameExpr(b)
239 c)))
240 Body(
241 PassStmt:3())
242 Pattern(
243 ValuePattern:4(
244 MemberExpr:4(
245 MemberExpr:4(
246 MemberExpr:4(
247 MemberExpr:4(
248 NameExpr(b)
249 c)
250 d)
251 e)
252 f)))
253 Body(
254 PassStmt:5())))
255
256 [case testGroupPattern]
257 # This is optimized out by the compiler. It doesn't appear in the ast
258 match a:
259 case (1):
260 pass
261 [out]
262 MypyFile:1(
263 MatchStmt:2(
264 NameExpr(a)
265 Pattern(
266 ValuePattern:3(
267 IntExpr(1)))
268 Body(
269 PassStmt:4())))
270
271 [case testSequencePattern]
272 match a:
273 case []:
274 pass
275 case ():
276 pass
277 case [1]:
278 pass
279 case (1,):
280 pass
281 case 1,:
282 pass
283 case [1, 2, 3]:
284 pass
285 case (1, 2, 3):
286 pass
287 case 1, 2, 3:
288 pass
289 case [1, *a, 2]:
290 pass
291 case (1, *a, 2):
292 pass
293 case 1, *a, 2:
294 pass
295 case [1, *_, 2]:
296 pass
297 case (1, *_, 2):
298 pass
299 case 1, *_, 2:
300 pass
301 [out]
302 MypyFile:1(
303 MatchStmt:1(
304 NameExpr(a)
305 Pattern(
306 SequencePattern:2())
307 Body(
308 PassStmt:3())
309 Pattern(
310 SequencePattern:4())
311 Body(
312 PassStmt:5())
313 Pattern(
314 SequencePattern:6(
315 ValuePattern:6(
316 IntExpr(1))))
317 Body(
318 PassStmt:7())
319 Pattern(
320 SequencePattern:8(
321 ValuePattern:8(
322 IntExpr(1))))
323 Body(
324 PassStmt:9())
325 Pattern(
326 SequencePattern:10(
327 ValuePattern:10(
328 IntExpr(1))))
329 Body(
330 PassStmt:11())
331 Pattern(
332 SequencePattern:12(
333 ValuePattern:12(
334 IntExpr(1))
335 ValuePattern:12(
336 IntExpr(2))
337 ValuePattern:12(
338 IntExpr(3))))
339 Body(
340 PassStmt:13())
341 Pattern(
342 SequencePattern:14(
343 ValuePattern:14(
344 IntExpr(1))
345 ValuePattern:14(
346 IntExpr(2))
347 ValuePattern:14(
348 IntExpr(3))))
349 Body(
350 PassStmt:15())
351 Pattern(
352 SequencePattern:16(
353 ValuePattern:16(
354 IntExpr(1))
355 ValuePattern:16(
356 IntExpr(2))
357 ValuePattern:16(
358 IntExpr(3))))
359 Body(
360 PassStmt:17())
361 Pattern(
362 SequencePattern:18(
363 ValuePattern:18(
364 IntExpr(1))
365 StarredPattern:18(
366 NameExpr(a))
367 ValuePattern:18(
368 IntExpr(2))))
369 Body(
370 PassStmt:19())
371 Pattern(
372 SequencePattern:20(
373 ValuePattern:20(
374 IntExpr(1))
375 StarredPattern:20(
376 NameExpr(a))
377 ValuePattern:20(
378 IntExpr(2))))
379 Body(
380 PassStmt:21())
381 Pattern(
382 SequencePattern:22(
383 ValuePattern:22(
384 IntExpr(1))
385 StarredPattern:22(
386 NameExpr(a))
387 ValuePattern:22(
388 IntExpr(2))))
389 Body(
390 PassStmt:23())
391 Pattern(
392 SequencePattern:24(
393 ValuePattern:24(
394 IntExpr(1))
395 StarredPattern:24()
396 ValuePattern:24(
397 IntExpr(2))))
398 Body(
399 PassStmt:25())
400 Pattern(
401 SequencePattern:26(
402 ValuePattern:26(
403 IntExpr(1))
404 StarredPattern:26()
405 ValuePattern:26(
406 IntExpr(2))))
407 Body(
408 PassStmt:27())
409 Pattern(
410 SequencePattern:28(
411 ValuePattern:28(
412 IntExpr(1))
413 StarredPattern:28()
414 ValuePattern:28(
415 IntExpr(2))))
416 Body(
417 PassStmt:29())))
418
419 [case testMappingPattern]
420 match a:
421 case {'k': v}:
422 pass
423 case {a.b: v}:
424 pass
425 case {1: v}:
426 pass
427 case {a.c: v}:
428 pass
429 case {'k': v1, a.b: v2, 1: v3, a.c: v4}:
430 pass
431 case {'k1': 1, 'k2': "str", 'k3': b'bytes', 'k4': None}:
432 pass
433 case {'k': v, **r}:
434 pass
435 case {**r}:
436 pass
437 [out]
438 MypyFile:1(
439 MatchStmt:1(
440 NameExpr(a)
441 Pattern(
442 MappingPattern:2(
443 Key(
444 StrExpr(k))
445 Value(
446 AsPattern:2(
447 NameExpr(v)))))
448 Body(
449 PassStmt:3())
450 Pattern(
451 MappingPattern:4(
452 Key(
453 MemberExpr:4(
454 NameExpr(a)
455 b))
456 Value(
457 AsPattern:4(
458 NameExpr(v)))))
459 Body(
460 PassStmt:5())
461 Pattern(
462 MappingPattern:6(
463 Key(
464 IntExpr(1))
465 Value(
466 AsPattern:6(
467 NameExpr(v)))))
468 Body(
469 PassStmt:7())
470 Pattern(
471 MappingPattern:8(
472 Key(
473 MemberExpr:8(
474 NameExpr(a)
475 c))
476 Value(
477 AsPattern:8(
478 NameExpr(v)))))
479 Body(
480 PassStmt:9())
481 Pattern(
482 MappingPattern:10(
483 Key(
484 StrExpr(k))
485 Value(
486 AsPattern:10(
487 NameExpr(v1)))
488 Key(
489 MemberExpr:10(
490 NameExpr(a)
491 b))
492 Value(
493 AsPattern:10(
494 NameExpr(v2)))
495 Key(
496 IntExpr(1))
497 Value(
498 AsPattern:10(
499 NameExpr(v3)))
500 Key(
501 MemberExpr:10(
502 NameExpr(a)
503 c))
504 Value(
505 AsPattern:10(
506 NameExpr(v4)))))
507 Body(
508 PassStmt:11())
509 Pattern(
510 MappingPattern:12(
511 Key(
512 StrExpr(k1))
513 Value(
514 ValuePattern:12(
515 IntExpr(1)))
516 Key(
517 StrExpr(k2))
518 Value(
519 ValuePattern:12(
520 StrExpr(str)))
521 Key(
522 StrExpr(k3))
523 Value(
524 ValuePattern:12(
525 BytesExpr(bytes)))
526 Key(
527 StrExpr(k4))
528 Value(
529 SingletonPattern:12())))
530 Body(
531 PassStmt:13())
532 Pattern(
533 MappingPattern:14(
534 Key(
535 StrExpr(k))
536 Value(
537 AsPattern:14(
538 NameExpr(v)))
539 Rest(
540 NameExpr(r))))
541 Body(
542 PassStmt:15())
543 Pattern(
544 MappingPattern:16(
545 Rest(
546 NameExpr(r))))
547 Body(
548 PassStmt:17())))
549
550 [case testClassPattern]
551 match a:
552 case A():
553 pass
554 case B(1, 2):
555 pass
556 case B(1, b=2):
557 pass
558 case B(a=1, b=2):
559 pass
560 [out]
561 MypyFile:1(
562 MatchStmt:1(
563 NameExpr(a)
564 Pattern(
565 ClassPattern:2(
566 NameExpr(A)))
567 Body(
568 PassStmt:3())
569 Pattern(
570 ClassPattern:4(
571 NameExpr(B)
572 Positionals(
573 ValuePattern:4(
574 IntExpr(1))
575 ValuePattern:4(
576 IntExpr(2)))))
577 Body(
578 PassStmt:5())
579 Pattern(
580 ClassPattern:6(
581 NameExpr(B)
582 Positionals(
583 ValuePattern:6(
584 IntExpr(1)))
585 Keyword(
586 b
587 ValuePattern:6(
588 IntExpr(2)))))
589 Body(
590 PassStmt:7())
591 Pattern(
592 ClassPattern:8(
593 NameExpr(B)
594 Keyword(
595 a
596 ValuePattern:8(
597 IntExpr(1)))
598 Keyword(
599 b
600 ValuePattern:8(
601 IntExpr(2)))))
602 Body(
603 PassStmt:9())))