diff --git a/Doc/library/typing.rst b/Doc/library/typing.rst index b2167cbc63a1ff..e71882c176b276 100644 --- a/Doc/library/typing.rst +++ b/Doc/library/typing.rst @@ -3727,6 +3727,24 @@ Introspection helpers .. versionadded:: 3.14 +.. function:: evaluate_type(type_hint, *, owner=None, globals=None, locals=None, type_params=None, format=annotationlib.Format.VALUE) + + Evaluate a :term:`type hint`, by resolving any forward references nested + within the type hint. + + This is similar to calling :func:`evaluate_forward_ref`, but unlike that + method, :func:`evaluate_type` also supports arbitrary type hints. + + See the documentation for :meth:`annotationlib.ForwardRef.evaluate` for + the meaning of the *owner*, *globals*, *locals*, *type_params*, and *format* parameters. + + .. caution:: + + This function may execute arbitrary code contained the type hint. + See :ref:`annotationlib-security` for more information. + + .. versionadded:: next + .. data:: NoDefault A sentinel object used to indicate that a type parameter has no default diff --git a/Lib/typing.py b/Lib/typing.py index 715d08e0e1603e..4c4ee5295d6d6e 100644 --- a/Lib/typing.py +++ b/Lib/typing.py @@ -1051,6 +1051,24 @@ def evaluate_forward_ref( ) +def evaluate_type( + type_hint, + *, + owner=None, + globalns=None, + localns=None, + type_params=None, + format=annotationlib.Format.VALUE, +): + """Evaluate a type hint, by resolving any forward references. + + This is similar to the evaluate_forward_ref() method, but unlike that method, + evaluate_type() also supports arbitrary type hints. + """ + + return _eval_type(type_hint, owner=owner, globalns=globalns, localns=localns, type_params=type_params, format=format) + + def _is_unpacked_typevartuple(x: Any) -> bool: # Need to check 'is True' here # See: https://github.com/python/cpython/issues/137706 diff --git a/Misc/NEWS.d/next/Library/2026-05-24-21-20-19.gh-issue-85668.KwCVdi.rst b/Misc/NEWS.d/next/Library/2026-05-24-21-20-19.gh-issue-85668.KwCVdi.rst new file mode 100644 index 00000000000000..46785e8c6538f5 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-05-24-21-20-19.gh-issue-85668.KwCVdi.rst @@ -0,0 +1 @@ +Add :func:`typing.evaluate_type` function.