Evaluate a forward reference as a type hint. This is similar to calling the ForwardRef.evaluate() method, but unlike that method, evaluate_forward_ref() also recursively evaluates forward references nested within the type hint. *forward_ref* must be an instance of ForwardRef. *owne
(
forward_ref,
*,
owner=None,
globals=None,
locals=None,
type_params=None,
format=None,
_recursive_guard=frozenset(),
)
| 962 | |
| 963 | |
| 964 | def evaluate_forward_ref( |
| 965 | forward_ref, |
| 966 | *, |
| 967 | owner=None, |
| 968 | globals=None, |
| 969 | locals=None, |
| 970 | type_params=None, |
| 971 | format=None, |
| 972 | _recursive_guard=frozenset(), |
| 973 | ): |
| 974 | """Evaluate a forward reference as a type hint. |
| 975 | |
| 976 | This is similar to calling the ForwardRef.evaluate() method, |
| 977 | but unlike that method, evaluate_forward_ref() also |
| 978 | recursively evaluates forward references nested within the type hint. |
| 979 | |
| 980 | *forward_ref* must be an instance of ForwardRef. *owner*, if given, |
| 981 | should be the object that holds the annotations that the forward reference |
| 982 | derived from, such as a module, class object, or function. It is used to |
| 983 | infer the namespaces to use for looking up names. *globals* and *locals* |
| 984 | can also be explicitly given to provide the global and local namespaces. |
| 985 | *type_params* is a tuple of type parameters that are in scope when |
| 986 | evaluating the forward reference. This parameter should be provided (though |
| 987 | it may be an empty tuple) if *owner* is not given and the forward reference |
| 988 | does not already have an owner set. *format* specifies the format of the |
| 989 | annotation and is a member of the annotationlib.Format enum, defaulting to |
| 990 | VALUE. |
| 991 | |
| 992 | """ |
| 993 | if format == _lazy_annotationlib.Format.STRING: |
| 994 | return forward_ref.__forward_arg__ |
| 995 | if forward_ref.__forward_arg__ in _recursive_guard: |
| 996 | return forward_ref |
| 997 | |
| 998 | if format is None: |
| 999 | format = _lazy_annotationlib.Format.VALUE |
| 1000 | value = forward_ref.evaluate(globals=globals, locals=locals, |
| 1001 | type_params=type_params, owner=owner, format=format) |
| 1002 | |
| 1003 | if (isinstance(value, _lazy_annotationlib.ForwardRef) |
| 1004 | and format == _lazy_annotationlib.Format.FORWARDREF): |
| 1005 | return value |
| 1006 | |
| 1007 | if isinstance(value, str): |
| 1008 | value = _make_forward_ref(value, module=forward_ref.__forward_module__, |
| 1009 | owner=owner or forward_ref.__owner__, |
| 1010 | is_argument=forward_ref.__forward_is_argument__, |
| 1011 | is_class=forward_ref.__forward_is_class__) |
| 1012 | if owner is None: |
| 1013 | owner = forward_ref.__owner__ |
| 1014 | return _eval_type( |
| 1015 | value, |
| 1016 | globals, |
| 1017 | locals, |
| 1018 | type_params, |
| 1019 | recursive_guard=_recursive_guard | {forward_ref.__forward_arg__}, |
| 1020 | format=format, |
| 1021 | owner=owner, |
no test coverage detected