This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author eriknw
Recipients berker.peksag, eriknw, rhettinger, serhiy.storchaka
Date 2020-10-18.20:22:43
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
The following is new to Python 3.9, and I consider the implementation incomplete.  I have code that works for Python 3.8 and before, but not for Python 3.9:

"Class methods can now wrap other :term:`descriptors <descriptor>` such as :func:`property`."

As implemented, `classmethod` does not correctly wrap descriptors that mimic classmethod.  Previously, __get__of wrapped objects wasn't invoked by classmethod, so it was safe to have an object with both __call__ and __get__ behave like a classmethod.  Now, classmethod calling __get__ first gives incorrect results.

Here is a minimal example:
from types import MethodType

class myclassmethod:
    def __init__(self, func):
        self.func = func

    def __call__(self, cls):
        return self.func(cls)

    def __get__(self, instance, owner=None):
        if owner is None:
            owner = type(instance)
        return MethodType(self, owner)

class A:
    def f1(cls):
        return cls

    def f2(cls):
        return cls

assert A.f1() is A
assert A.f2() is A  # <-- fails in 3.9, works in 3.8 and before
This pattern would typically be used to do something extra in __call__.

For the sake of discussion, let's call the two arguments to __get__ "instance" and "owner".  Typically, "instance" is an instance of "owner", or, equivalently, "owner" is the type of "instance".  If "owner" is None, it is generally assumed to be the type of "instance".

In bpo19072 (and gh8405), classmethod was changed to call `obj.__get__(owner)` if the wrapped object "obj" has __get__.  Notice that only the "instance" argument is provided.  Moreover, the type `owner` is passed as the "instance" argument.  This means that the "owner" argument (which is None) will be assumed to be the type of the "instance" argument, which is the type of the `owner` type.  This is wrong.  The "owner" argument should be `owner`.

I believe it would be better for classmethod to call `obj.__get__(owner, owner)` if "obj" has __get__.

This is kind of difficult to explain.  I will make a PR with more informative tests shortly.  Here is the simple diff to make the above example pass:
diff --git a/Objects/funcobject.c b/Objects/funcobject.c
index bd24f67b97..74f9167566 100644
--- a/Objects/funcobject.c
+++ b/Objects/funcobject.c
@@ -739,7 +739,7 @@ cm_descr_get(PyObject *self, PyObject *obj, PyObject *type)
         type = (PyObject *)(Py_TYPE(obj));
     if (Py_TYPE(cm->cm_callable)->tp_descr_get != NULL) {
         return Py_TYPE(cm->cm_callable)->tp_descr_get(cm->cm_callable, type,
-                                                      NULL);
+                                                      type);
     return PyMethod_New(cm->cm_callable, type);

Since I consider the new behavior to have introduced a regression, I think this change should be applied to both 3.9 and 3.10.

Date User Action Args
2020-10-18 20:22:44eriknwsetrecipients: + eriknw, rhettinger, berker.peksag, serhiy.storchaka
2020-10-18 20:22:44eriknwsetmessageid: <>
2020-10-18 20:22:44eriknwlinkissue42073 messages
2020-10-18 20:22:43eriknwcreate