8000 gh-119180: annotationlib: Fix __all__, formatting (#122365) · python/cpython@4534068 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4534068

Browse files
gh-119180: annotationlib: Fix __all__, formatting (#122365)
1 parent 016f4b5 commit 4534068

File tree

3 files changed

+68
-33
lines changed

3 files changed

+68
-33
lines changed

Lib/annotationlib.py

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,14 @@
66
import sys
77
import types
88

9-
__all__ = ["Format", "ForwardRef", "call_annotate_function", "get_annotations"]
9+
__all__ = [
10+
"Format",
11+
"ForwardRef",
12+
"call_annotate_function",
13+
"call_evaluate_function",
14+
"get_annotate_function",
15+
"get_annotations",
16+
]
1017

1118

1219
class Format(enum.IntEnum):
@@ -426,8 +433,7 @@ def call_evaluate_function(evaluate, format, *, owner=None):
426433
return call_annotate_function(evaluate, format, owner=owner, _is_evaluate=True)
427434

428435

429-
def call_annotate_function(annotate, format, *, owner=None,
430-
_is_evaluate=False):
436+
def call_annotate_function(annotate, format, *, owner=None, _is_evaluate=False):
431437
"""Call an __annotate__ function. __annotate__ functions are normally
432438
generated by the compiler to defer the evaluation of annotations. They
433439
can be called with any of the format arguments in the Format enum, but
@@ -473,8 +479,13 @@ def call_annotate_function(annotate, format, *, owner=None,
473479
closure = tuple(new_closure)
474480
else:
475481
closure = None
476-
func = types.FunctionType(annotate.__code__, globals, closure=closure,
477-
argdefs=annotate.__defaults__, kwdefaults=annotate.__kwdefaults__)
482+
func = types.FunctionType(
483+
annotate.__code__,
484+
globals,
485+
closure=closure,
486+
argdefs=annotate.__defaults__,
487+
kwdefaults=annotate.__kwdefaults__,
488+
)
478489
annos = func(Format.VALUE)
479490
if _is_evaluate:
480491
return annos if isinstance(annos, str) else repr(annos)
@@ -528,8 +539,13 @@ def call_annotate_function(annotate, format, *, owner=None,
528539
closure = tuple(new_closure)
529540
else:
530541
closure = None
531-
func = types.FunctionType(annotate.__code__, globals, closure=closure,
532-
argdefs=annotate.__defaults__, kwdefaults=annotate.__kwdefaults__)
542+
func = types.FunctionType(
543+
annotate.__code__,
544+
globals,
545+
closure=closure,
546+
argdefs=annotate.__defaults__,
547+
kwdefaults=annotate.__kwdefaults__,
548+
)
533549
result = func(Format.VALUE)
534550
for obj in globals.stringifiers:
535551
obj.__class__ = ForwardRef

Lib/inspect.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424
stack(), trace() - get info about frames on the stack or in a traceback
2525
2626
signature() - get a Signature object for the callable
27-
28-
get_annotations() - safely compute an object's annotations
2927
"""
3028

3129
# This module is in the public domain. No warranties.
@@ -142,7 +140,7 @@
142140

143141

144142
import abc
145-
from annotationlib import get_annotations
143+
from annotationlib import get_annotations # re-exported
146144
import ast
147145
import dis
148146
import collections.abc

Lib/test/test_annotationlib.py

Lines changed: 44 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from annotationlib import Format, ForwardRef, get_annotations, get_annotate_function
99
from typing import Unpack
1010

11+
from test import support
1112
from test.test_inspect import inspect_stock_annotations
1213
from test.test_inspect import inspect_stringized_annotations
1314
from test.test_inspect import inspect_stringized_annotations_2
@@ -327,7 +328,9 @@ class C1(metaclass=NoDict):
327328
)
328329
self.assertEqual(annotationlib.get_annotations(NoDict), {"b": str})
329330
self.assertEqual(
330-
annotationlib.get_annotations(NoDict, format=annotationlib.Format.FORWARDREF),
331+
annotationlib.get_annotations(
332+
NoDict, format=annotationlib.Format.FORWARDREF
333+
),
331334
{"b": str},
332335
)
333336
self.assertEqual(
@@ -715,12 +718,13 @@ def test_pep695_generic_class_with_future_annotations_and_local_shadowing(self):
715718
)
716719
self.assertEqual(B_annotations, {"x": int, "y": str, "z": bytes})
717720

718-
def test_pep695_generic_class_with_future_annotations_name_clash_with_global_vars(self):
721+
def test_pep695_generic_class_with_future_annotations_name_clash_with_global_vars(
722+
self,
723+
):
719724
ann_module695 = inspect_stringized_annotations_pep695
720725
C_annotations = annotationlib.get_annotations(ann_module695.C, eval_str=True)
721726
self.assertEqual(
722-
set(C_annotations.values()),
723-
set(ann_module695.C.__type_params__)
727+
set(C_annotations.values()), set(ann_module695.C.__type_params__)
724728
)
725729

726730
def test_pep_695_generic_function_with_future_annotations(self):
@@ -737,17 +741,19 @@ def test_pep_695_generic_function_with_future_annotations(self):
737741
self.assertIs(generic_func_annotations["z"].__origin__, func_t_params[2])
738742
self.assertIs(generic_func_annotations["zz"].__origin__, func_t_params[2])
739743

740-
def test_pep_695_generic_function_with_future_annotations_name_clash_with_global_vars(self):
744+
def test_pep_695_generic_function_with_future_annotations_name_clash_with_global_vars(
745+
self,
746+
):
741747
self.assertEqual(
742748
set(
743749
annotationlib.get_annotations(
744750
inspect_stringized_annotations_pep695.generic_function_2,
745-
eval_str=True
751+
eval_str=True,
746752
).values()
747753
),
748754
set(
749755
inspect_stringized_annotations_pep695.generic_function_2.__type_params__
750-
)
756+
),
751757
)
752758

