mirror of
https://github.com/python/cpython.git
synced 2025-08-24 10:45:53 +00:00
gh-119180: annotationlib: Fix __all__, formatting (#122365)
This commit is contained in:
parent
016f4b5975
commit
4534068f22
3 changed files with 68 additions and 33 deletions
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue