MCPcopy Index your code
hub / github.com/python/cpython / test_method_classlevel_calls

Method test_method_classlevel_calls

Lib/test/test_functools.py:3072–3121  ·  view source on GitHub ↗

Regression test for GH-143535.

(self)

Source from the content-addressed store, hash-verified

3070 self.assertEqual(A().static_func.__name__, 'static_func')
3071
3072 def test_method_classlevel_calls(self):
3073 """Regression test for GH-143535."""
3074 class C:
3075 @functools.singledispatchmethod
3076 def generic(self, x: object):
3077 return "generic"
3078
3079 @generic.register
3080 def special1(self, x: int):
3081 return "special1"
3082
3083 @generic.register
3084 @classmethod
3085 def special2(self, x: float):
3086 return "special2"
3087
3088 @generic.register
3089 @staticmethod
3090 def special3(x: complex):
3091 return "special3"
3092
3093 def special4(self, x):
3094 return "special4"
3095
3096 class D1:
3097 def __get__(self, _, owner):
3098 return lambda inst, x: owner.special4(inst, x)
3099
3100 generic.register(D1, D1())
3101
3102 def special5(self, x):
3103 return "special5"
3104
3105 class D2:
3106 def __get__(self, inst, owner):
3107 # Different instance bound to the returned method
3108 # doesn't cause it to receive the original instance
3109 # as a separate argument.
3110 # To work around this, wrap the returned bound method
3111 # with `functools.partial`.
3112 return C().special5
3113
3114 generic.register(D2, D2())
3115
3116 self.assertEqual(C.generic(C(), "foo"), "generic")
3117 self.assertEqual(C.generic(C(), 1), "special1")
3118 self.assertEqual(C.generic(C(), 2.0), "special2")
3119 self.assertEqual(C.generic(C(), 3j), "special3")
3120 self.assertEqual(C.generic(C(), C.D1()), "special4")
3121 self.assertEqual(C.generic(C(), C.D2()), "special5")
3122
3123 def test_method_repr(self):
3124 class Callable:

Callers

nothing calls this directly

Calls 3

CClass · 0.70
assertEqualMethod · 0.45
genericMethod · 0.45

Tested by

no test coverage detected