Skip to content

Commit cdb7cca

Browse files
committed
Always respect descriptors, fallback to assumptions on function-like objects
1 parent 17b5088 commit cdb7cca

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

Lib/functools.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -902,6 +902,7 @@ def _get_dispatch_param(func, *, _insideclass=False):
902902
func = func.__func__
903903
elif isinstance(func, classmethod):
904904
func = func.__func__
905+
idx = 1
905906
if isinstance(func, FunctionType) and not hasattr(func, "__wrapped__"):
906907
func_code = func.__code__
907908
try:

Lib/test/test_functools.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3008,10 +3008,15 @@ def outer1(arg: complex):
30083008
@staticmethod
30093009
def outer2(arg: bool):
30103010
return isinstance(arg, bool)
3011+
@wrapper_decorator
3012+
@staticmethod
3013+
def outer3(arg: bytearray):
3014+
return isinstance(arg, bytearray)
30113015

30123016
A.t.register(staticmethod(A.outer1))
3017+
A.t.register(staticmethod(A.__dict__['outer2']))
30133018
a = A()
3014-
a.t.register(staticmethod(a.outer2))
3019+
a.t.register(staticmethod(a.outer3))
30153020

30163021
self.assertTrue(A.t(0))
30173022
self.assertTrue(A.t(''))
@@ -3020,6 +3025,8 @@ def outer2(arg: bool):
30203025
self.assertTrue(a.t(42j))
30213026
self.assertTrue(A.t(True))
30223027
self.assertTrue(a.t(False))
3028+
self.assertTrue(A.t(bytearray([1])))
3029+
self.assertTrue(a.t(bytearray()))
30233030

30243031
def test_classmethod_type_ann_register(self):
30253032
def wrapper_decorator(func):
@@ -3058,17 +3065,23 @@ def outer1(cls, arg: list):
30583065
@classmethod
30593066
def outer2(cls, arg: complex):
30603067
return cls("complex")
3068+
@wrapper_decorator
3069+
@classmethod
3070+
def outer3(cls, arg: bytearray):
3071+
return cls("bytearray")
30613072

30623073
A.t.register(A.outer1)
3074+
A.t.register(A.__dict__['outer2'])
30633075
a = A(None)
3064-
a.t.register(a.outer2)
3076+
a.t.register(a.outer3)
30653077

30663078
self.assertEqual(A.t(0).arg, "int")
30673079
self.assertEqual(a.t('').arg, "str")
30683080
self.assertEqual(A.t(0.0).arg, "base")
30693081
self.assertEqual(a.t(b'').arg, "bytes")
30703082
self.assertEqual(A.t([]).arg, "list")
30713083
self.assertEqual(a.t(0j).arg, "complex")
3084+
self.assertEqual(A.t(bytearray()).arg, "bytearray")
30723085

30733086
def test_method_wrapping_attributes(self):
30743087
class A:

0 commit comments

Comments
 (0)