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(),
)
| 1010 | |
| 1011 | |
| 1012 | def evaluate_forward_ref( |
| 1013 | forward_ref, |
| 1014 | *, |
| 1015 | owner=None, |
| 1016 | globals=None, |
| 1017 | locals=None, |
| 1018 | type_params=None, |
| 1019 | format=None, |
| 1020 | _recursive_guard=frozenset(), |
| 1021 | ): |
| 1022 | """Evaluate a forward reference as a type hint. |
| 1023 | |
| 1024 | This is similar to calling the ForwardRef.evaluate() method, |
| 1025 | but unlike that method, evaluate_forward_ref() also |
| 1026 | recursively evaluates forward references nested within the type hint. |
| 1027 | |
| 1028 | *forward_ref* must be an instance of ForwardRef. *owner*, if given, |
| 1029 | should be the object that holds the annotations that the forward reference |
| 1030 | derived from, such as a module, class object, or function. It is used to |
| 1031 | infer the namespaces to use for looking up names. *globals* and *locals* |
| 1032 | can also be explicitly given to provide the global and local namespaces. |
| 1033 | *type_params* is a tuple of type parameters that are in scope when |
| 1034 | evaluating the forward reference. This parameter should be provided (though |
| 1035 | it may be an empty tuple) if *owner* is not given and the forward reference |
| 1036 | does not already have an owner set. *format* specifies the format of the |
| 1037 | annotation and is a member of the annotationlib.Format enum, defaulting to |
| 1038 | VALUE. |
| 1039 | |
| 1040 | """ |
| 1041 | if format == _lazy_annotationlib.Format.STRING: |
| 1042 | return forward_ref.__forward_arg__ |
| 1043 | if forward_ref.__forward_arg__ in _recursive_guard: |
| 1044 | return forward_ref |
| 1045 | |
| 1046 | if format is None: |
| 1047 | format = _lazy_annotationlib.Format.VALUE |
| 1048 | value = forward_ref.evaluate(globals=globals, locals=locals, |
| 1049 | type_params=type_params, owner=owner, format=format) |
| 1050 | |
| 1051 | if (isinstance(value, _lazy_annotationlib.ForwardRef) |
| 1052 | and format == _lazy_annotationlib.Format.FORWARDREF): |
| 1053 | return value |
| 1054 | |
| 1055 | if isinstance(value, str): |
| 1056 | value = _make_forward_ref(value, module=forward_ref.__forward_module__, |
| 1057 | owner=owner or forward_ref.__owner__, |
| 1058 | is_argument=forward_ref.__forward_is_argument__, |
| 1059 | is_class=forward_ref.__forward_is_class__) |
| 1060 | if owner is None: |
| 1061 | owner = forward_ref.__owner__ |
| 1062 | return _eval_type( |
| 1063 | value, |
| 1064 | globals, |
| 1065 | locals, |
| 1066 | type_params, |
| 1067 | recursive_guard=_recursive_guard | {forward_ref.__forward_arg__}, |
| 1068 | format=format, |
| 1069 | owner=owner, |
no test coverage detected
searching dependent graphs…