[Python-checkins] bpo-19072: Update descriptor howto for decorator chaining (GH-22934)

rhettinger webhook-mailer at python.org
Fri Oct 23 21:37:33 EDT 2020


https://github.com/python/cpython/commit/8e5b0fdce337ef0a1f4f38b31a8c6b66c56b16d2
commit: 8e5b0fdce337ef0a1f4f38b31a8c6b66c56b16d2
branch: master
author: Raymond Hettinger <rhettinger at users.noreply.github.com>
committer: rhettinger <rhettinger at users.noreply.github.com>
date: 2020-10-23T18:37:27-07:00
summary:

bpo-19072: Update descriptor howto for decorator chaining (GH-22934)

files:
M Doc/howto/descriptor.rst

diff --git a/Doc/howto/descriptor.rst b/Doc/howto/descriptor.rst
index 4a53b9e615692..4e9fad30d31c4 100644
--- a/Doc/howto/descriptor.rst
+++ b/Doc/howto/descriptor.rst
@@ -872,6 +872,16 @@ Using the non-data descriptor protocol, a pure Python version of
         def __get__(self, obj, cls=None):
             if cls is None:
                 cls = type(obj)
-            def newfunc(*args):
-                return self.f(cls, *args)
-            return newfunc
+            if hasattr(obj, '__get__'):
+                return self.f.__get__(cls)
+            return types.MethodType(self.f, cls)
+
+The code path for ``hasattr(obj, '__get__')`` was added in Python 3.9 and
+makes it possible for :func:`classmethod` to support chained decorators.
+For example, a classmethod and property could be chained together::
+
+    class G:
+        @classmethod
+        @property
+        def __doc__(cls):
+            return f'A doc for {cls.__name__!r}'



More information about the Python-checkins mailing list