Skip to content

Commit a1daa6e

Browse files
committed
Improve documentation for custom callable annotate functions
1 parent 943181c commit a1daa6e

File tree

1 file changed

+21
-11
lines changed

1 file changed

+21
-11
lines changed

Doc/library/annotationlib.rst

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,6 @@ annotations from the class and puts them in a separate attribute:
510510
return typ
511511
512512
513-
514513
Creating a custom callable annotate function
515514
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
516515

@@ -519,22 +518,33 @@ automatically generated for functions, classes, and modules. Or, they may wish t
519518
the encapsulation provided by classes, in which case any :term:`callable` can be used as
520519
an :term:`annotate function`.
521520

522-
However, :term:`methods <method>`, class instances that implement
523-
:meth:`object.__call__`, and most other callables, do not provide the same attributes as
524-
true functions, which are needed for the :attr:`~Format.VALUE_WITH_FAKE_GLOBALS`
525-
machinery to work. :func:`call_annotate_function` and other :mod:`annotationlib`
526-
functions will attempt to infer those attributes where possible, but some of them must
527-
always be present for :attr:`~Format.VALUE_WITH_FAKE_GLOBALS` to work.
528-
529-
Below is an example of a callable class that provides the necessary attributes to be
530-
used with all formats, and takes advantage of class encapsulation:
521+
To provide the :attr:`~Format.VALUE`, :attr:`~Format.STRING`, or
522+
:attr:`~Format.FORWARDREF` formats directly, an :ref:`annotate function` must provide
523+
the following attribute:
524+
- A callable ``__call__`` with signature ``__call__(format, /) -> dict``, that does not
525+
raise a :err:`NotImplementedError` when called with a supported format.
526+
527+
To provide the :attr:`~Format.VALUE_WITH_FAKE_GLOBALS` format, which is used to
528+
automatically generate :attr:`~Format.STRING` or :attr:`~Format.FORWARDREF` if they are
529+
not supported directly, :term:`annotate functions <annotate function>` must provide the
530+
following attributes:
531+
- A callable ``__call__`` with signature ``__call__(format, /) -> dict``, that does not
532+
raise a :err:`NotImplementedError` when called with
533+
:attr:`~Format.VALUE_WITH_FAKE_GLOBALS`.
534+
- A :ref:`code object <code-objects>` ``__code__`` containing the compiled code for the
535+
annotate function.
536+
- Optional: A tuple of the function's positional defaults ``__kwdefaults__``, if the
537+
function represented by ``__code__`` uses any positional defaults.
538+
- Optional: A dict of the function's keyword defaults ``__defaults__``, if the function
539+
represented by ``__code__`` uses any keyword defaults.
540+
- Optional: All other :ref:`function attributes <inspect-types>`.
531541

532542
.. code-block:: python
533543
534544
class Annotate:
535545
called_formats = []
536546
537-
def __call__(self, format=None, *, _self=None):
547+
def __call__(self, format=None, /, *, _self=None):
538548
# When called with fake globals, `_self` will be the
539549
# actual self value, and `self` will be the format.
540550
if _self is not None:

0 commit comments

Comments
 (0)