[issue43917] An error in classmethod example in the documentation of descriptor

New submission from Egor <pixel.egor@gmail.com>: In this section https://docs.python.org/3/howto/descriptor.html#class-methods in the example of python implementation of ClassMethod inside __get__ method I think that we should check hasattr(self.f, "__get__") instead of hasattr(obj, "__get__"). ---------- assignee: docs@python components: Documentation messages: 391638 nosy: docs@python, rhettinger, titanolodon priority: normal severity: normal status: open title: An error in classmethod example in the documentation of descriptor versions: Python 3.9 _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue43917> _______________________________________

Change by Raymond Hettinger <raymond.hettinger@gmail.com>: ---------- assignee: docs@python -> rhettinger _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue43917> _______________________________________

Change by Roundup Robot <devnull@psf.upfronthosting.co.za>: ---------- keywords: +patch nosy: +python-dev nosy_count: 3.0 -> 4.0 pull_requests: +24262 stage: -> patch review pull_request: https://github.com/python/cpython/pull/25541 _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue43917> _______________________________________

Raymond Hettinger <raymond.hettinger@gmail.com> added the comment: It looks like it should be: if hasattr(type(self.f), "__get__"): Here's the relevant C code: static PyObject * cm_descr_get(PyObject *self, PyObject *obj, PyObject *type) { classmethod *cm = (classmethod *)self; if (cm->cm_callable == NULL) { PyErr_SetString(PyExc_RuntimeError, "uninitialized classmethod object"); return NULL; } if (type == NULL) 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); } return PyMethod_New(cm->cm_callable, type); } ---------- _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue43917> _______________________________________

Change by Raymond Hettinger <raymond.hettinger@gmail.com>: ---------- pull_requests: +24268 pull_request: https://github.com/python/cpython/pull/25544 _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue43917> _______________________________________

Raymond Hettinger <raymond.hettinger@gmail.com> added the comment: New changeset 14092b5a4ae4caf1c77f685450016a0d1ad0bd6c by Raymond Hettinger in branch 'master': bpo-43917: Fix pure python equivalent for classmethod (GH-25544) https://github.com/python/cpython/commit/14092b5a4ae4caf1c77f685450016a0d1ad... ---------- _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue43917> _______________________________________

Change by miss-islington <mariatta.wijaya+miss-islington@gmail.com>: ---------- nosy: +miss-islington nosy_count: 4.0 -> 5.0 pull_requests: +24270 pull_request: https://github.com/python/cpython/pull/25546 _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue43917> _______________________________________

Raymond Hettinger <raymond.hettinger@gmail.com> added the comment: New changeset 34be48450f03b121be10a9f8e8989603478f0469 by Miss Islington (bot) in branch '3.9': bpo-43917: Fix pure python equivalent for classmethod (GH-25544) (GH-25546) https://github.com/python/cpython/commit/34be48450f03b121be10a9f8e8989603478... ---------- _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue43917> _______________________________________

Raymond Hettinger <raymond.hettinger@gmail.com> added the comment: Okay, it's fixed. Thanks for the report. ---------- resolution: -> fixed stage: patch review -> resolved status: open -> closed type: -> behavior versions: +Python 3.10 _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue43917> _______________________________________
participants (4)
-
Egor
-
miss-islington
-
Raymond Hettinger
-
Roundup Robot