gh-119180: Add VALUE_WITH_FAKE_GLOBALS format to annotationlib (#124415)
This commit is contained in:
@@ -22,8 +22,9 @@ __all__ = [
|
||||
|
||||
class Format(enum.IntEnum):
|
||||
VALUE = 1
|
||||
FORWARDREF = 2
|
||||
STRING = 3
|
||||
VALUE_WITH_FAKE_GLOBALS = 2
|
||||
FORWARDREF = 3
|
||||
STRING = 4
|
||||
|
||||
|
||||
_Union = None
|
||||
@@ -513,6 +514,8 @@ def call_annotate_function(annotate, format, *, owner=None, _is_evaluate=False):
|
||||
on the generated ForwardRef objects.
|
||||
|
||||
"""
|
||||
if format == Format.VALUE_WITH_FAKE_GLOBALS:
|
||||
raise ValueError("The VALUE_WITH_FAKE_GLOBALS format is for internal use only")
|
||||
try:
|
||||
return annotate(format)
|
||||
except NotImplementedError:
|
||||
@@ -546,7 +549,7 @@ def call_annotate_function(annotate, format, *, owner=None, _is_evaluate=False):
|
||||
argdefs=annotate.__defaults__,
|
||||
kwdefaults=annotate.__kwdefaults__,
|
||||
)
|
||||
annos = func(Format.VALUE)
|
||||
annos = func(Format.VALUE_WITH_FAKE_GLOBALS)
|
||||
if _is_evaluate:
|
||||
return annos if isinstance(annos, str) else repr(annos)
|
||||
return {
|
||||
@@ -607,7 +610,7 @@ def call_annotate_function(annotate, format, *, owner=None, _is_evaluate=False):
|
||||
argdefs=annotate.__defaults__,
|
||||
kwdefaults=annotate.__kwdefaults__,
|
||||
)
|
||||
result = func(Format.VALUE)
|
||||
result = func(Format.VALUE_WITH_FAKE_GLOBALS)
|
||||
for obj in globals.stringifiers:
|
||||
obj.__class__ = ForwardRef
|
||||
obj.__stringifier_dict__ = None # not needed for ForwardRef
|
||||
@@ -726,6 +729,8 @@ def get_annotations(
|
||||
# But if we didn't get it, we use __annotations__ instead.
|
||||
ann = _get_dunder_annotations(obj)
|
||||
return annotations_to_string(ann)
|
||||
case Format.VALUE_WITH_FAKE_GLOBALS:
|
||||
raise ValueError("The VALUE_WITH_FAKE_GLOBALS format is for internal use only")
|
||||
case _:
|
||||
raise ValueError(f"Unsupported format {format!r}")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user