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 )
@@ -324,7 +336,10 @@ def test_name_lookup_without_eval(self):
324336 # namespaces without going through eval()
325337 self .assertIs (ForwardRef ("int" ).evaluate (), int )
326338 self .assertIs (ForwardRef ("int" ).evaluate (locals = {"int" : str }), str )
327- self .assertIs (ForwardRef ("int" ).evaluate (locals = {"int" : float }, globals = {"int" : str }), float )
339+ self .assertIs (
340+ ForwardRef ("int" ).evaluate (locals = {"int" : float }, globals = {"int" : str }),
341+ float ,
342+ )
328343 self .assertIs (ForwardRef ("int" ).evaluate (globals = {"int" : str }), str )
329344 with support .swap_attr (builtins , "int" , dict ):
330345 self .assertIs (ForwardRef ("int" ).evaluate (), dict )
@@ -788,9 +803,8 @@ def __annotations__(self):
788803 annotationlib .get_annotations (ha , format = Format .FORWARDREF ), {"x" : int }
789804 )
790805
791- # TODO(gh-124412): This should return {'x': 'int'} instead.
792806 self .assertEqual (
793- annotationlib .get_annotations (ha , format = Format .SOURCE ), {"x" : int }
807+ annotationlib .get_annotations (ha , format = Format .SOURCE ), {"x" : " int" }
794808 )
795809
796810 def test_raising_annotations_on_custom_object (self ):
@@ -1078,6 +1092,29 @@ class C:
10781092 self .assertEqual (get_annotate_function (C )(Format .VALUE ), {"a" : int })
10791093
10801094
1095+ class TestToSource (unittest .TestCase ):
1096+ def test_value_to_source (self ):
1097+ self .assertEqual (value_to_source (int ), "int" )
1098+ self .assertEqual (value_to_source (MyClass ), "test.test_annotationlib.MyClass" )
1099+ self .assertEqual (value_to_source (len ), "len" )
1100+ self .assertEqual (value_to_source (value_to_source ), "value_to_source" )
1101+ self .assertEqual (value_to_source (times_three ), "times_three" )
1102+ self .assertEqual (value_to_source (...), "..." )
1103+ self .assertEqual (value_to_source (None ), "None" )
1104+ self .assertEqual (value_to_source (1 ), "1" )
1105+ self .assertEqual (value_to_source ("1" ), "'1'" )
1106+ self .assertEqual (value_to_source (Format .VALUE ), repr (Format .VALUE ))
1107+ self .assertEqual (value_to_source (MyClass ()), "my repr" )
1108+
1109+ def test_annotations_to_source (self ):
1110+ self .assertEqual (annotations_to_source ({}), {})
1111+ self .assertEqual (annotations_to_source ({"x" : int }), {"x" : "int" })
1112+ self .assertEqual (annotations_to_source ({"x" : "int" }), {"x" : "int" })
1113+ self .assertEqual (
1114+ annotations_to_source ({"x" : int , "y" : str }), {"x" : "int" , "y" : "str" }
1115+ )
1116+
1117+
10811118class TestAnnotationLib (unittest .TestCase ):
10821119 def test__all__ (self ):
10831120 support .check__all__ (self , annotationlib )
0 commit comments