def evaluate_forwardref(ref: ForwardRef, globalns: Dict, localns: Dict) -> Any: """Evaluate a forward reference.""" if sys.version_info > (3, 9): return ref._evaluate(globalns, localns, frozenset()) else: return ref._evaluate(globalns, localns)
def evaluate(annotation: Any, globalns: Dict, localns: Dict) -> Any: """Evaluate unresolved type annotation.""" try: if isinstance(annotation, str): ref = ForwardRef(annotation, True) annotation = ref._evaluate(globalns, localns) if isinstance(annotation, ForwardRef): annotation = annotation._evaluate(globalns, localns) elif isinstance(annotation, str): # might be a ForwardRef'ed annotation in overloaded functions ref = ForwardRef(annotation, True) annotation = ref._evaluate(globalns, localns) except (NameError, TypeError): # failed to evaluate type. skipped. pass return annotation