summaryrefslogtreecommitdiffstats
path: root/Lib/annotationlib.py
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/annotationlib.py')
-rw-r--r--Lib/annotationlib.py19
1 files changed, 16 insertions, 3 deletions
diff --git a/Lib/annotationlib.py b/Lib/annotationlib.py
index eea2423..141e31b 100644
--- a/Lib/annotationlib.py
+++ b/Lib/annotationlib.py
@@ -413,7 +413,16 @@ class _StringifierDict(dict):
return fwdref
-def call_annotate_function(annotate, format, owner=None):
+def call_evaluate_function(evaluate, format, *, owner=None):
+ """Call an evaluate function. Evaluate functions are normally generated for
+ the value of type aliases and the bounds, constraints, and defaults of
+ type parameter objects.
+ """
+ return call_annotate_function(evaluate, format, owner=owner, _is_evaluate=True)
+
+
+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
@@ -459,8 +468,11 @@ def call_annotate_function(annotate, format, owner=None):
closure = tuple(new_closure)
else:
closure = None
- func = types.FunctionType(annotate.__code__, globals, closure=closure)
+ 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()
@@ -511,7 +523,8 @@ def call_annotate_function(annotate, format, owner=None):
closure = tuple(new_closure)
else:
closure = None
- func = types.FunctionType(annotate.__code__, globals, closure=closure)
+ 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