@@ -2722,6 +2722,74 @@ def _(arg: int | float):
2722
2722
self .assertEqual (f (1 ), "types.UnionType" )
2723
2723
self .assertEqual (f (1.0 ), "types.UnionType" )
2724
2724
2725
+ def test_register_genericalias (self ):
2726
+ @functools .singledispatch
2727
+ def f (arg ):
2728
+ return "default"
2729
+
2730
+ with self .assertRaisesRegex (TypeError , "Invalid first argument to " ):
2731
+ f .register (list [int ], lambda arg : "types.GenericAlias" )
2732
+ with self .assertRaisesRegex (TypeError , "Invalid first argument to " ):
2733
+ f .register (typing .List [int ], lambda arg : "typing.GenericAlias" )
2734
+ with self .assertRaisesRegex (TypeError , "Invalid first argument to " ):
2735
+ f .register (list [int ] | str , lambda arg : "types.UnionTypes(types.GenericAlias)" )
2736
+ with self .assertRaisesRegex (TypeError , "Invalid first argument to " ):
2737
+ f .register (typing .List [float ] | bytes , lambda arg : "typing.Union[typing.GenericAlias]" )
2738
+ with self .assertRaisesRegex (TypeError , "Invalid first argument to " ):
2739
+ f .register (typing .Any , lambda arg : "typing.Any" )
2740
+
2741
+ self .assertEqual (f ([1 ]), "default" )
2742
+ self .assertEqual (f ([1.0 ]), "default" )
2743
+ self .assertEqual (f ("" ), "default" )
2744
+ self .assertEqual (f (b"" ), "default" )
2745
+
2746
+ def test_register_genericalias_decorator (self ):
2747
+ @functools .singledispatch
2748
+ def f (arg ):
2749
+ return "default"
2750
+
2751
+ with self .assertRaisesRegex (TypeError , "Invalid first argument to " ):
2752
+ f .register (list [int ])
2753
+ with self .assertRaisesRegex (TypeError , "Invalid first argument to " ):
2754
+ f .register (typing .List [int ])
2755
+ with self .assertRaisesRegex (TypeError , "Invalid first argument to " ):
2756
+ f .register (list [int ] | str )
2757
+ with self .assertRaisesRegex (TypeError , "Invalid first argument to " ):
2758
+ f .register (typing .List [int ] | str )
2759
+ with self .assertRaisesRegex (TypeError , "Invalid first argument to " ):
2760
+ f .register (typing .Any )
2761
+
2762
+ def test_register_genericalias_annotation (self ):
2763
+ @functools .singledispatch
2764
+ def f (arg ):
2765
+ return "default"
2766
+
2767
+ with self .assertRaisesRegex (TypeError , "Invalid annotation for 'arg'" ):
2768
+ @f .register
2769
+ def _ (arg : list [int ]):
2770
+ return "types.GenericAlias"
2771
+ with self .assertRaisesRegex (TypeError , "Invalid annotation for 'arg'" ):
2772
+ @f .register
2773
+ def _ (arg : typing .List [float ]):
2774
+ return "typing.GenericAlias"
2775
+ with self .assertRaisesRegex (TypeError , "Invalid annotation for 'arg'" ):
2776
+ @f .register
2777
+ def _ (arg : list [int ] | str ):
2778
+ return "types.UnionType(types.GenericAlias)"
2779
+ with self .assertRaisesRegex (TypeError , "Invalid annotation for 'arg'" ):
2780
+ @f .register
2781
+ def _ (arg : typing .List [float ] | bytes ):
2782
+ return "typing.Union[typing.GenericAlias]"
2783
+ with self .assertRaisesRegex (TypeError , "Invalid annotation for 'arg'" ):
2784
+ @f .register
2785
+ def _ (arg : typing .Any ):
2786
+ return "typing.Any"
2787
+
2788
+ self .assertEqual (f ([1 ]), "default" )
2789
+ self .assertEqual (f ([1.0 ]), "default" )
2790
+ self .assertEqual (f ("" ), "default" )
2791
+ self .assertEqual (f (b"" ), "default" )
2792
+
2725
2793
2726
2794
class CachedCostItem :
2727
2795
_cost = 1
0 commit comments