@@ -2560,7 +2560,7 @@ def decorated_classmethod(cls, arg: int) -> str:
2560
2560
2561
2561
self .assertEqual (without_single_dispatch_foo , single_dispatch_foo )
2562
2562
self .assertEqual (single_dispatch_foo , '5' )
2563
-
2563
+
2564
2564
self .assertEqual (
2565
2565
WithoutSingleDispatch .decorated_classmethod (5 ),
2566
2566
WithSingleDispatch .decorated_classmethod (5 )
@@ -2655,6 +2655,74 @@ def f(*args):
2655
2655
with self .assertRaisesRegex (TypeError , msg ):
2656
2656
f ()
2657
2657
2658
+ def test_register_genericalias (self ):
2659
+ @functools .singledispatch
2660
+ def f (arg ):
2661
+ return "default"
2662
+
2663
+ with self .assertRaisesRegex (TypeError , "Invalid first argument to " ):
2664
+ f .register (list [int ], lambda arg : "types.GenericAlias" )
2665
+ with self .assertRaisesRegex (TypeError , "Invalid first argument to " ):
2666
+ f .register (typing .List [int ], lambda arg : "typing.GenericAlias" )
2667
+ with self .assertRaisesRegex (TypeError , "Invalid first argument to " ):
2668
+ f .register (typing .Union [list [int ], str ], lambda arg : "typing.Union[types.GenericAlias]" )
2669
+ with self .assertRaisesRegex (TypeError , "Invalid first argument to " ):
2670
+ f .register (typing .Union [typing .List [float ], bytes ], lambda arg : "typing.Union[typing.GenericAlias]" )
2671
+ with self .assertRaisesRegex (TypeError , "Invalid first argument to " ):
2672
+ f .register (typing .Any , lambda arg : "typing.Any" )
2673
+
2674
+ self .assertEqual (f ([1 ]), "default" )
2675
+ self .assertEqual (f ([1.0 ]), "default" )
2676
+ self .assertEqual (f ("" ), "default" )
2677
+ self .assertEqual (f (b"" ), "default" )
2678
+
2679
+ def test_register_genericalias_decorator (self ):
2680
+ @functools .singledispatch
2681
+ def f (arg ):
2682
+ return "default"
2683
+
2684
+ with self .assertRaisesRegex (TypeError , "Invalid first argument to " ):
2685
+ f .register (list [int ])
2686
+ with self .assertRaisesRegex (TypeError , "Invalid first argument to " ):
2687
+ f .register (typing .List [int ])
2688
+ with self .assertRaisesRegex (TypeError , "Invalid first argument to " ):
2689
+ f .register (typing .Union [list [int ], str ])
2690
+ with self .assertRaisesRegex (TypeError , "Invalid first argument to " ):
2691
+ f .register (typing .Union [typing .List [int ], str ])
2692
+ with self .assertRaisesRegex (TypeError , "Invalid first argument to " ):
2693
+ f .register (typing .Any )
2694
+
2695
+ def test_register_genericalias_annotation (self ):
2696
+ @functools .singledispatch
2697
+ def f (arg ):
2698
+ return "default"
2699
+
2700
+ with self .assertRaisesRegex (TypeError , "Invalid annotation for 'arg'" ):
2701
+ @f .register
2702
+ def _ (arg : list [int ]):
2703
+ return "types.GenericAlias"
2704
+ with self .assertRaisesRegex (TypeError , "Invalid annotation for 'arg'" ):
2705
+ @f .register
2706
+ def _ (arg : typing .List [float ]):
2707
+ return "typing.GenericAlias"
2708
+ with self .assertRaisesRegex (TypeError , "Invalid annotation for 'arg'" ):
2709
+ @f .register
2710
+ def _ (arg : typing .Union [list [int ], str ]):
2711
+ return "types.UnionType(types.GenericAlias)"
2712
+ with self .assertRaisesRegex (TypeError , "Invalid annotation for 'arg'" ):
2713
+ @f .register
2714
+ def _ (arg : typing .Union [typing .List [float ], bytes ]):
2715
+ return "typing.Union[typing.GenericAlias]"
2716
+ with self .assertRaisesRegex (TypeError , "Invalid annotation for 'arg'" ):
2717
+ @f .register
2718
+ def _ (arg : typing .Any ):
2719
+ return "typing.Any"
2720
+
2721
+ self .assertEqual (f ([1 ]), "default" )
2722
+ self .assertEqual (f ([1.0 ]), "default" )
2723
+ self .assertEqual (f ("" ), "default" )
2724
+ self .assertEqual (f (b"" ), "default" )
2725
+
2658
2726
2659
2727
class CachedCostItem :
2660
2728
_cost = 1
0 commit comments