gh-119180: annotationlib: Fix __all__, formatting (#122365)

This commit is contained in:
Jelle Zijlstra 2024-08-11 16:44:51 -07:00 committed by GitHub
parent 016f4b5975
commit 4534068f22
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 68 additions and 33 deletions

View file

@ -6,7 +6,14 @@ import functools
import sys
import types
__all__ = ["Format", "ForwardRef", "call_annotate_function", "get_annotations"]
__all__ = [
"Format",
"ForwardRef",
"call_annotate_function",
"call_evaluate_function",
"get_annotate_function",
"get_annotations",
]
class Format(enum.IntEnum):
@ -426,8 +433,7 @@ def call_evaluate_function(evaluate, format, *, owner=None):
return call_annotate_function(evaluate, format, owner=owner, _is_evaluate=True)
def call_annotate_function(annotate, format, *, owner=None,
_is_evaluate=False):
def call_annotate_function(annotate, format, *, owner=None, _is_evaluate=False):
"""Call an __annotate__ function. __annotate__ functions are normally
generated by the compiler to defer the evaluation of annotations. They
can be called with any of the format arguments in the Format enum, but
@ -473,8 +479,13 @@ def call_annotate_function(annotate, format, *, owner=None,
closure = tuple(new_closure)
else:
closure = None
func = types.FunctionType(annotate.__code__, globals, closure=closure,
argdefs=annotate.__defaults__, kwdefaults=annotate.__kwdefaults__)
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)
@ -528,8 +539,13 @@ def call_annotate_function(annotate, format, *, owner=None,
closure = tuple(new_closure)
else:
closure = None
func = types.FunctionType(annotate.__code__, globals, closure=closure,
argdefs=annotate.__defaults__, kwdefaults=annotate.__kwdefaults__)
func = types.FunctionType(
annotate.__code__,
globals,
closure=closure,
argdefs=annotate.__defaults__,
kwdefaults=annotate.__kwdefaults__,
)
result = func(Format.VALUE)
for obj in globals.stringifiers:
obj.__class__ = ForwardRef