Skip to content

Commit ba46e43

Browse files
committed
Add previously failing tests only
1 parent ac2f5a2 commit ba46e43

File tree

1 file changed

+76
-13
lines changed

1 file changed

+76
-13
lines changed

Lib/test/test_functools.py

Lines changed: 76 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2903,13 +2903,34 @@ def t(self, arg):
29032903
def _(self, arg: int):
29042904
return "int"
29052905
@t.register
2906-
def _(self, arg: str):
2906+
def _(self, arg: complex, /):
2907+
return "complex"
2908+
@t.register
2909+
def _(self, /, arg: str):
29072910
return "str"
2911+
# See GH-130827.
2912+
def wrapped1(self: typing.Self, arg: bytes):
2913+
return "bytes"
2914+
@t.register
2915+
@functools.wraps(wrapped1)
2916+
def wrapper1(self, *args, **kwargs):
2917+
return self.wrapped1(*args, **kwargs)
2918+
2919+
def wrapped2(self, arg: bytearray) -> str:
2920+
return "bytearray"
2921+
@t.register
2922+
@functools.wraps(wrapped2)
2923+
def wrapper2(self, *args: typing.Any, **kwargs: typing.Any):
2924+
return self.wrapped2(*args, **kwargs)
2925+
29082926
a = A()
29092927

29102928
self.assertEqual(a.t(0), "int")
2929+
self.assertEqual(a.t(0j), "complex")
29112930
self.assertEqual(a.t(''), "str")
29122931
self.assertEqual(a.t(0.0), "base")
2932+
self.assertEqual(a.t(b''), "bytes")
2933+
self.assertEqual(a.t(bytearray()), "bytearray")
29132934

29142935
def test_staticmethod_type_ann_register(self):
29152936
class A:
@@ -3170,12 +3191,27 @@ def test_invalid_registrations(self):
31703191
@functools.singledispatch
31713192
def i(arg):
31723193
return "base"
3194+
with self.assertRaises(TypeError) as exc:
3195+
@i.register
3196+
def _() -> None:
3197+
return "My function doesn't take arguments"
3198+
self.assertStartsWith(str(exc.exception), msg_prefix)
3199+
self.assertEndsWith(str(exc.exception), "does not accept positional arguments.")
3200+
3201+
with self.assertRaises(TypeError) as exc:
3202+
@i.register
3203+
def _(*, foo: str) -> None:
3204+
return "My function takes keyword-only arguments"
3205+
self.assertStartsWith(str(exc.exception), msg_prefix)
3206+
self.assertEndsWith(str(exc.exception), "does not accept positional arguments.")
3207+
31733208
with self.assertRaises(TypeError) as exc:
31743209
@i.register(42)
31753210
def _(arg):
31763211
return "I annotated with a non-type"
31773212
self.assertStartsWith(str(exc.exception), msg_prefix + "42")
31783213
self.assertEndsWith(str(exc.exception), msg_suffix)
3214+
31793215
with self.assertRaises(TypeError) as exc:
31803216
@i.register
31813217
def _(arg):
@@ -3185,6 +3221,33 @@ def _(arg):
31853221
)
31863222
self.assertEndsWith(str(exc.exception), msg_suffix)
31873223

3224+
with self.assertRaises(TypeError) as exc:
3225+
@i.register
3226+
def _(arg, extra: int):
3227+
return "I did not annotate the right param"
3228+
self.assertStartsWith(str(exc.exception), msg_prefix +
3229+
"<function TestSingleDispatch.test_invalid_registrations.<locals>._"
3230+
)
3231+
self.assertEndsWith(str(exc.exception),
3232+
"Use either `@register(some_class)` or add a type annotation "
3233+
f"to parameter 'arg' of your callable.")
3234+
3235+
with self.assertRaises(TypeError) as exc:
3236+
# See GH-84644.
3237+
3238+
@functools.singledispatch
3239+
def func(arg):...
3240+
3241+
@func.register
3242+
def _int(arg) -> int:...
3243+
3244+
self.assertStartsWith(str(exc.exception), msg_prefix +
3245+
"<function TestSingleDispatch.test_invalid_registrations.<locals>._int"
3246+
)
3247+
self.assertEndsWith(str(exc.exception),
3248+
"Use either `@register(some_class)` or add a type annotation "
3249+
f"to parameter 'arg' of your callable.")
3250+
31883251
with self.assertRaises(TypeError) as exc:
31893252
@i.register
31903253
def _(arg: typing.Iterable[str]):
@@ -3448,44 +3511,44 @@ def _(item: int, arg: bytes) -> str:
34483511

34493512
def test_method_signatures(self):
34503513
class A:
3451-
def m(self, item, arg: int) -> str:
3514+
def m(self, item: int, arg) -> str:
34523515
return str(item)
34533516
@classmethod
3454-
def cm(cls, item, arg: int) -> str:
3517+
def cm(cls, item: int, arg) -> str:
34553518
return str(item)
34563519
@functools.singledispatchmethod
3457-
def func(self, item, arg: int) -> str:
3520+
def func(self, item: int, arg) -> str:
34583521
return str(item)
34593522
@func.register
3460-
def _(self, item, arg: bytes) -> str:
3523+
def _(self, item: bytes, arg) -> str:
34613524
return str(item)
34623525

34633526
@functools.singledispatchmethod
34643527
@classmethod
3465-
def cls_func(cls, item, arg: int) -> str:
3528+
def cls_func(cls, item: int, arg) -> str:
34663529
return str(arg)
34673530
@func.register
34683531
@classmethod
3469-
def _(cls, item, arg: bytes) -> str:
3532+
def _(cls, item: bytes, arg) -> str:
34703533
return str(item)
34713534

34723535
@functools.singledispatchmethod
34733536
@staticmethod
3474-
def static_func(item, arg: int) -> str:
3537+
def static_func(item: int, arg) -> str:
34753538
return str(arg)
34763539
@func.register
34773540
@staticmethod
3478-
def _(item, arg: bytes) -> str:
3541+
def _(item: bytes, arg) -> str:
34793542
return str(item)
34803543

34813544
self.assertEqual(str(Signature.from_callable(A.func)),
3482-
'(self, item, arg: int) -> str')
3545+
'(self, item: int, arg) -> str')
34833546
self.assertEqual(str(Signature.from_callable(A().func)),
3484-
'(self, item, arg: int) -> str')
3547+
'(self, item: int, arg) -> str')
34853548
self.assertEqual(str(Signature.from_callable(A.cls_func)),
3486-
'(cls, item, arg: int) -> str')
3549+
'(cls, item: int, arg) -> str')
34873550
self.assertEqual(str(Signature.from_callable(A.static_func)),
3488-
'(item, arg: int) -> str')
3551+
'(item: int, arg) -> str')
34893552

34903553
def test_method_non_descriptor(self):
34913554
class Callable:

0 commit comments

Comments
 (0)