753759
def test_pep_695_generic_method_with_future_annotations(self):
@@ -761,23 +767,27 @@ def test_pep_695_generic_method_with_future_annotations(self):
761767
}
762768
self.assertEqual(
763769
generic_method_annotations,
764-
{"x": params["Foo"], "y": params["Bar"], "return": None}
770+
{"x": params["Foo"], "y": params["Bar"], "return": None},
765771
)
766772

767-
def test_pep_695_generic_method_with_future_annotations_name_clash_with_global_vars(self):
773+
def test_pep_695_generic_method_with_future_annotations_name_clash_with_global_vars(
774+
self,
775+
):
768776
self.assertEqual(
769777
set(
770778
annotationlib.get_annotations(
771779
inspect_stringized_annotations_pep695.D.generic_method_2,
772-
eval_str=True
780+
eval_str=True,
773781
).values()
774782
),
775783
set(
776784
inspect_stringized_annotations_pep695.D.generic_method_2.__type_params__
777-
)
785+
),
778786
)
779787

780-
def test_pep_695_generic_method_with_future_annotations_name_clash_with_global_and_local_vars(self):
788+
def test_pep_695_generic_method_with_future_annotations_name_clash_with_global_and_local_vars(
789+
self,
790+
):
781791
self.assertEqual(
782792
annotationlib.get_annotations(
783793
inspect_stringized_annotations_pep695.E, eval_str=True
@@ -789,20 +799,20 @@ def test_pep_695_generics_with_future_annotations_nested_in_function(self):
789799
results = inspect_stringized_annotations_pep695.nested()
790800

791801
self.assertEqual(
792-
set(results.F_annotations.values()),
793-
set(results.F.__type_params__)
802+
set(results.F_annotations.values()), set(results.F.__type_params__)
794803
)
795804
self.assertEqual(
796805
set(results.F_meth_annotations.values()),
797-
set(results.F.generic_method.__type_params__)
806+
set(results.F.generic_method.__type_params__),
798807
)
799808
self.assertNotEqual(
800-
set(results.F_meth_annotations.values()),
801-
set(results.F.__type_params__)
809+
set(results.F_meth_annotations.values()), set(results.F.__type_params__)
802810
)
803811
self.assertEqual(
804-
set(results.F_meth_annotations.values()).intersection(results.F.__type_params__),
805-
set()
812+
set(results.F_meth_annotations.values()).intersection(
813+
results.F.__type_params__
814+
),
815+
set(),
806816
)
807817

808818
self.assertEqual(results.G_annotations, {"x": str})
@@ -823,7 +833,9 @@ def evaluate(format, exc=NotImplementedError):
823833
with self.assertRaises(NameError):
824834
annotationlib.call_evaluate_function(evaluate, annotationlib.Format.VALUE)
825835
self.assertEqual(
826-
annotationlib.call_evaluate_function(evaluate, annotationlib.Format.FORWARDREF),
836+
annotationlib.call_evaluate_function(
837+
evaluate, annotationlib.Format.FORWARDREF
838+
),
827839
annotationlib.ForwardRef("undefined"),
828840
)
829841
self.assertEqual(
@@ -853,12 +865,14 @@ class Y(metaclass=Meta):
853865
self.assertEqual(get_annotate_function(Y)(Format.VALUE), {"b": float})
854866

855867
def test_unannotated_meta(self):
856-
class Meta(type): pass
868+
class Meta(type):
869+
pass
857870

858871
class X(metaclass=Meta):
859872
a: str
860873

861-
class Y(X): pass
874+
class Y(X):
875+
pass
862876

863877
self.assertEqual(get_annotations(Meta), {})
864878
self.assertIs(get_annotate_function(Meta), None)
@@ -907,6 +921,13 @@ class D(metaclass=Meta):
907921
self.assertEqual(get_annotations(c), c.expected_annotations)
908922
annotate_func = get_annotate_function(c)
909923
if c.expected_annotations:
910-
self.assertEqual(annotate_func(Format.VALUE), c.expected_annotations)
924+
self.assertEqual(
925+
annotate_func(Format.VALUE), c.expected_annotations
926+
)
911927
else:
912928
self.assertIs(annotate_func, None)
929+
930+
931+
class TestAnnotationLib(unittest.TestCase):
932+
def test__all__(self):
933+
support.check__all__(self, annotationlib)

0 commit comments

Comments
 (0)
155E
0