Convert a Python value to a format suitable for use with the STRING format. 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.
(value)
| 1071 | |
| 1072 | |
| 1073 | def type_repr(value): |
| 1074 | """Convert a Python value to a format suitable for use with the STRING format. |
| 1075 | |
| 1076 | This is intended as a helper for tools that support the STRING format but do |
| 1077 | not have access to the code that originally produced the annotations. It uses |
| 1078 | repr() for most objects. |
| 1079 | |
| 1080 | """ |
| 1081 | if isinstance(value, (type, types.FunctionType, types.BuiltinFunctionType)): |
| 1082 | if value.__module__ == "builtins": |
| 1083 | return value.__qualname__ |
| 1084 | return f"{value.__module__}.{value.__qualname__}" |
| 1085 | elif isinstance(value, _Template): |
| 1086 | tree = _template_to_ast(value) |
| 1087 | return ast.unparse(tree) |
| 1088 | if value is ...: |
| 1089 | return "..." |
| 1090 | return repr(value) |
| 1091 | |
| 1092 | |
| 1093 | def annotations_to_string(annotations): |