PyDocs

Nav

Development Tools

get_type_hints

typing

function (source)
  • get_type_hints(obj, globalns=None, localns=None, include_extras=False, *, format=Format.VALUE)
Return type hints for an object.

This is often the same as annotationlib.get_annotations(obj) or obj.__annotations__,
but it handles forward references encoded as string literals and recursively replaces all
'Annotated[T, ...]' with 'T' (unless 'include_extras=True').

The argument may be a module, class, method, or function. The annotations
are returned as a dictionary. For classes, annotations include also
inherited members.

TypeError is raised if the argument is not of a type that can contain
annotations, and an empty dictionary is returned if no annotations are
present.

BEWARE -- the behavior of globalns and localns is counterintuitive
(unless you are familiar with how eval() and exec() work).  The
search order is locals first, then globals.

- If no dict arguments are passed, an attempt is made to use the
  globals from obj (or the respective module's globals for classes),
  and these are also used as the locals.  If the object does not appear
  to have globals, an empty dictionary is used.  For classes, the search
  order is globals first then locals.

- If one dict argument is passed, it is used for both globals and
  locals.

- If two dict arguments are passed, they specify globals and
  locals, respectively.
Changed in 3.9, 3.11, 3.14
  • 3.9: Added include_extras parameter as part of PEP 593. See the documentation on Annotated for more information.
  • 3.11: Previously, Optional[t] was added for function and method annotations if a default value equal to None was set. Now the annotation is returned unchanged.
  • 3.14: Added the format parameter. See the documentation on annotationlib.get_annotations() for more information.
  • 3.14: Calling get_type_hints() on instances is no longer supported. Some instances were accepted in earlier versions as an undocumented implementation detail.
0 Examples
No examples for typing/get_type_hints.
See Also
No see-alsos for typing/get_type_hints
0 Notes
No notes for get_type_hints