]>
Commit | Line | Data |
---|---|---|
53e6db90 DC |
1 | from sys import version_info |
2 | ||
3 | from pyflakes.test.harness import TestCase, skipIf | |
4 | ||
5 | ||
6 | @skipIf(version_info < (3, 10), "Python >= 3.10 only") | |
7 | class TestMatch(TestCase): | |
8 | def test_match_bindings(self): | |
9 | self.flakes(''' | |
10 | def f(): | |
11 | x = 1 | |
12 | match x: | |
13 | case 1 as y: | |
14 | print(f'matched as {y}') | |
15 | ''') | |
16 | self.flakes(''' | |
17 | def f(): | |
18 | x = [1, 2, 3] | |
19 | match x: | |
20 | case [1, y, 3]: | |
21 | print(f'matched {y}') | |
22 | ''') | |
23 | self.flakes(''' | |
24 | def f(): | |
25 | x = {'foo': 1} | |
26 | match x: | |
27 | case {'foo': y}: | |
28 | print(f'matched {y}') | |
29 | ''') | |
30 | ||
31 | def test_match_pattern_matched_class(self): | |
32 | self.flakes(''' | |
33 | from a import B | |
34 | ||
35 | match 1: | |
36 | case B(x=1) as y: | |
37 | print(f'matched {y}') | |
38 | ''') | |
39 | self.flakes(''' | |
40 | from a import B | |
41 | ||
42 | match 1: | |
43 | case B(a, x=z) as y: | |
44 | print(f'matched {y} {a} {z}') | |
45 | ''') | |
46 | ||
47 | def test_match_placeholder(self): | |
48 | self.flakes(''' | |
49 | def f(): | |
50 | match 1: | |
51 | case _: | |
52 | print('catchall!') | |
53 | ''') | |
54 | ||
55 | def test_match_singleton(self): | |
56 | self.flakes(''' | |
57 | match 1: | |
58 | case True: | |
59 | print('true') | |
60 | ''') | |
61 | ||
62 | def test_match_or_pattern(self): | |
63 | self.flakes(''' | |
64 | match 1: | |
65 | case 1 | 2: | |
66 | print('one or two') | |
67 | ''') | |
68 | ||
69 | def test_match_star(self): | |
70 | self.flakes(''' | |
71 | x = [1, 2, 3] | |
72 | match x: | |
73 | case [1, *y]: | |
74 | print(f'captured: {y}') | |
75 | ''') | |
76 | ||
77 | def test_match_double_star(self): | |
78 | self.flakes(''' | |
79 | x = {'foo': 'bar', 'baz': 'womp'} | |
80 | match x: | |
81 | case {'foo': k1, **rest}: | |
82 | print(f'{k1=} {rest=}') | |
83 | ''') | |
84 | ||
85 | def test_defined_in_different_branches(self): | |
86 | self.flakes(''' | |
87 | def f(x): | |
88 | match x: | |
89 | case 1: | |
90 | def y(): pass | |
91 | case _: | |
92 | def y(): print(1) | |
93 | return y | |
94 | ''') |