@@ -2665,6 +2665,74 @@ def f(*args):
2665
2665
with self .assertRaisesRegex (TypeError , msg ):
2666
2666
f ()
2667
2667
2668
+ def test_register_genericalias (self ):
2669
+ @functools .singledispatch
2670
+ def f (arg ):
2671
+ return "default"
2672
+
2673
+ with self .assertRaisesRegex (TypeError , "Invalid first argument to " ):
2674
+ f .register (list [int ], lambda arg : "types.GenericAlias" )
2675
+ with self .assertRaisesRegex (TypeError , "Invalid first argument to " ):
2676
+ f .register (typing .List [int ], lambda arg : "typing.GenericAlias" )
2677
+ with self .assertRaisesRegex (TypeError , "Invalid first argument to " ):
2678
+ f .register (list [int ] | str , lambda arg : "types.UnionTypes(types.GenericAlias)" )
2679
+ with self .assertRaisesRegex (TypeError , "Invalid first argument to " ):
2680
+ f .register (typing .List [float ] | bytes , lambda arg : "typing.Union[typing.GenericAlias]" )
2681
+ with self .assertRaisesRegex (TypeError , "Invalid first argument to " ):
2682
+ f .register (typing .Any , lambda arg : "typing.Any" )
2683
+
2684
+ self .assertEqual (f ([1 ]), "default" )
2685
+ self .assertEqual (f ([1.0 ]), "default" )
2686
+ self .assertEqual (f ("" ), "default" )
2687
+ self .assertEqual (f (b"" ), "default" )
2688
+
2689
+ def test_register_genericalias_decorator (self ):
2690
+ @functools .singledispatch
2691
+ def f (arg ):
2692
+ return "default"
2693
+
2694
+ with self .assertRaisesRegex (TypeError , "Invalid first argument to " ):
2695
+ f .register (list [int ])
2696
+ with self .assertRaisesRegex (TypeError , "Invalid first argument to " ):
2697
+ f .register (typing .List [int ])
2698
+ with self .assertRaisesRegex (TypeError , "Invalid first argument to " ):
2699
+ f .register (list [int ] | str )
2700
+ with self .assertRaisesRegex (TypeError , "Invalid first argument to " ):
2701
+ f .register (typing .List [int ] | str )
2702
+ with self .assertRaisesRegex (TypeError , "Invalid first argument to " ):
2703
+ f .register (typing .Any )
2704
+
2705
+ def test_register_genericalias_annotation (self ):
2706
+ @functools .singledispatch
2707
+ def f (arg ):
2708
+ return "default"
2709
+
2710
+ with self .assertRaisesRegex (TypeError , "Invalid annotation for 'arg'" ):
2711
+ @f .register
2712
+ def _ (arg : list [int ]):
2713
+ return "types.GenericAlias"
2714
+ with self .assertRaisesRegex (TypeError , "Invalid annotation for 'arg'" ):
2715
+ @f .register
2716
+ def _ (arg : typing .List [float ]):
2717
+ return "typing.GenericAlias"
2718
+ with self .assertRaisesRegex (TypeError , "Invalid annotation for 'arg'" ):
2719
+ @f .register
2720
+ def _ (arg : list [int ] | str ):
2721
+ return "types.UnionType(types.GenericAlias)"
2722
+ with self .assertRaisesRegex (TypeError , "Invalid annotation for 'arg'" ):
2723
+ @f .register
2724
+ def _ (arg : typing .List [float ] | bytes ):
2725
+ return "typing.Union[typing.GenericAlias]"
2726
+ with self .assertRaisesRegex (TypeError , "Invalid annotation for 'arg'" ):
2727
+ @f .register
2728
+ def _ (arg : typing .Any ):
2729
+ return "typing.Any"
2730
+
2731
+ self .assertEqual (f ([1 ]), "default" )
2732
+ self .assertEqual (f ([1.0 ]), "default" )
2733
+ self .assertEqual (f ("" ), "default" )
2734
+ self .assertEqual (f (b"" ), "default" )
2735
+
2668
2736
2669
2737
class CachedCostItem :
2670
2738
_cost = 1
0 commit comments