gh-124412: Add helpers for converting annotations to source format (#124551)

This commit is contained in:
Jelle Zijlstra 2024-09-25 17:01:09 -07:00 committed by GitHub
parent 0268b072d8
commit 4e829c0e6f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 113 additions and 42 deletions

View file

@ -15,6 +15,8 @@ __all__ = [
"call_evaluate_function",
"get_annotate_function",
"get_annotations",
"annotations_to_source",
"value_to_source",
]
@ -693,7 +695,7 @@ def get_annotations(
return ann
# But if we didn't get it, we use __annotations__ instead.
ann = _get_dunder_annotations(obj)
return ann
return annotations_to_source(ann)
case _:
raise ValueError(f"Unsupported format {format!r}")
@ -762,6 +764,33 @@ def get_annotations(
return return_value
def value_to_source(value):
"""Convert a Python value to a format suitable for use with the SOURCE format.
This is inteded as a helper for tools that support the SOURCE format but do
not have access to the code that originally produced the annotations. It uses
repr() for most objects.
"""
if isinstance(value, type):
if value.__module__ == "builtins":
return value.__qualname__
return f"{value.__module__}.{value.__qualname__}"
if value is ...:
return "..."
if isinstance(value, (types.FunctionType, types.BuiltinFunctionType)):
return value.__name__
return repr(value)
def annotations_to_source(annotations):
"""Convert an annotation dict containing values to approximately the SOURCE format."""
return {
n: t if isinstance(t, str) else value_to_source(t)
for n, t in annotations.items()
}
def _get_and_call_annotate(obj, format):
annotate = get_annotate_function(obj)
if annotate is not None: