@@ -9,37 +9,42 @@ def test_get_func_annotations_exception(self):
99
1010 def foo (a : int , b , c : str = "string" ):
1111 pass
12- self .assertRaises (SyntaxError , type_annotations .get_func_annotations , foo )
12+ with self .assertRaises (SyntaxError ) as raises :
13+ type_annotations .get_func_annotations (foo )
14+ self .assertIn ('Not found annotation for parameter b' , str (raises .exception ))
1315
1416 def test_get_cls_annotations (self ):
15- class MyClass (object ):
17+ class TestClass (object ):
1618 x : int = 3
1719 y : str = "string"
1820
19- def __init__ (self , x : str , y : int ):
21+ def __init__ (self , x , y ):
2022 self .x = x
2123 self .y = y
2224
23- self .assertEqual (type_annotations .get_cls_annotations (MyClass ), ({'x' : int , 'y' : str }, {}))
25+ result = type_annotations .get_cls_annotations (TestClass )
26+ expected = ({'x' : [int ], 'y' : [str ]}, {})
27+ self .assertEqual (result , expected )
2428
2529 def test_get_func_annotations (self ):
30+
2631 def func_one (a : int , b : Union [int , float ], c : str ):
2732 pass
28- with self .subTest ("annotations" ):
29- self .assertEqual (type_annotations .get_func_annotations (func_one ),
30- ({'a' : int , 'b' : [int , float ], 'c' : str }, {}))
3133
3234 def func_two (a : int = 2 , b : str = "string" , c : List [int ] = [1 , 2 , 3 ]):
3335 pass
34- with self .subTest ("annotations and all default values" ):
35- self .assertEqual (type_annotations .get_func_annotations (func_two ),
36- ({'a' : int , 'b' : str , 'c' : List [int ]}, {'a' : 2 , 'b' : 'string' , 'c' : [1 , 2 , 3 ]}))
3736
3837 def func_three (a : Dict [int , str ], b : str = "string" , c : int = 1 ):
3938 pass
40- with self .subTest ("annotations and not all default values" ):
41- self .assertEqual (type_annotations .get_func_annotations (func_three ),
42- ({'a' : Dict [int , str ], 'b' : str , 'c' : int }, {'b' : 'string' , 'c' : 1 }))
39+
40+ expected_results = {
41+ func_one : ({'a' : [int ], 'b' : [int , float ], 'c' : [str ]}, {}),
42+ func_two : ({'a' : [int ], 'b' : [str ], 'c' : [List [int ]]}, {'a' : 2 , 'b' : 'string' , 'c' : [1 , 2 , 3 ]}),
43+ func_three : ({'a' : [Dict [int , str ]], 'b' : [str ], 'c' : [int ]}, {'b' : 'string' , 'c' : 1 }),
44+ }
45+ for f , expected in expected_results .items ():
46+ with self .subTest (func = f .__name__ ):
47+ self .assertEqual (type_annotations .get_func_annotations (f ), expected )
4348
4449
4550if __name__ == '__main__' :
0 commit comments