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

This commit is contained in:
Miss Skeleton (bot) 2020-10-23 19:01:35 -07:00 committed by GitHub
parent dc33f79813
commit c17f63fae5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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}'