77import itertools
88import pickle
99import unittest
10- from annotationlib import Format , ForwardRef , get_annotations , get_annotate_function
10+ from annotationlib import (
11+ Format ,
12+ ForwardRef ,
13+ get_annotations ,
14+ get_annotate_function ,
15+ annotations_to_source ,
16+ value_to_source ,
17+ )
1118from typing import Unpack
1219
1320from test import support
@@ -25,6 +32,11 @@ def wrapper(a, b):
2532 return wrapper
2633
2734
35+ class MyClass :
36+ def __repr__ (self ):
37+ return "my repr"
38+
39+
2840class TestFormat (unittest .TestCase ):
2941 def test_enum (self ):
3042 self .assertEqual (annotationlib .Format .VALUE .value , 1 )
@@ -327,7 +339,10 @@ def test_name_lookup_without_eval(self):
327339 # namespaces without going through eval()
328340 self .assertIs (ForwardRef ("int" ).evaluate (), int )
329341 self .assertIs (ForwardRef ("int" ).evaluate (locals = {"int" : str }), str )
330- self .assertIs (ForwardRef ("int" ).evaluate (locals = {"int" : float }, globals = {"int" : str }), float )
342+ self .assertIs (
343+ ForwardRef ("int" ).evaluate (locals = {"int" : float }, globals = {"int" : str }),
344+ float ,
345+ )
331346 self .assertIs (ForwardRef ("int" ).evaluate (globals = {"int" : str }), str )
332347 with support .swap_attr (builtins , "int" , dict ):
333348 self .assertIs (ForwardRef ("int" ).evaluate (), dict )
@@ -804,9 +819,8 @@ def __annotations__(self):
804819 annotationlib .get_annotations (ha , format = Format .FORWARDREF ), {"x" : int }
805820 )
806821
807- # TODO(gh-124412): This should return {'x': 'int'} instead.
808822 self .assertEqual (
809- annotationlib .get_annotations (ha , format = Format .SOURCE ), {"x" : int }
823+ annotationlib .get_annotations (ha , format = Format .SOURCE ), {"x" : " int" }
810824 )
811825
812826 def test_raising_annotations_on_custom_object (self ):
@@ -1094,6 +1108,29 @@ class C:
10941108 self .assertEqual (get_annotate_function (C )(Format .VALUE ), {"a" : int })
10951109
10961110
1111+ class TestToSource (unittest .TestCase ):
1112+ def test_value_to_source (self ):
1113+ self .assertEqual (value_to_source (int ), "int" )
1114+ self .assertEqual (value_to_source (MyClass ), "test.test_annotationlib.MyClass" )
1115+ self .assertEqual (value_to_source (len ), "len" )
1116+ self .assertEqual (value_to_source (value_to_source ), "value_to_source" )
1117+ self .assertEqual (value_to_source (times_three ), "times_three" )
1118+ self .assertEqual (value_to_source (...), "..." )
1119+ self .assertEqual (value_to_source (None ), "None" )
1120+ self .assertEqual (value_to_source (1 ), "1" )
1121+ self .assertEqual (value_to_source ("1" ), "'1'" )
1122+ self .assertEqual (value_to_source (Format .VALUE ), repr (Format .VALUE ))
1123+ self .assertEqual (value_to_source (MyClass ()), "my repr" )
1124+
1125+ def test_annotations_to_source (self ):
1126+ self .assertEqual (annotations_to_source ({}), {})
1127+ self .assertEqual (annotations_to_source ({"x" : int }), {"x" : "int" })
1128+ self .assertEqual (annotations_to_source ({"x" : "int" }), {"x" : "int" })
1129+ self .assertEqual (
1130+ annotations_to_source ({"x" : int , "y" : str }), {"x" : "int" , "y" : "str" }
1131+ )
1132+
1133+
10971134class TestAnnotationLib (unittest .TestCase ):
10981135 def test__all__ (self ):
10991136 support .check__all__ (self , annotationlib )
0 commit comments