From f482def9e6c0288206e170e31fde963bfe2daee0 Mon Sep 17 00:00:00 2001 From: crypt0lith <118923461+crypt0lith@users.noreply.github.com> Date: Sat, 17 Oct 2026 16:04:44 -0500 Subject: [PATCH] Fix docs saying built-in functions have __self__ set to None Since PEP 3121, functions in C modules are bound to their module, so their __self__ is the module object. The inspect.isbuiltin docstring, the inspect attribute table and the data model reference still say it is None, as it was in Python 2. Describe it as the module object the function was defined in, or None. --- Doc/library/inspect.rst | 2 ++ Doc/reference/datamodel.rst | 3 ++- Lib/inspect.py | 3 ++- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/Doc/library/inspect.rst b/Doc/library/inspect.rst index 624b13029ec78ba..196479eb7550e25 100644 --- a/Doc/library/inspect.rst +++ b/Doc/library/inspect.rst @@ -319,6 +319,8 @@ attributes (see :ref:`import-mod-attrs` for module attributes): +-----------------+-------------------+---------------------------+ | | __self__ | instance to which a | | | | method is bound, or | +| | | module object a function | +| | | was defined in, or | | | | ``None`` | +-----------------+-------------------+---------------------------+ diff --git a/Doc/reference/datamodel.rst b/Doc/reference/datamodel.rst index c860a3356628af1..d2089724fa29572 100644 --- a/Doc/reference/datamodel.rst +++ b/Doc/reference/datamodel.rst @@ -863,7 +863,8 @@ determined by the C function. Special read-only attributes: * :attr:`!__doc__` is the function's documentation string, or ``None`` if unavailable. See :attr:`function.__doc__`. * :attr:`!__name__` is the function's name. See :attr:`function.__name__`. -* :attr:`!__self__` is set to ``None`` (but see the next item). +* :attr:`!__self__` is the module object the function was defined in or + ``None`` if unavailable. * :attr:`!__module__` is the name of the module the function was defined in or ``None`` if unavailable. See :attr:`function.__module__`. diff --git a/Lib/inspect.py b/Lib/inspect.py index bceed35a53abba1..db9129d47db6abe 100644 --- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -441,7 +441,8 @@ def isbuiltin(object): Built-in functions and methods provide these attributes: __doc__ documentation string __name__ original name of this function or method - __self__ instance to which a method is bound, or None""" + __self__ instance to which a method is bound, or module object + a function was defined in, or None""" return isinstance(object, types.BuiltinFunctionType) def ismethodwrapper(object):