11import unittest
22import type_annotations
33from typing import Union , Dict , List , TypeVar
4+ from collections import namedtuple
45
56
67def check_equal (result , expected ):
@@ -12,6 +13,21 @@ def check_equal(result, expected):
1213 return True
1314
1415
16+ def check_equal_annotations (result , expected ):
17+
18+ if len (result .parameters ) != len (expected .parameters ):
19+ return False
20+
21+ for sig in result .parameters :
22+ if sig not in expected .parameters :
23+ return False
24+
25+ if result .defaults != expected .defaults :
26+ return False
27+
28+ return True
29+
30+
1531class TestTypeAnnotations (unittest .TestCase ):
1632
1733 def test_get_func_annotations_exceptions (self ):
@@ -92,22 +108,17 @@ def test_add_vals_to_signature(self):
92108 {'a' : Dict [str , str ], 'b' : int }]
93109 vals = {'a' : {'name' : 3 }, 'b' : 3 }
94110
95- result = type_annotations .add_vals_to_signature (signature , vals )
96- expected = [[{'a' : Dict [float , int ], 'b' : int }, {'a' : {'name' : 3 }, 'b' : 3 }],
97- [{'a' : Dict [str , int ], 'b' : int }, {'a' : {'name' : 3 }, 'b' : 3 }],
98- [{'a' : Dict [float , str ], 'b' : int }, {'a' : {'name' : 3 }, 'b' : 3 }],
99- [{'a' : Dict [str , str ], 'b' : int }, {'a' : {'name' : 3 }, 'b' : 3 }]]
100- self .assertEqual (result , expected )
111+ Annotations = namedtuple ('Annotations' , ['parameters' , 'defaults' ])
101112
102- def test_exist_typevar ( self ):
103- T = TypeVar ( 'T' , float , str )
104- S = TypeVar ( 'S' , int , str )
105- types = [ List [ List [ T ]], Dict [T , S ], int , T , S ]
106- expected = [{ True }, { False , True }, { False }, { True }, { False }]
113+ expected = Annotations ( parameters = [{ 'a' : Dict [ float , int ], 'b' : int },
114+ { 'a' : Dict [ str , int ], 'b' : int },
115+ { 'a' : Dict [ float , str ], 'b' : int },
116+ { 'a' : Dict [str , str ], 'b' : int }],
117+ defaults = { 'a' : { 'name' : 3 }, 'b' : 3 })
107118
108- for i in range ( len ( types )):
109- with self . subTest ( types = i ):
110- self .assertEqual (type_annotations . exist_typevar ( types [ i ], T ), expected [ i ] )
119+ result = type_annotations . add_vals_to_signature ( signature , vals )
120+
121+ self .assertEqual (result , expected )
111122
112123 def test_replace_typevar (self ):
113124 T = TypeVar ('T' , int , str )
@@ -168,18 +179,21 @@ def test_product_annotations(self):
168179 annotations = ({'a' : [T ], 'b' : [Dict [T , S ]],
169180 'c' : [T , bool ], 'd' : [int ]}, {'d' : 3 })
170181
171- expected = [[{'a' : int , 'b' : Dict [int , float ], 'c' : int , 'd' : int }, {'d' : 3 }],
172- [{'a' : int , 'b' : Dict [int , bool ], 'c' : int , 'd' : int }, {'d' : 3 }],
173- [{'a' : str , 'b' : Dict [str , float ], 'c' : str , 'd' : int }, {'d' : 3 }],
174- [{'a' : str , 'b' : Dict [str , bool ], 'c' : str , 'd' : int }, {'d' : 3 }],
175- [{'a' : int , 'b' : Dict [int , float ], 'c' : bool , 'd' : int }, {'d' : 3 }],
176- [{'a' : int , 'b' : Dict [int , bool ], 'c' : bool , 'd' : int }, {'d' : 3 }],
177- [{'a' : str , 'b' : Dict [str , float ], 'c' : bool , 'd' : int }, {'d' : 3 }],
178- [{'a' : str , 'b' : Dict [str , bool ], 'c' : bool , 'd' : int }, {'d' : 3 }]]
182+ Annotations = namedtuple ('Annotations' , ['parameters' , 'defaults' ])
183+
184+ expected = Annotations (parameters = [{'a' : int , 'b' : Dict [int , float ], 'c' : int , 'd' : int },
185+ {'a' : str , 'b' : Dict [str , float ], 'c' : str , 'd' : int },
186+ {'a' : int , 'b' : Dict [int , bool ], 'c' : int , 'd' : int },
187+ {'a' : str , 'b' : Dict [str , bool ], 'c' : str , 'd' : int },
188+ {'a' : int , 'b' : Dict [int , float ], 'c' : bool , 'd' : int },
189+ {'a' : str , 'b' : Dict [str , float ], 'c' : bool , 'd' : int },
190+ {'a' : int , 'b' : Dict [int , bool ], 'c' : bool , 'd' : int },
191+ {'a' : str , 'b' : Dict [str , bool ], 'c' : bool , 'd' : int }],
192+ defaults = {'d' : 3 })
179193
180194 result = type_annotations .product_annotations (annotations )
181195
182- self .assertTrue (check_equal (result , expected ))
196+ self .assertTrue (check_equal_annotations (result , expected ))
183197
184198
185199if __name__ == '__main__' :
0 commit comments