@@ -3080,35 +3080,37 @@ Introspection helpers
30803080 Return a dictionary containing type hints for a function, method, module
30813081 or class object.
30823082
3083- This is often the same as ``obj.__annotations__ ``. In addition,
3084- forward references encoded as string literals are handled by evaluating
3085- them in ``globals ``, ``locals `` and (where applicable)
3086- :ref: `type parameter <type-params >` namespaces.
3087- For a class ``C ``, return
3088- a dictionary constructed by merging all the ``__annotations__ `` along
3089- ``C.__mro__ `` in reverse order.
3090-
3091- The function recursively replaces all ``Annotated[T, ...] `` with ``T ``,
3092- unless ``include_extras `` is set to ``True `` (see :class: `Annotated ` for
3093- more information). For example:
3094-
3095- .. testcode ::
3096-
3097- class Student(NamedTuple):
3098- name: Annotated[str, 'some marker']
3099-
3100- assert get_type_hints(Student) == {'name': str}
3101- assert get_type_hints(Student, include_extras=False) == {'name': str}
3102- assert get_type_hints(Student, include_extras=True) == {
3103- 'name': Annotated[str, 'some marker']
3104- }
3083+ This is often the same as ``obj.__annotations__ ``, but this function makes
3084+ the following changes to the annotations dictionary:
3085+
3086+ * Forward references encoded as string literals or :class: `ForwardRef `
3087+ objects are handled by evaluating them in *globalns *, *localns *, and
3088+ (where applicable) *obj *'s :ref: `type parameter <type-params >` namespace.
3089+ If *globalns * or *localns * is not given, appropriate namespace
3090+ dictionaries are inferred from *obj *.
3091+ * ``None `` is replaced with :class: `types.NoneType `.
3092+ * If :func: `@no_type_check <no_type_check> ` has been applied to *obj *, an
3093+ empty dictionary is returned.
3094+ * If *obj * is a class ``C ``, the function returns a dictionary that merges
3095+ annotations from ``C ``'s base classes with those on ``C `` directly. This
3096+ is done by traversing ``C.__mro__ `` and iteratively combining
3097+ ``__annotations__ `` dictionaries. Annotations on classes appearing
3098+ earlier in the :term: `method resolution order ` always take precedence over
3099+ annotations on classes appearing later in the method resolution order.
3100+ * The function recursively replaces all occurrences of ``Annotated[T, ...] ``
3101+ with ``T ``, unless *include_extras * is set to ``True `` (see
3102+ :class: `Annotated ` for more information).
3103+
3104+ See also :func: `inspect.get_annotations `, a lower-level function that
3105+ returns annotations more directly.
31053106
31063107 .. note ::
31073108
3108- :func: `get_type_hints ` does not work with imported
3109- :ref: `type aliases <type-aliases >` that include forward references.
3110- Enabling postponed evaluation of annotations (:pep: `563 `) may remove
3111- the need for most forward references.
3109+ If any forward references in the annotations of *obj * are not resolvable
3110+ or are not valid Python code, this function will raise an exception
3111+ such as :exc: `NameError `. For example, this can happen with imported
3112+ :ref: `type aliases <type-aliases >` that include forward references,
3113+ or with names imported under :data: `if TYPE_CHECKING <TYPE_CHECKING> `.
31123114
31133115 .. versionchanged :: 3.9
31143116 Added ``include_extras `` parameter as part of :pep: `593 `.
0 commit comments