aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Lib/annotationlib.py
diff options
context:
space:
mode:
authorJelle Zijlstra <jelle.zijlstra@gmail.com>2025-04-15 13:10:53 -0700
committerGitHub <noreply@github.com>2025-04-15 20:10:53 +0000
commit11f66038453dff51ba4ee80460e30acf276d472a (patch)
treea05d60786525c27efcb496a6df3e6f7d7687e568 /Lib/annotationlib.py
parent5e80fee41a61bbb39c6054c5a5d82dc80b1adf8a (diff)
downloadcpython-11f66038453dff51ba4ee80460e30acf276d472a.tar.gz
cpython-11f66038453dff51ba4ee80460e30acf276d472a.zip
gh-132491: Rename annotationlib.value_to_string to type_repr (#132492)
Diffstat (limited to 'Lib/annotationlib.py')
-rw-r--r--Lib/annotationlib.py12
1 files changed, 5 insertions, 7 deletions
diff --git a/Lib/annotationlib.py b/Lib/annotationlib.py
index a5defefb10e..322b6ded2b3 100644
--- a/Lib/annotationlib.py
+++ b/Lib/annotationlib.py
@@ -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()
}