@@ -2460,12 +2460,27 @@ class Eq:
2460
2460
def __eq__ (self , other ):
2461
2461
return True
2462
2462
x = eq = Eq ()
2463
+ # None
2463
2464
y = None
2464
2465
match x :
2465
2466
case None :
2466
2467
y = 0
2467
2468
self .assertIs (x , eq )
2468
2469
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 )
2469
2484
2470
2485
def test_patma_233 (self ):
2471
2486
x = False
@@ -2668,6 +2683,83 @@ def f(self, x):
2668
2683
setattr (c , "__attr" , "spam" ) # setattr is needed because we're in a class scope
2669
2684
self .assertEqual (Outer ().f (c ), "spam" )
2670
2685
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
+
2671
2763
2672
2764
class TestSyntaxErrors (unittest .TestCase ):
2673
2765
@@ -2885,6 +2977,37 @@ def test_real_number_required_in_complex_literal_3(self):
2885
2977
pass
2886
2978
""" )
2887
2979
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
+
2888
3011
def test_wildcard_makes_remaining_patterns_unreachable_0 (self ):
2889
3012
self .assert_syntax_error ("""
2890
3013
match ...:
@@ -3067,6 +3190,14 @@ class Class:
3067
3190
self .assertIs (y , None )
3068
3191
self .assertIs (z , None )
3069
3192
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
+
3070
3201
3071
3202
class TestValueErrors (unittest .TestCase ):
3072
3203
0 commit comments