Skip to content

Commit d2dc8a3

Browse files
committed
Improve comments for annotate callables in annotationlib
1 parent 33c9d13 commit d2dc8a3

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

Lib/annotationlib.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -759,6 +759,9 @@ def call_annotate_function(annotate, format, *, owner=None, _is_evaluate=False):
759759
# reconstruct the source. But in the dictionary that we eventually return, we
760760
# want to return objects with more user-friendly behavior, such as an __eq__
761761
# that returns a bool and an defined set of attributes.
762+
763+
# Grab and store all the annotate function attributes that we might need to access
764+
# multiple times as variables, as this could be a bit expensive for non-functions.
762765
annotate_globals = _get_annotate_attr(annotate, "__globals__", {})
763766
annotate_code = _get_annotate_attr(annotate, "__code__", None)
764767
annotate_defaults = _get_annotate_attr(annotate, "__defaults__", None)
@@ -929,6 +932,8 @@ def _get_annotate_attr(annotate, attr, default):
929932
if (unwrapped := getattr(annotate, "__wrapped__", None)) is not None:
930933
return _get_annotate_attr(unwrapped, attr, default)
931934

935+
# Partial functions and methods both store their underlying function as a
936+
# func attribute. They can wrap any callable, so we need to recursively unwrap.
932937
if (
933938
(functools := sys.modules.get("functools", None))
934939
and isinstance(annotate, functools.partial)
@@ -1004,9 +1009,10 @@ def _direct_call_annotate(func, annotate, *args):
10041009
return functools.partial(func, self, *annotate.args, **annotate.keywords)(*args)
10051010
return functools.partial(func, *annotate.args, **annotate.keywords)(*args)
10061011

1007-
# If annotate is a cached function, we've now updated the function data, so
1008-
# let's not use the old cache. Furthermore, we're about to call the function
1009-
# and never use it again, so let's not bother trying to cache it.
1012+
# If annotate is a cached function, we've now updated the function data, so
1013+
# let's not use the old cache. Furthermore, we're about to call the function
1014+
# and never use it again, so let's not bother trying to cache it.
1015+
10101016
# Or, if it's a normal function or unsupported callable, we should just call it.
10111017
return func(*args)
10121018

0 commit comments

Comments
 (0)