Skip to content

Commit 46ef8b1

Browse files
committed
Remove defaults fro non-function annotates in call_annotate_function()
1 parent 5440128 commit 46ef8b1

File tree

2 files changed

+20
-34
lines changed

2 files changed

+20
-34
lines changed

Lib/annotationlib.py

Lines changed: 19 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -711,9 +711,6 @@ def call_annotate_function(annotate, format, *, owner=None, _is_evaluate=False):
711711
return annotate(format)
712712
except NotImplementedError:
713713
pass
714-
715-
annotate_defaults = getattr(annotate, "__defaults__", None)
716-
annotate_kwdefaults = getattr(annotate, "__kwdefaults__", None)
717714
if format == Format.STRING:
718715
# STRING is implemented by calling the annotate function in a special
719716
# environment where every name lookup results in an instance of _Stringifier.
@@ -737,16 +734,14 @@ def call_annotate_function(annotate, format, *, owner=None, _is_evaluate=False):
737734
globals = _StringifierDict({}, format=format)
738735
is_class = isinstance(owner, type)
739736
closure, _ = _build_closure(
740-
annotate, owner, is_class, globals,
741-
getattr(annotate, "__globals__", {}), allow_evaluation=False
737+
annotate, owner, is_class, globals, allow_evaluation=False
742738
)
743-
annotate_code = annotate.__code__
744739
func = types.FunctionType(
745-
annotate_code,
740+
annotate.__code__,
746741
globals,
747742
closure=closure,
748-
argdefs=annotate_defaults,
749-
kwdefaults=annotate_kwdefaults,
743+
argdefs=annotate.__defaults__,
744+
kwdefaults=annotate.__kwdefaults__,
750745
)
751746
annos = func(Format.VALUE_WITH_FAKE_GLOBALS)
752747
if _is_evaluate:
@@ -773,31 +768,24 @@ def call_annotate_function(annotate, format, *, owner=None, _is_evaluate=False):
773768
# reconstruct the source. But in the dictionary that we eventually return, we
774769
# want to return objects with more user-friendly behavior, such as an __eq__
775770
# that returns a bool and an defined set of attributes.
776-
annotate_globals = getattr(annotate, "__globals__", {})
777-
if annotate_builtins := getattr(annotate, "__builtins__", None):
778-
namespace = {**annotate_builtins, **annotate_globals}
779-
elif annotate_builtins := annotate_globals.get("__builtins__"):
780-
namespace = {**annotate_builtins, **annotate_globals}
781-
else:
782-
namespace = {**builtins.__dict__, **annotate_globals}
771+
namespace = {**annotate.__builtins__, **annotate.__globals__}
783772
is_class = isinstance(owner, type)
784773
globals = _StringifierDict(
785774
namespace,
786-
globals=annotate_globals,
775+
globals=annotate.__globals__,
787776
owner=owner,
788777
is_class=is_class,
789778
format=format,
790779
)
791780
closure, cell_dict = _build_closure(
792-
annotate, owner, is_class, globals, annotate_globals, allow_evaluation=True
781+
annotate, owner, is_class, globals, allow_evaluation=True
793782
)
794-
annotate_code = annotate.__code__
795783
func = types.FunctionType(
796-
annotate_code,
784+
annotate.__code__,
797785
globals,
798786
closure=closure,
799-
argdefs=annotate_defaults,
800-
kwdefaults=annotate_kwdefaults,
787+
argdefs=annotate.__defaults__,
788+
kwdefaults=annotate.__kwdefaults__,
801789
)
802790
try:
803791
result = func(Format.VALUE_WITH_FAKE_GLOBALS)
@@ -814,21 +802,20 @@ def call_annotate_function(annotate, format, *, owner=None, _is_evaluate=False):
814802
# a value in certain cases where an exception gets raised during evaluation.
815803
globals = _StringifierDict(
816804
{},
817-
globals=annotate_globals,
805+
globals=annotate.__globals__,
818806
owner=owner,
819807
is_class=is_class,
820808
format=format,
821809
)
822810
closure, cell_dict = _build_closure(
823-
annotate, owner, is_class, globals,
824-
annotate_globals, allow_evaluation=False
811+
annotate, owner, is_class, globals, allow_evaluation=False
825812
)
826813
func = types.FunctionType(
827-
annotate_code,
814+
annotate.__code__,
828815
globals,
829816
closure=closure,
830-
argdefs=annotate_defaults,
831-
kwdefaults=annotate_kwdefaults,
817+
argdefs=annotate.__defaults__,
818+
kwdefaults=annotate.__kwdefaults__,
832819
)
833820
result = func(Format.VALUE_WITH_FAKE_GLOBALS)
834821
globals.transmogrify(cell_dict)
@@ -854,13 +841,12 @@ def call_annotate_function(annotate, format, *, owner=None, _is_evaluate=False):
854841
raise ValueError(f"Invalid format: {format!r}")
855842

856843

857-
def _build_closure(annotate, owner, is_class, stringifier_dict,
858-
annotate_globals, *, allow_evaluation):
859-
if not (annotate_closure := getattr(annotate, "__closure__", None)):
844+
def _build_closure(annotate, owner, is_class, stringifier_dict, *, allow_evaluation):
845+
if not annotate.__closure__:
860846
return None, None
861847
new_closure = []
862848
cell_dict = {}
863-
for name, cell in zip(annotate.__code__.co_freevars, annotate_closure, strict=True):
849+
for name, cell in zip(annotate.__code__.co_freevars, annotate.__closure__, strict=True):
864850
cell_dict[name] = cell
865851
new_cell = None
866852
if allow_evaluation:
@@ -875,7 +861,7 @@ def _build_closure(annotate, owner, is_class, stringifier_dict,
875861
name,
876862
cell=cell,
877863
owner=owner,
878-
globals=annotate_globals,
864+
globals=annotate.__globals__,
879865
is_class=is_class,
880866
stringifier_dict=stringifier_dict,
881867
)

Lib/test/test_annotationlib.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1615,7 +1615,7 @@ def __call__(self, format, /, __Format=Format,
16151615
Annotate(), Format.FORWARDREF
16161616
)
16171617

1618-
self.assertEqual(cm.exception.name, "__code__")
1618+
self.assertEqual(cm.exception.name, "__builtins__")
16191619
self.assertIsInstance(cm.exception.obj, Annotate)
16201620

16211621
def test_full_non_function_annotate(self):

0 commit comments

Comments
 (0)