@@ -2895,47 +2895,6 @@ def add(self, x, y):
28952895 Abstract ()
28962896
28972897 def test_type_ann_register (self ):
2898- @functools .singledispatch
2899- def t (arg ):
2900- return "base"
2901- @t .register
2902- def _ (arg : int ):
2903- return "int"
2904- @t .register
2905- def _ (arg : str ):
2906- return "str"
2907- @t .register
2908- def _ (arg : float , / ):
2909- return "float"
2910- @t .register
2911- def _ (a1 : list , a2 : None , / , a3 : None , * , a4 : None ):
2912- return "list"
2913-
2914- def wrapped1 (arg : bytes ) -> str :
2915- return "bytes"
2916- @t .register
2917- @functools .wraps (wrapped1 )
2918- def wrapper1 (* args , ** kwargs ):
2919- return wrapped1 (* args , ** kwargs )
2920-
2921- def wrapped2 (arg : bytearray ) -> str :
2922- return "bytearray"
2923- @t .register
2924- @functools .wraps (wrapped2 )
2925- def wrapper2 (* args : typing .Any , ** kwargs : typing .Any ):
2926- return wrapped2 (* args , ** kwargs )
2927-
2928- # Check if the dispatch works.
2929- self .assertEqual (t (0 ), "int" )
2930- self .assertEqual (t ('' ), "str" )
2931- self .assertEqual (t (0.0 ), "float" )
2932- self .assertEqual (t ([], None , None , a4 = None ), "list" )
2933- self .assertEqual (t (NotImplemented ), "base" )
2934- self .assertEqual (t (b'' ), "bytes" )
2935- self .assertEqual (t (bytearray ()), "bytearray" )
2936-
2937- def test_method_type_ann_register (self ):
2938-
29392898 class A :
29402899 @functools .singledispatchmethod
29412900 def t (self , arg ):
@@ -2944,43 +2903,15 @@ def t(self, arg):
29442903 def _ (self , arg : int ):
29452904 return "int"
29462905 @t .register
2947- def _ (self , arg : complex , / ):
2948- return "complex"
2949- @t .register
2950- def _ (self , / , arg : str ):
2906+ def _ (self , arg : str ):
29512907 return "str"
2952- # See GH-130827.
2953- def wrapped1 (self : typing .Self , arg : bytes ):
2954- return "bytes"
2955- @t .register
2956- @functools .wraps (wrapped1 )
2957- def wrapper1 (self , * args , ** kwargs ):
2958- return self .wrapped1 (* args , ** kwargs )
2959-
2960- def wrapped2 (self , arg : bytearray ) -> str :
2961- return "bytearray"
2962- @t .register
2963- @functools .wraps (wrapped2 )
2964- def wrapper2 (self , * args : typing .Any , ** kwargs : typing .Any ):
2965- return self .wrapped2 (* args , ** kwargs )
2966-
29672908 a = A ()
29682909
29692910 self .assertEqual (a .t (0 ), "int" )
2970- self .assertEqual (a .t (0j ), "complex" )
29712911 self .assertEqual (a .t ('' ), "str" )
29722912 self .assertEqual (a .t (0.0 ), "base" )
2973- self .assertEqual (a .t (b'' ), "bytes" )
2974- self .assertEqual (a .t (bytearray ()), "bytearray" )
29752913
29762914 def test_staticmethod_type_ann_register (self ):
2977- def wrapper_decorator (func ):
2978- wrapped = func .__func__
2979- @staticmethod
2980- @functools .wraps (wrapped )
2981- def wrapper (* args , ** kwargs ):
2982- return wrapped (* args , ** kwargs )
2983- return wrapper
29842915 class A :
29852916 @functools .singledispatchmethod
29862917 @staticmethod
@@ -2992,49 +2923,15 @@ def _(arg: int):
29922923 return isinstance (arg , int )
29932924 @t .register
29942925 @staticmethod
2995- def _ (arg : str , / ):
2926+ def _ (arg : str ):
29962927 return isinstance (arg , str )
2997- @t .register
2998- @wrapper_decorator
2999- @staticmethod
3000- def _ (arg : bytes ) -> bool :
3001- return isinstance (arg , bytes )
3002- @wrapper_decorator
3003- @staticmethod
3004- def outer1 (arg : complex ):
3005- return isinstance (arg , complex )
3006- @wrapper_decorator
3007- @staticmethod
3008- def outer2 (arg : bool ):
3009- return isinstance (arg , bool )
3010- @wrapper_decorator
3011- @staticmethod
3012- def outer3 (arg : bytearray ):
3013- return isinstance (arg , bytearray )
3014-
3015- A .t .register (staticmethod (A .outer1 ))
3016- A .t .register (staticmethod (A .__dict__ ['outer2' ]))
30172928 a = A ()
3018- a .t .register (staticmethod (a .outer3 ))
30192929
30202930 self .assertTrue (A .t (0 ))
30212931 self .assertTrue (A .t ('' ))
30222932 self .assertEqual (A .t (0.0 ), 0.0 )
3023- self .assertTrue (A .t (0j ))
3024- self .assertTrue (a .t (42j ))
3025- self .assertTrue (A .t (True ))
3026- self .assertTrue (a .t (False ))
3027- self .assertTrue (A .t (bytearray ([1 ])))
3028- self .assertTrue (a .t (bytearray ()))
30292933
30302934 def test_classmethod_type_ann_register (self ):
3031- def wrapper_decorator (func ):
3032- wrapped = func .__func__
3033- @classmethod
3034- @functools .wraps (wrapped )
3035- def wrapper (* args , ** kwargs ):
3036- return wrapped (* args , ** kwargs )
3037- return wrapper
30382935 class A :
30392936 def __init__ (self , arg ):
30402937 self .arg = arg
@@ -3051,55 +2948,10 @@ def _(cls, arg: int):
30512948 @classmethod
30522949 def _ (cls , arg : str ):
30532950 return cls ("str" )
3054- @t .register
3055- @wrapper_decorator
3056- @classmethod
3057- def _ (cls , arg : bytes ):
3058- return cls ("bytes" )
3059- @wrapper_decorator
3060- @classmethod
3061- def outer1 (cls , arg : list ):
3062- return cls ("list" )
3063- @wrapper_decorator
3064- @classmethod
3065- def outer2 (cls , arg : complex ):
3066- return cls ("complex" )
3067- @wrapper_decorator
3068- @classmethod
3069- def outer3 (cls , arg : bytearray ):
3070- return cls ("bytearray" )
3071-
3072- A .t .register (A .outer1 )
3073- A .t .register (A .__dict__ ['outer2' ])
3074- a = A (None )
3075- a .t .register (a .outer3 )
30762951
30772952 self .assertEqual (A .t (0 ).arg , "int" )
3078- self .assertEqual (a .t ('' ).arg , "str" )
2953+ self .assertEqual (A .t ('' ).arg , "str" )
30792954 self .assertEqual (A .t (0.0 ).arg , "base" )
3080- self .assertEqual (a .t (b'' ).arg , "bytes" )
3081- self .assertEqual (A .t ([]).arg , "list" )
3082- self .assertEqual (a .t (0j ).arg , "complex" )
3083- self .assertEqual (A .t (bytearray ()).arg , "bytearray" )
3084-
3085- def test_boundmethod_type_ann_register (self ):
3086- class C :
3087- @functools .singledispatchmethod
3088- def sdm (self , x : object ) -> str :
3089- return "C.sdm"
3090-
3091- def method (self , x : int ) -> str :
3092- return "C.method"
3093-
3094- sd = functools .singledispatch (lambda x : "sd" )
3095-
3096- sd .register (C ().method )
3097- self .assertEqual (sd (0j ), "sd" )
3098- self .assertEqual (sd (1 ), "C.method" )
3099-
3100- C .sdm .register (C ().method )
3101- self .assertEqual (C ().sdm (0j ), "C.sdm" )
3102- self .assertEqual (C ().sdm (1 ), "C.method" )
31032955
31042956 def test_method_wrapping_attributes (self ):
31052957 class A :
@@ -3318,27 +3170,12 @@ def test_invalid_registrations(self):
33183170 @functools .singledispatch
33193171 def i (arg ):
33203172 return "base"
3321- with self .assertRaises (TypeError ) as exc :
3322- @i .register
3323- def _ () -> None :
3324- return "My function doesn't take arguments"
3325- self .assertStartsWith (str (exc .exception ), msg_prefix )
3326- self .assertEndsWith (str (exc .exception ), "does not accept positional arguments." )
3327-
3328- with self .assertRaises (TypeError ) as exc :
3329- @i .register
3330- def _ (* , foo : str ) -> None :
3331- return "My function takes keyword-only arguments"
3332- self .assertStartsWith (str (exc .exception ), msg_prefix )
3333- self .assertEndsWith (str (exc .exception ), "does not accept positional arguments." )
3334-
33353173 with self .assertRaises (TypeError ) as exc :
33363174 @i .register (42 )
33373175 def _ (arg ):
33383176 return "I annotated with a non-type"
33393177 self .assertStartsWith (str (exc .exception ), msg_prefix + "42" )
33403178 self .assertEndsWith (str (exc .exception ), msg_suffix )
3341-
33423179 with self .assertRaises (TypeError ) as exc :
33433180 @i .register
33443181 def _ (arg ):
@@ -3348,33 +3185,6 @@ def _(arg):
33483185 )
33493186 self .assertEndsWith (str (exc .exception ), msg_suffix )
33503187
3351- with self .assertRaises (TypeError ) as exc :
3352- @i .register
3353- def _ (arg , extra : int ):
3354- return "I did not annotate the right param"
3355- self .assertStartsWith (str (exc .exception ), msg_prefix +
3356- "<function TestSingleDispatch.test_invalid_registrations.<locals>._"
3357- )
3358- self .assertEndsWith (str (exc .exception ),
3359- "Use either `@register(some_class)` or add a type annotation "
3360- f"to parameter 'arg' of your callable." )
3361-
3362- with self .assertRaises (TypeError ) as exc :
3363- # See GH-84644.
3364-
3365- @functools .singledispatch
3366- def func (arg ):...
3367-
3368- @func .register
3369- def _int (arg ) -> int :...
3370-
3371- self .assertStartsWith (str (exc .exception ), msg_prefix +
3372- "<function TestSingleDispatch.test_invalid_registrations.<locals>._int"
3373- )
3374- self .assertEndsWith (str (exc .exception ),
3375- "Use either `@register(some_class)` or add a type annotation "
3376- f"to parameter 'arg' of your callable." )
3377-
33783188 with self .assertRaises (TypeError ) as exc :
33793189 @i .register
33803190 def _ (arg : typing .Iterable [str ]):
@@ -3638,44 +3448,44 @@ def _(item: int, arg: bytes) -> str:
36383448
36393449 def test_method_signatures (self ):
36403450 class A :
3641- def m (self , item : int , arg ) -> str :
3451+ def m (self , item , arg : int ) -> str :
36423452 return str (item )
36433453 @classmethod
3644- def cm (cls , item : int , arg ) -> str :
3454+ def cm (cls , item , arg : int ) -> str :
36453455 return str (item )
36463456 @functools .singledispatchmethod
3647- def func (self , item : int , arg ) -> str :
3457+ def func (self , item , arg : int ) -> str :
36483458 return str (item )
36493459 @func .register
3650- def _ (self , item : bytes , arg ) -> str :
3460+ def _ (self , item , arg : bytes ) -> str :
36513461 return str (item )
36523462
36533463 @functools .singledispatchmethod
36543464 @classmethod
3655- def cls_func (cls , item : int , arg ) -> str :
3465+ def cls_func (cls , item , arg : int ) -> str :
36563466 return str (arg )
36573467 @func .register
36583468 @classmethod
3659- def _ (cls , item : bytes , arg ) -> str :
3469+ def _ (cls , item , arg : bytes ) -> str :
36603470 return str (item )
36613471
36623472 @functools .singledispatchmethod
36633473 @staticmethod
3664- def static_func (item : int , arg ) -> str :
3474+ def static_func (item , arg : int ) -> str :
36653475 return str (arg )
36663476 @func .register
36673477 @staticmethod
3668- def _ (item : bytes , arg ) -> str :
3478+ def _ (item , arg : bytes ) -> str :
36693479 return str (item )
36703480
36713481 self .assertEqual (str (Signature .from_callable (A .func )),
3672- '(self, item: int , arg) -> str' )
3482+ '(self, item, arg: int ) -> str' )
36733483 self .assertEqual (str (Signature .from_callable (A ().func )),
3674- '(self, item: int , arg) -> str' )
3484+ '(self, item, arg: int ) -> str' )
36753485 self .assertEqual (str (Signature .from_callable (A .cls_func )),
3676- '(cls, item: int , arg) -> str' )
3486+ '(cls, item, arg: int ) -> str' )
36773487 self .assertEqual (str (Signature .from_callable (A .static_func )),
3678- '(item: int , arg) -> str' )
3488+ '(item, arg: int ) -> str' )
36793489
36803490 def test_method_non_descriptor (self ):
36813491 class Callable :
0 commit comments