Skip to content

Commit c8de0ec

Browse files
miss-islingtonJelleZijlstraAlexWaygood
authored
[3.12] Improve documentation for typing.get_type_hints (GH-119928) (#119944)
- Explicit list of what it does that is different from "just return __annotations__" - Remove reference to PEP 563; adding the future import doesn't do anything to type aliases, and in general it will never make get_type_hints() less likely to fail. - Remove example, as the Annotated docs already have a similar example, and it's unbalanced to have one example about this one edge case but not about other behaviors of the function. (cherry picked from commit aa9fe98) Co-authored-by: Jelle Zijlstra <[email protected]> Co-authored-by: Alex Waygood <[email protected]>
1 parent e57a4a1 commit c8de0ec

File tree

1 file changed

+28
-26
lines changed

1 file changed

+28
-26
lines changed

Doc/library/typing.rst

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2883,35 +2883,37 @@ Introspection helpers
28832883
Return a dictionary containing type hints for a function, method, module
28842884
or class object.
28852885

2886-
This is often the same as ``obj.__annotations__``. In addition,
2887-
forward references encoded as string literals are handled by evaluating
2888-
them in ``globals``, ``locals`` and (where applicable)
2889-
:ref:`type parameter <type-params>` namespaces.
2890-
For a class ``C``, return
2891-
a dictionary constructed by merging all the ``__annotations__`` along
2892-
``C.__mro__`` in reverse order.
2893-
2894-
The function recursively replaces all ``Annotated[T, ...]`` with ``T``,
2895-
unless ``include_extras`` is set to ``True`` (see :class:`Annotated` for
2896-
more information). For example:
2897-
2898-
.. testcode::
2899-
2900-
class Student(NamedTuple):
2901-
name: Annotated[str, 'some marker']
2902-
2903-
assert get_type_hints(Student) == {'name': str}
2904-
assert get_type_hints(Student, include_extras=False) == {'name': str}
2905-
assert get_type_hints(Student, include_extras=True) == {
2906-
'name': Annotated[str, 'some marker']
2907-
}
2886+
This is often the same as ``obj.__annotations__``, but this function makes
2887+
the following changes to the annotations dictionary:
2888+
2889+
* Forward references encoded as string literals or :class:`ForwardRef`
2890+
objects are handled by evaluating them in *globalns*, *localns*, and
2891+
(where applicable) *obj*'s :ref:`type parameter <type-params>` namespace.
2892+
If *globalns* or *localns* is not given, appropriate namespace
2893+
dictionaries are inferred from *obj*.
2894+
* ``None`` is replaced with :class:`types.NoneType`.
2895+
* If :func:`@no_type_check <no_type_check>` has been applied to *obj*, an
2896+
empty dictionary is returned.
2897+
* If *obj* is a class ``C``, the function returns a dictionary that merges
2898+
annotations from ``C``'s base classes with those on ``C`` directly. This
2899+
is done by traversing ``C.__mro__`` and iteratively combining
2900+
``__annotations__`` dictionaries. Annotations on classes appearing
2901+
earlier in the :term:`method resolution order` always take precedence over
2902+
annotations on classes appearing later in the method resolution order.
2903+
* The function recursively replaces all occurrences of ``Annotated[T, ...]``
2904+
with ``T``, unless *include_extras* is set to ``True`` (see
2905+
:class:`Annotated` for more information).
2906+
2907+
See also :func:`inspect.get_annotations`, a lower-level function that
2908+
returns annotations more directly.
29082909

29092910
.. note::
29102911

2911-
:func:`get_type_hints` does not work with imported
2912-
:ref:`type aliases <type-aliases>` that include forward references.
2913-
Enabling postponed evaluation of annotations (:pep:`563`) may remove
2914-
the need for most forward references.
2912+
If any forward references in the annotations of *obj* are not resolvable
2913+
or are not valid Python code, this function will raise an exception
2914+
such as :exc:`NameError`. For example, this can happen with imported
2915+
:ref:`type aliases <type-aliases>` that include forward references,
2916+
or with names imported under :data:`if TYPE_CHECKING <TYPE_CHECKING>`.
29152917

29162918
.. versionchanged:: 3.9
29172919
Added ``include_extras`` parameter as part of :pep:`593`.

0 commit comments

Comments
 (0)