Teks sumber — English
Terjemahan — Bahasa Indonesia
The :mod:`!annotationlib` module provides tools for introspecting :term:`annotations <annotation>` on modules, classes, and functions.
Classes
Classes
The *globals*, *locals*, and *type_params* parameters provide a more precise mechanism for influencing the names that are available when the :class:`~ForwardRef` is evaluated. *globals* and *locals* are passed to :func:`eval`, representing the global and local namespaces in which the name is evaluated. The *type_params* parameter is relevant for objects created using the native syntax for :ref:`generic classes <generic-classes>` and :ref:`functions <generic-functions>`. It is a tuple of :ref:`type parameters <type-params>` that are in scope while the forward reference is being evaluated. For example, if evaluating a :class:`~ForwardRef` retrieved from an annotation found in the class namespace of a generic class ``C``, *type_params* should be set to ``C.__type_params__``.
For example, this is used to implement the :attr:`~Format.STRING` format for :class:`typing.TypedDict` classes created through the functional syntax:
This helper function is required because annotate functions generated by the compiler for functions, classes, and modules only support the :attr:`~Format.VALUE` format when called directly. To support other formats, this function calls the annotate function in a special environment that allows it to produce annotations in the other formats. This is a useful building block when implementing functionality that needs to partially evaluate annotations while a class is being constructed.
If *obj* doesn't have an annotations dict, returns an empty dict. (Functions and methods always have an annotations dict; classes, modules, and other types of callables may not.)
Ignores inherited annotations on classes, as well as annotations on metaclasses. If a class doesn't have its own annotations dict, returns an empty dict.
A :ref:`metaclass <metaclasses>` may want to inspect or even modify the annotations in a class body during class creation. Doing so requires retrieving annotations from the class namespace dictionary. For classes created with ``from __future__ import annotations``, the annotations will be in the ``__annotations__`` key of the dictionary. For other classes with annotations, :func:`get_annotate_from_class_namespace` can be used to get the annotate function, and :func:`call_annotate_function` can be used to call it and retrieve the annotations. Using the :attr:`~Format.FORWARDREF` format will usually be best, because this allows the annotations to refer to names that cannot yet be resolved when the class is created.
Custom :term:`annotate functions <annotate function>` may be literal functions like those automatically generated for functions, classes, and modules. Or, they may wish to utilise the encapsulation provided by classes, in which case any :term:`callable` can be used as an :term:`annotate function`.
>>> def f(x: (1).__class__.__base__.__subclasses__()[-1].__init__.__builtins__["print"]("Hello world")): pass
...
>>> annotationlib.get_annotations(f, format=annotationlib.Format.STRING)
Hello world
{'x': 'None'}