mirror of
https://github.com/python/cpython.git
synced 2025-07-07 19:35:27 +00:00
gh-132491: Rename annotationlib.value_to_string to type_repr (#132492)
This commit is contained in:
parent
5e80fee41a
commit
11f6603845
6 changed files with 37 additions and 26 deletions
|
@ -15,7 +15,7 @@ __all__ = [
|
|||
"get_annotate_function",
|
||||
"get_annotations",
|
||||
"annotations_to_string",
|
||||
"value_to_string",
|
||||
"type_repr",
|
||||
]
|
||||
|
||||
|
||||
|
@ -795,29 +795,27 @@ def get_annotations(
|
|||
return return_value
|
||||
|
||||
|
||||
def value_to_string(value):
|
||||
def type_repr(value):
|
||||
"""Convert a Python value to a format suitable for use with the STRING format.
|
||||
|
||||
This is inteded as a helper for tools that support the STRING format but do
|
||||
This is intended as a helper for tools that support the STRING 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 isinstance(value, (type, types.FunctionType, types.BuiltinFunctionType)):
|
||||
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_string(annotations):
|
||||
"""Convert an annotation dict containing values to approximately the STRING format."""
|
||||
return {
|
||||
n: t if isinstance(t, str) else value_to_string(t)
|
||||
n: t if isinstance(t, str) else type_repr(t)
|
||||
for n, t in annotations.items()
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue