Skip to content

Commit 54fcfbd

Browse files
hugovkAlexWaygood
andauthored
[3.12] gh-101100: Improve documentation on function attributes (GH-112933) (#112974)
(cherry picked from commit 4c5b9c1) Co-authored-by: Alex Waygood <[email protected]>
1 parent 7d24eb8 commit 54fcfbd

13 files changed

+158
-129
lines changed

Doc/c-api/function.rst

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,20 @@ There are a few functions specific to Python functions.
3434
Return a new function object associated with the code object *code*. *globals*
3535
must be a dictionary with the global variables accessible to the function.
3636
37-
The function's docstring and name are retrieved from the code object. *__module__*
37+
The function's docstring and name are retrieved from the code object.
38+
:func:`~function.__module__`
3839
is retrieved from *globals*. The argument defaults, annotations and closure are
39-
set to ``NULL``. *__qualname__* is set to the same value as the code object's
40-
:attr:`~codeobject.co_qualname` field.
40+
set to ``NULL``. :attr:`~function.__qualname__` is set to the same value as
41+
the code object's :attr:`~codeobject.co_qualname` field.
4142
4243
4344
.. c:function:: PyObject* PyFunction_NewWithQualName(PyObject *code, PyObject *globals, PyObject *qualname)
4445
4546
As :c:func:`PyFunction_New`, but also allows setting the function object's
46-
``__qualname__`` attribute. *qualname* should be a unicode object or ``NULL``;
47-
if ``NULL``, the ``__qualname__`` attribute is set to the same value as the
48-
code object's :attr:`~codeobject.co_qualname` field.
47+
:attr:`~function.__qualname__` attribute.
48+
*qualname* should be a unicode object or ``NULL``;
49+
if ``NULL``, the :attr:`!__qualname__` attribute is set to the same value as
50+
the code object's :attr:`~codeobject.co_qualname` field.
4951
5052
.. versionadded:: 3.3
5153
@@ -62,11 +64,12 @@ There are a few functions specific to Python functions.
6264
6365
.. c:function:: PyObject* PyFunction_GetModule(PyObject *op)
6466
65-
Return a :term:`borrowed reference` to the *__module__* attribute of the
66-
function object *op*. It can be *NULL*.
67+
Return a :term:`borrowed reference` to the :attr:`~function.__module__`
68+
attribute of the :ref:`function object <user-defined-funcs>` *op*.
69+
It can be *NULL*.
6770
68-
This is normally a string containing the module name, but can be set to any
69-
other object by Python code.
71+
This is normally a :class:`string <str>` containing the module name,
72+
but can be set to any other object by Python code.
7073
7174
7275
.. c:function:: PyObject* PyFunction_GetDefaults(PyObject *op)

Doc/howto/annotations.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,8 @@ on an arbitrary object ``o``:
153153
unwrap it by accessing either ``o.__wrapped__`` or ``o.func`` as
154154
appropriate, until you have found the root unwrapped function.
155155
* If ``o`` is a callable (but not a class), use
156-
``o.__globals__`` as the globals when calling :func:`eval`.
156+
:attr:`o.__globals__ <function.__globals__>` as the globals when calling
157+
:func:`eval`.
157158

158159
However, not all string values used as annotations can
159160
be successfully turned into Python values by :func:`eval`.

Doc/howto/descriptor.rst

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1332,7 +1332,8 @@ Using the non-data descriptor protocol, a pure Python version of
13321332
The :func:`functools.update_wrapper` call adds a ``__wrapped__`` attribute
13331333
that refers to the underlying function. Also it carries forward
13341334
the attributes necessary to make the wrapper look like the wrapped
1335-
function: ``__name__``, ``__qualname__``, ``__doc__``, and ``__annotations__``.
1335+
function: :attr:`~function.__name__`, :attr:`~function.__qualname__`,
1336+
:attr:`~function.__doc__`, and :attr:`~function.__annotations__`.
13361337

13371338
.. testcode::
13381339
:hide:
@@ -1543,8 +1544,9 @@ chained together. In Python 3.11, this functionality was deprecated.
15431544
The :func:`functools.update_wrapper` call in ``ClassMethod`` adds a
15441545
``__wrapped__`` attribute that refers to the underlying function. Also
15451546
it carries forward the attributes necessary to make the wrapper look
1546-
like the wrapped function: ``__name__``, ``__qualname__``, ``__doc__``,
1547-
and ``__annotations__``.
1547+
like the wrapped function: :attr:`~function.__name__`,
1548+
:attr:`~function.__qualname__`, :attr:`~function.__doc__`,
1549+
and :attr:`~function.__annotations__`.
15481550

15491551

15501552
Member objects and __slots__

Doc/library/inspect.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1208,9 +1208,10 @@ Classes and functions
12081208
* If ``obj`` is a class, ``globals`` defaults to
12091209
``sys.modules[obj.__module__].__dict__`` and ``locals`` defaults
12101210
to the ``obj`` class namespace.
1211-
* If ``obj`` is a callable, ``globals`` defaults to ``obj.__globals__``,
1211+
* If ``obj`` is a callable, ``globals`` defaults to
1212+
:attr:`obj.__globals__ <function.__globals__>`,
12121213
although if ``obj`` is a wrapped function (using
1213-
``functools.update_wrapper()``) it is first unwrapped.
1214+
:func:`functools.update_wrapper`) it is first unwrapped.
12141215

12151216
Calling ``get_annotations`` is best practice for accessing the
12161217
annotations dict of any object. See :ref:`annotations-howto` for

Doc/library/stdtypes.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5381,10 +5381,10 @@ Code objects are used by the implementation to represent "pseudo-compiled"
53815381
executable Python code such as a function body. They differ from function
53825382
objects because they don't contain a reference to their global execution
53835383
environment. Code objects are returned by the built-in :func:`compile` function
5384-
and can be extracted from function objects through their :attr:`__code__`
5385-
attribute. See also the :mod:`code` module.
5384+
and can be extracted from function objects through their
5385+
:attr:`~function.__code__` attribute. See also the :mod:`code` module.
53865386

5387-
Accessing ``__code__`` raises an :ref:`auditing event <auditing>`
5387+
Accessing :attr:`~function.__code__` raises an :ref:`auditing event <auditing>`
53885388
``object.__getattr__`` with arguments ``obj`` and ``"__code__"``.
53895389

53905390
.. index::

Doc/library/xmlrpc.server.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,12 @@ alone XML-RPC servers.
8484

8585
Register a function that can respond to XML-RPC requests. If *name* is given,
8686
it will be the method name associated with *function*, otherwise
87-
``function.__name__`` will be used. *name* is a string, and may contain
87+
:attr:`function.__name__` will be used. *name* is a string, and may contain
8888
characters not legal in Python identifiers, including the period character.
8989

9090
This method can also be used as a decorator. When used as a decorator,
9191
*name* can only be given as a keyword argument to register *function* under
92-
*name*. If no *name* is given, ``function.__name__`` will be used.
92+
*name*. If no *name* is given, :attr:`function.__name__` will be used.
9393

9494
.. versionchanged:: 3.7
9595
:meth:`register_function` can be used as a decorator.
@@ -298,12 +298,12 @@ requests sent to Python CGI scripts.
298298

299299
Register a function that can respond to XML-RPC requests. If *name* is given,
300300
it will be the method name associated with *function*, otherwise
301-
``function.__name__`` will be used. *name* is a string, and may contain
301+
:attr:`function.__name__` will be used. *name* is a string, and may contain
302302
characters not legal in Python identifiers, including the period character.
303303

304304
This method can also be used as a decorator. When used as a decorator,
305305
*name* can only be given as a keyword argument to register *function* under
306-
*name*. If no *name* is given, ``function.__name__`` will be used.
306+
*name*. If no *name* is given, :attr:`function.__name__` will be used.
307307

308308
.. versionchanged:: 3.7
309309
:meth:`register_function` can be used as a decorator.

Doc/reference/compound_stmts.rst

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1261,7 +1261,8 @@ except that the original function is not temporarily bound to the name ``func``.
12611261
A list of :ref:`type parameters <type-params>` may be given in square brackets
12621262
between the function's name and the opening parenthesis for its parameter list.
12631263
This indicates to static type checkers that the function is generic. At runtime,
1264-
the type parameters can be retrieved from the function's ``__type_params__``
1264+
the type parameters can be retrieved from the function's
1265+
:attr:`~function.__type_params__`
12651266
attribute. See :ref:`generic-functions` for more.
12661267

12671268
.. versionchanged:: 3.12
@@ -1868,8 +1869,8 @@ like ``TYPE_PARAMS_OF_ListOrSet`` are not actually bound at runtime.
18681869
are mappings.
18691870
18701871
.. [#] A string literal appearing as the first statement in the function body is
1871-
transformed into the function's ``__doc__`` attribute and therefore the
1872-
function's :term:`docstring`.
1872+
transformed into the function's :attr:`~function.__doc__` attribute and
1873+
therefore the function's :term:`docstring`.
18731874
18741875
.. [#] A string literal appearing as the first statement in the class body is
18751876
transformed into the namespace's ``__doc__`` item and therefore the class's

0 commit comments

Comments
 (0)