From a365780b96ae5c3e7ce47c41fe648faa14ce0de1 Mon Sep 17 00:00:00 2001 From: Florian Dahlitz Date: Sat, 23 Nov 2019 14:50:49 +0100 Subject: [PATCH 1/3] bpo-38524: add note about the implicit and explicit calling of __set_name__ --- Doc/reference/datamodel.rst | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Doc/reference/datamodel.rst b/Doc/reference/datamodel.rst index 76b7035ced9794..bb68cf005beddf 100644 --- a/Doc/reference/datamodel.rst +++ b/Doc/reference/datamodel.rst @@ -2002,6 +2002,16 @@ invoked after creating the class object: * finally, the :meth:`~object.__init_subclass__` hook is called on the immediate parent of the new class in its method resolution order. +.. note:: + + ``__set_name__`` is only called implicitly as part of the ``type`` constructor, so + it will need to be called explicitly with the appropriate parameters when a + descriptor is added to a class after initial creation:: + + descr = custom_descriptor() + cls.attr = descr + descr.__set_name__(cls, 'attr') + After the class object is created, it is passed to the class decorators included in the class definition (if any) and the resulting object is bound in the local namespace as the defined class. From 205f2745509ef107906a2ee7480a85d07845dea0 Mon Sep 17 00:00:00 2001 From: Florian Dahlitz Date: Sun, 24 Nov 2019 12:42:59 +0100 Subject: [PATCH 2/3] bpo-38524: move note about __set_name__ call to descriptor section --- Doc/reference/datamodel.rst | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/Doc/reference/datamodel.rst b/Doc/reference/datamodel.rst index bb68cf005beddf..f7b3b97ca5b55d 100644 --- a/Doc/reference/datamodel.rst +++ b/Doc/reference/datamodel.rst @@ -1657,6 +1657,18 @@ class' :attr:`~object.__dict__`. .. versionadded:: 3.6 + .. note:: + + ``__set_name__`` is only called implicitly as part of the ``type`` constructor, so + it will need to be called explicitly with the appropriate parameters when a + descriptor is added to a class after initial creation:: + + descr = custom_descriptor() + cls.attr = descr + descr.__set_name__(cls, 'attr') + + See :ref:`class-object-creation` for more details. + The attribute :attr:`__objclass__` is interpreted by the :mod:`inspect` module as specifying the class where this object was defined (setting this @@ -2002,16 +2014,6 @@ invoked after creating the class object: * finally, the :meth:`~object.__init_subclass__` hook is called on the immediate parent of the new class in its method resolution order. -.. note:: - - ``__set_name__`` is only called implicitly as part of the ``type`` constructor, so - it will need to be called explicitly with the appropriate parameters when a - descriptor is added to a class after initial creation:: - - descr = custom_descriptor() - cls.attr = descr - descr.__set_name__(cls, 'attr') - After the class object is created, it is passed to the class decorators included in the class definition (if any) and the resulting object is bound in the local namespace as the defined class. From 018e52dd092a1754296669f5e4d0a66cf8511fe7 Mon Sep 17 00:00:00 2001 From: Florian Dahlitz Date: Mon, 25 Nov 2019 17:23:53 +0100 Subject: [PATCH 3/3] bpo-38524: move versionadded after note for consistency --- Doc/reference/datamodel.rst | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Doc/reference/datamodel.rst b/Doc/reference/datamodel.rst index f7b3b97ca5b55d..68098508afbaa2 100644 --- a/Doc/reference/datamodel.rst +++ b/Doc/reference/datamodel.rst @@ -1655,8 +1655,6 @@ class' :attr:`~object.__dict__`. Called at the time the owning class *owner* is created. The descriptor has been assigned to *name*. - .. versionadded:: 3.6 - .. note:: ``__set_name__`` is only called implicitly as part of the ``type`` constructor, so @@ -1669,6 +1667,7 @@ class' :attr:`~object.__dict__`. See :ref:`class-object-creation` for more details. + .. versionadded:: 3.6 The attribute :attr:`__objclass__` is interpreted by the :mod:`inspect` module as specifying the class where this object was defined (setting this