-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
Closed
Labels
Description
Feature or enhancement
Let's say you have a dict of some custom annotations like I have in #122262
How users are expected to convert say an annotation dict of {'user': CustomUser[AuthToken], 'auth_callback': Callable[[CustomUser[T]], T]}
to string?
There are several ways right now:
repr
for simple types, which might not work for complex onesLines 466 to 501 in 536bc8a
if format == Format.SOURCE: # SOURCE is implemented by calling the annotate function in a special # environment where every name lookup results in an instance of _Stringifier. # _Stringifier supports every dunder operation and returns a new _Stringifier. # At the end, we get a dictionary that mostly contains _Stringifier objects (or # possibly constants if the annotate function uses them directly). We then # convert each of those into a string to get an approximation of the # original source. globals = _StringifierDict({}) if annotate.__closure__: freevars = annotate.__code__.co_freevars new_closure = [] for i, cell in enumerate(annotate.__closure__): if i < len(freevars): name = freevars[i] else: name = "__cell__" fwdref = _Stringifier(ast.Name(id=name)) new_closure.append(types.CellType(fwdref)) closure = tuple(new_closure) else: closure = None func = types.FunctionType( annotate.__code__, globals, closure=closure, argdefs=annotate.__defaults__, kwdefaults=annotate.__kwdefaults__, ) annos = func(Format.VALUE) if _is_evaluate: return annos if isinstance(annos, str) else repr(annos) return { key: val if isinstance(val, str) else repr(val) for key, val in annos.items() } annotate
objectLines 2955 to 2956 in 536bc8a
def _convert_to_source(types): return {n: t if isinstance(t, str) else _type_repr(t) for n, t in types.items()}
I propose adding a public and documented API for that.