[issue39679] functools: singledispatchmethod doesn't work with classmethod

Karthikeyan Singaravelan report at bugs.python.org
Wed Feb 19 00:36:16 EST 2020


Karthikeyan Singaravelan <tir.karthi at gmail.com> added the comment:

I guess the method checks for annotation on cls [0] which will be classmethod/staticmethod object in the report and won't have annotations. The annotations should be looked up in the function the classmethod/staticmethod decorator wraps around as in cls.__func__ . Something like below so that the right annotations are picked up. In addition to this the registry should store the type annotation as key to cls or cls.__func__ depending on normal method or classmethod/staticmethod.

diff --git Lib/functools.py Lib/functools.py
index 050bec8605..a66711208d 100644
--- Lib/functools.py
+++ Lib/functools.py
@@ -1073,24 +1073,33 @@ def singledispatch(func):
         if func is None:
             if isinstance(cls, type):
                 return lambda f: register(cls, f)
-            ann = getattr(cls, '__annotations__', {})
+            if isinstance(cls, (classmethod, staticmethod)):
+                ann = getattr(cls.__func__, '__annotations__', {})
+                func = cls.__func__
+            else:
+                ann = getattr(cls, '__annotations__', {})
+                func = cls

[0] https://github.com/python/cpython/blob/ab6423fe2de0ed5f8a0dc86a9c7070229326b0f0/Lib/functools.py#L1076

----------
nosy: +levkivskyi, ncoghlan, xtreak

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue39679>
_______________________________________


More information about the Python-bugs-list mailing list