Skip to content

Commit 09f235e

Browse files
miss-islingtonzuopicnixz
authored
[3.13] gh-122102: Fix/improve docs of descriptor-related tools in inspect (GH-153950)
(cherry picked from commit 87f8fc8) Co-authored-by: Jan Kaliszewski <zuo@kaliszewski.net> Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
1 parent bf5f85d commit 09f235e

2 files changed

Lines changed: 40 additions & 24 deletions

File tree

Doc/library/inspect.rst

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -538,19 +538,18 @@ attributes (see :ref:`import-mod-attrs` for module attributes):
538538
.. function:: ismethoddescriptor(object)
539539

540540
Return ``True`` if the object is a method descriptor, but not if
541-
:func:`ismethod`, :func:`isclass`, :func:`isfunction` or :func:`isbuiltin`
542-
are true.
541+
:func:`isclass`, :func:`ismethod` or :func:`isfunction` is true.
543542

544543
This, for example, is true of ``int.__add__``. An object passing this test
545544
has a :meth:`~object.__get__` method, but not a :meth:`~object.__set__`
546545
method or a :meth:`~object.__delete__` method. Beyond that, the set of
547546
attributes varies. A :attr:`~definition.__name__` attribute is usually
548547
sensible, and :attr:`~definition.__doc__` often is.
549548

550-
Methods implemented via descriptors that also pass one of the other tests
551-
return ``False`` from the :func:`ismethoddescriptor` test, simply because the
552-
other tests promise more -- you can, e.g., count on having the
553-
:attr:`~method.__func__` attribute (etc) when an object passes
549+
Method descriptors that also pass any of the other tests (:func:`!isclass`,
550+
:func:`!ismethod` or :func:`!isfunction`) make this function return ``False``,
551+
simply because those other tests promise more -- you can, for example, count
552+
on having the :attr:`~method.__func__` attribute when an object passes
554553
:func:`ismethod`.
555554

556555
.. versionchanged:: 3.13
@@ -561,16 +560,28 @@ attributes (see :ref:`import-mod-attrs` for module attributes):
561560

562561
.. function:: isdatadescriptor(object)
563562

564-
Return ``True`` if the object is a data descriptor.
563+
Return ``True`` if the object is a data descriptor, but not if
564+
:func:`isclass`, :func:`ismethod` or :func:`isfunction` is true.
565565

566-
Data descriptors have a :attr:`~object.__set__` or a :attr:`~object.__delete__` method.
567-
Examples are properties (defined in Python), getsets, and members. The
568-
latter two are defined in C and there are more specific tests available for
569-
those types, which is robust across Python implementations. Typically, data
570-
descriptors will also have :attr:`~definition.__name__` and :attr:`!__doc__` attributes
571-
(properties, getsets, and members have both of these attributes), but this is
572-
not guaranteed.
566+
Data descriptors always have a :meth:`~object.__set__` method and/or
567+
a :meth:`~object.__delete__` method. Optionally, they may also have a
568+
:meth:`~object.__get__` method.
573569

570+
Examples of data descriptors are :func:`properties <property>`, getsets and
571+
member descriptors. Note that for the latter two (defined only in C extension
572+
modules), more specific tests are available: :func:`isgetsetdescriptor` and
573+
:func:`ismemberdescriptor`, respectively.
574+
575+
While data descriptors may also have :attr:`~definition.__name__` and
576+
:attr:`!__doc__` attributes (as properties, getsets and member descriptors
577+
do), this is not necessarily the case in general.
578+
579+
.. versionchanged:: 3.8
580+
This function now reports objects with only a :meth:`~object.__set__` method
581+
as being data descriptors (the presence of :meth:`~object.__get__` is no
582+
longer required for that). Moreover, objects with :meth:`~object.__delete__`,
583+
but not :meth:`~object.__set__`, are now properly recognized as data
584+
descriptors as well, which was not the case previously.
574585

575586
.. function:: isgetsetdescriptor(object)
576587

Lib/inspect.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -312,18 +312,18 @@ def ismethod(object):
312312
def ismethoddescriptor(object):
313313
"""Return true if the object is a method descriptor.
314314
315-
But not if ismethod() or isclass() or isfunction() are true.
315+
But not if ismethod(), isclass() or isfunction() is true.
316316
317-
This is new in Python 2.2, and, for example, is true of int.__add__.
318-
An object passing this test has a __get__ attribute, but not a
319-
__set__ attribute or a __delete__ attribute. Beyond that, the set
320-
of attributes varies; __name__ is usually sensible, and __doc__
321-
often is.
317+
An object passing this test (for example, int.__add__) has a __get__
318+
attribute, but not a __set__ attribute or a __delete__ attribute.
319+
Beyond that, the set of attributes varies; __name__ is usually
320+
sensible, and __doc__ often is.
322321
323322
Methods implemented via descriptors that also pass one of the other
324-
tests return false from the ismethoddescriptor() test, simply because
325-
the other tests promise more -- you can, e.g., count on having the
326-
__func__ attribute (etc) when an object passes ismethod()."""
323+
tests (ismethod(), isclass(), isfunction()) make this function return
324+
false, simply because those other tests promise more -- you can, for
325+
example, count on having the __func__ attribute when an object passes
326+
ismethod()."""
327327
if isclass(object) or ismethod(object) or isfunction(object):
328328
# mutual exclusion
329329
return False
@@ -340,8 +340,13 @@ def ismethoddescriptor(object):
340340
def isdatadescriptor(object):
341341
"""Return true if the object is a data descriptor.
342342
343+
But not if ismethod(), isclass() or isfunction() is true.
344+
343345
Data descriptors have a __set__ or a __delete__ attribute. Examples are
344-
properties (defined in Python) and getsets and members (defined in C).
346+
properties, getsets, and members. For the latter two (defined only in C
347+
extension modules) more specific tests are available as well:
348+
isgetsetdescriptor() and ismemberdescriptor(), respectively.
349+
345350
Typically, data descriptors will also have __name__ and __doc__ attributes
346351
(properties, getsets, and members have both of these attributes), but this
347352
is not guaranteed."""

0 commit comments

Comments
 (0)