8000 gh-125618: Make FORWARDREF format succeed more often by JelleZijlstra · Pull Request #132818 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-125618: Make FORWARDREF format succeed more often #132818

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
May 4, 2025
Prev Previous commit
Next Next commit
Merge remote-tracking branch 'upstream/main' into fwdref-format
  • Loading branch information
JelleZijlstra committed May 4, 2025
commit ec613f819ce26b15af71b3e527a90d24e9a88eb7
25 changes: 21 additions & 4 deletions Lib/annotationlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,11 @@ def transmogrify(self):
obj.__arg__ = obj.__ast_node__
obj.__ast_node__ = None

def create_unique_name(self):
name = f"__annotationlib_name_{self.next_id}__"
self.next_id += 1
return name


def call_evaluate_function(evaluate, format, *, owner=None):
"""Call an evaluate function. Evaluate functions are normally generated for
Expand Down Expand Up @@ -632,7 +637,7 @@ def call_annotate_function(annotate, format, *, owner=None, _is_evaluate=False):
# 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({})
globals = _StringifierDict({}, format=format)
is_class = isinstance(owner, type)
closure = _build_closure(
annotate, owner, is_class, globals, allow_evaluation=False
Expand Down Expand Up @@ -671,7 +676,13 @@ def call_annotate_function(annotate, format, *, owner=None, _is_evaluate=False):
# that returns a bool and an defined set of attributes.
namespace = {**annotate.__builtins__, **annotate.__globals__}
is_class = isinstance(owner, type)
globals = _StringifierDict(namespace, annotate.__globals__, owner, is_class)
globals = _StringifierDict(
namespace,
globals=annotate.__globals__,
owner=owner,
is_class=is_class,
format=format,
)
closure = _build_closure(
annotate, owner, is_class, globals, allow_evaluation=True
)
Expand Down Expand Up @@ -760,8 +771,14 @@ def _build_closure(annotate, owner, is_class, stringifier_dict, *, allow_evaluat
return tuple(new_closure)


def get_annotate_function(obj):
"""Get the __annotate__ function for an object.
def _stringify_single(anno):
if anno is ...:
return "..."
# We have to handle str specially to support PEP 563 stringified annotations.
elif isinstance(anno, str):
return anno
else:
return repr(anno)


def get_annotate_from_class_namespace(obj):
Expand Down
Loading
You are viewing a condensed version of this merge commit. You can view the full changes here.
0