Skip to content

Commit 904aef9

Browse files
authored
GH-106135: Add more edge-"cases" to test_patma (GH-106271)
1 parent 2062e11 commit 904aef9

File tree

1 file changed

+131
-0
lines changed

1 file changed

+131
-0
lines changed

Lib/test/test_patma.py

+131
Original file line numberDiff line numberDiff line change
@@ -2460,12 +2460,27 @@ class Eq:
24602460
def __eq__(self, other):
24612461
return True
24622462
x = eq = Eq()
2463+
# None
24632464
y = None
24642465
match x:
24652466
case None:
24662467
y = 0
24672468
self.assertIs(x, eq)
24682469
self.assertEqual(y, None)
2470+
# True
2471+
y = None
2472+
match x:
2473+
case True:
2474+
y = 0
2475+
self.assertIs(x, eq)
2476+
self.assertEqual(y, None)
2477+
# False
2478+
y = None
2479+
match x:
2480+
case False:
2481+
y = 0
2482+
self.assertIs(x, eq)
2483+
self.assertEqual(y, None)
24692484

24702485
def test_patma_233(self):
24712486
x = False
@@ -2668,6 +2683,83 @@ def f(self, x):
26682683
setattr(c, "__attr", "spam") # setattr is needed because we're in a class scope
26692684
self.assertEqual(Outer().f(c), "spam")
26702685

2686+
def test_patma_250(self):
2687+
def f(x):
2688+
match x:
2689+
case {"foo": y} if y >= 0:
2690+
return True
2691+
case {"foo": y} if y < 0:
2692+
return False
2693+
2694+
self.assertIs(f({"foo": 1}), True)
2695+
self.assertIs(f({"foo": -1}), False)
2696+
2697+
def test_patma_251(self):
2698+
def f(v, x):
2699+
match v:
2700+
case x.attr if x.attr >= 0:
2701+
return True
2702+
case x.attr if x.attr < 0:
2703+
return False
2704+
case _:
2705+
return None
2706+
2707+
class X:
2708+
def __init__(self, attr):
2709+
self.attr = attr
2710+
2711+
self.assertIs(f(1, X(1)), True)
2712+
self.assertIs(f(-1, X(-1)), False)
2713+
self.assertIs(f(1, X(-1)), None)
2714+
2715+
def test_patma_252(self):
2716+
# Side effects must be possible in guards:
2717+
effects = []
2718+
def lt(x, y):
2719+
effects.append((x, y))
2720+
return x < y
2721+
2722+
res = None
2723+
match {"foo": 1}:
2724+
case {"foo": x} if lt(x, 0):
2725+
res = 0
2726+
case {"foo": x} if lt(x, 1):
2727+
res = 1
2728+
case {"foo": x} if lt(x, 2):
2729+
res = 2
2730+
2731+
self.assertEqual(res, 2)
2732+
self.assertEqual(effects, [(1, 0), (1, 1), (1, 2)])
2733+
2734+
def test_patma_253(self):
2735+
def f(v):
2736+
match v:
2737+
case [x] | x:
2738+
return x
2739+
2740+
self.assertEqual(f(1), 1)
2741+
self.assertEqual(f([1]), 1)
2742+
2743+
def test_patma_254(self):
2744+
def f(v):
2745+
match v:
2746+
case {"x": x} | x:
2747+
return x
2748+
2749+
self.assertEqual(f(1), 1)
2750+
self.assertEqual(f({"x": 1}), 1)
2751+
2752+
def test_patma_255(self):
2753+
x = []
2754+
match x:
2755+
case [] as z if z.append(None):
2756+
y = 0
2757+
case [None]:
2758+
y = 1
2759+
self.assertEqual(x, [None])
2760+
self.assertEqual(y, 1)
2761+
self.assertIs(z, x)
2762+
26712763

26722764
class TestSyntaxErrors(unittest.TestCase):
26732765

@@ -2885,6 +2977,37 @@ def test_real_number_required_in_complex_literal_3(self):
28852977
pass
28862978
""")
28872979

2980+
def test_real_number_multiple_ops(self):
2981+
self.assert_syntax_error("""
2982+
match ...:
2983+
case 0 + 0j + 0:
2984+
pass
2985+
""")
2986+
2987+
def test_real_number_wrong_ops(self):
2988+
for op in ["*", "/", "@", "**", "%", "//"]:
2989+
with self.subTest(op=op):
2990+
self.assert_syntax_error(f"""
2991+
match ...:
2992+
case 0 {op} 0j:
2993+
pass
2994+
""")
2995+
self.assert_syntax_error(f"""
2996+
match ...:
2997+
case 0j {op} 0:
2998+
pass
2999+
""")
3000+
self.assert_syntax_error(f"""
3001+
match ...:
3002+
case -0j {op} 0:
3003+
pass
3004+
""")
3005+
self.assert_syntax_error(f"""
3006+
match ...:
3007+
case 0j {op} -0:
3008+
pass
3009+
""")
3010+
28883011
def test_wildcard_makes_remaining_patterns_unreachable_0(self):
28893012
self.assert_syntax_error("""
28903013
match ...:
@@ -3067,6 +3190,14 @@ class Class:
30673190
self.assertIs(y, None)
30683191
self.assertIs(z, None)
30693192

3193+
def test_class_pattern_not_type(self):
3194+
w = None
3195+
with self.assertRaises(TypeError):
3196+
match 1:
3197+
case max(0, 1):
3198+
w = 0
3199+
self.assertIsNone(w)
3200+
30703201

30713202
class TestValueErrors(unittest.TestCase):
30723203

0 commit comments

Comments
 (0)