1
1
import unittest
2
2
import type_annotations
3
3
from typing import Union , Dict , List , TypeVar
4
+ from collections import namedtuple
4
5
5
6
6
7
def check_equal (result , expected ):
@@ -12,6 +13,21 @@ def check_equal(result, expected):
12
13
return True
13
14
14
15
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
+
15
31
class TestTypeAnnotations (unittest .TestCase ):
16
32
17
33
def test_get_func_annotations_exceptions (self ):
@@ -92,22 +108,17 @@ def test_add_vals_to_signature(self):
92
108
{'a' : Dict [str , str ], 'b' : int }]
93
109
vals = {'a' : {'name' : 3 }, 'b' : 3 }
94
110
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' ])
101
112
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 })
107
118
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 )
111
122
112
123
def test_replace_typevar (self ):
113
124
T = TypeVar ('T' , int , str )
@@ -168,18 +179,21 @@ def test_product_annotations(self):
168
179
annotations = ({'a' : [T ], 'b' : [Dict [T , S ]],
169
180
'c' : [T , bool ], 'd' : [int ]}, {'d' : 3 })
170
181
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 })
179
193
180
194
result = type_annotations .product_annotations (annotations )
181
195
182
- self .assertTrue (check_equal (result , expected ))
196
+ self .assertTrue (check_equal_annotations (result , expected ))
183
197
184
198
185
199
if __name__ == '__main__' :
0 commit comments