Python Glossary Русский

Python 3.14+typingfunction

typing.evaluate_forward_ref()

annotation introspection

Evaluates a ForwardRef (a deferred string annotation) into a real type, resolving nested forward references recursively. Python 3.14+.

Syntax

typing.evaluate_forward_ref(forward_ref, *, owner=None, globals=None, locals=None, type_params=None, format=annotationlib.Format.VALUE)

You rarely need it directly: typing.get_type_hints() resolves every annotation of a function or class at once, while this call is for a single ForwardRef you pulled out of someone else's annotation. The name is looked up in the owner/globals/locals you pass, so a name that is not visible there raises NameError; with format=annotationlib.Format.FORWARDREF the reference is left unresolved instead.

Examples

import typing
ref = typing.ForwardRef('int')
print(typing.evaluate_forward_ref(ref))    # → <class 'int'>
print(typing.evaluate_forward_ref(typing.ForwardRef('list[int]')))   # → list[int]
print(typing.evaluate_forward_ref(typing.ForwardRef('T'), globals={'T': str}))   # → <class 'str'>
print(typing.evaluate_forward_ref(ref) is int)   # → True

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy