@@ -46,7 +46,7 @@ def func_three(a: Dict[int, str], b: str = "string", c: int = 1):
4646 with self .subTest (func = f .__name__ ):
4747 self .assertEqual (type_annotations .get_func_annotations (f ), expected )
4848
49- def test_product_annotations (self ):
49+ ''' def test_product_annotations(self):
5050
5151 S = TypeVar('S', float, str)
5252 annotations = ({'a': [int], 'b': [int, float], 'c': [S]}, {})
@@ -55,6 +55,132 @@ def test_product_annotations(self):
5555 [{'a': int, 'b': int, 'c': str}, {}],
5656 [{'a': int, 'b': float, 'c': float}, {}],
5757 [{'a': int, 'b': float, 'c': str}, {}]]
58+ self.assertEqual(result, expected)'''
59+
60+ def test_convert_to_sig_list (self ):
61+ T = TypeVar ('T' , int , str )
62+ S = TypeVar ('S' , float , str )
63+ annotations = [{'a' : [int ], 'b' : [int , float ], 'c' : [S ]},
64+ {'a' : [int ], 'b' : [T ], 'c' : [S ]},
65+ {'a' : [int , str ], 'b' : [int , float ], 'c' : [S ]}]
66+
67+ expected = [[{'a' : int , 'b' : int , 'c' : S },
68+ {'a' : int , 'b' : float , 'c' : S }],
69+ [{'a' : int , 'b' : T , 'c' : S }],
70+ [{'a' : int , 'b' : int , 'c' : S },
71+ {'a' : int , 'b' : float , 'c' : S },
72+ {'a' : str , 'b' : int , 'c' : S },
73+ {'a' : str , 'b' : float , 'c' : S }]]
74+
75+ for i in range (len (annotations )):
76+ with self .subTest (annotations = i ):
77+ self .assertEqual (type_annotations .convert_to_sig_list (annotations [i ]), expected [i ])
78+
79+ def test_get_typevars (self ):
80+ T = TypeVar ('T' , int , str )
81+ S = TypeVar ('S' , float , str )
82+ types = [List [T ], Dict [T , S ], int , T , List [List [T ]]]
83+
84+ expected = [{T }, {T , S }, set (), {T }, {T }]
85+
86+ for i in range (len (types )):
87+ with self .subTest (types = i ):
88+ self .assertEqual (type_annotations .get_typevars (types [i ]), expected [i ])
89+
90+ def test_add_vals_to_signature (self ):
91+ signature = [{'a' : Dict [float , int ], 'b' : int },
92+ {'a' : Dict [str , int ], 'b' : int },
93+ {'a' : Dict [float , str ], 'b' : int },
94+ {'a' : Dict [str , str ], 'b' : int }]
95+ vals = {'a' : {'name' : 3 }, 'b' : 3 }
96+
97+ result = type_annotations .add_vals_to_signature (signature , vals )
98+ expected = [[{'a' : Dict [float , int ], 'b' : int }, {'a' : {'name' : 3 }, 'b' : 3 }],
99+ [{'a' : Dict [str , int ], 'b' : int }, {'a' : {'name' : 3 }, 'b' : 3 }],
100+ [{'a' : Dict [float , str ], 'b' : int }, {'a' : {'name' : 3 }, 'b' : 3 }],
101+ [{'a' : Dict [str , str ], 'b' : int }, {'a' : {'name' : 3 }, 'b' : 3 }]]
102+ self .assertEqual (result , expected )
103+
104+ def test_exist_typevar (self ):
105+ T = TypeVar ('T' , float , str )
106+ S = TypeVar ('S' , int , str )
107+ types = [List [List [T ]], Dict [T , S ], int , T , S ]
108+ expected = [{True }, {False , True }, {False }, {True }, {False }]
109+
110+ for i in range (len (types )):
111+ with self .subTest (types = i ):
112+ self .assertEqual (type_annotations .exist_typevar (types [i ], T ), expected [i ])
113+
114+ def test_replace_typevar (self ):
115+ T = TypeVar ('T' , int , str )
116+ S = TypeVar ('S' , float , str )
117+
118+ types = [List [List [T ]], Dict [T , S ], T ]
119+ expected = [List [List [int ]], Dict [int , S ], int ]
120+
121+ for i in range (len (types )):
122+ with self .subTest (types = i ):
123+ self .assertEqual (type_annotations .replace_typevar (types [i ], T , int ), expected [i ])
124+
125+ def test_get_internal_typevars (self ):
126+
127+ T = TypeVar ('T' , int , str )
128+ S = TypeVar ('S' , float , bool )
129+ signature = {'a' : T , 'b' : Dict [T , S ]}
130+ expected = [{'a' : int , 'b' : Dict [int , float ]},
131+ {'a' : int , 'b' : Dict [int , bool ]},
132+ {'a' : str , 'b' : Dict [str , float ]},
133+ {'a' : str , 'b' : Dict [str , bool ]}]
134+
135+ result = type_annotations .get_internal_typevars (signature )
136+ self .assertEqual (result , expected )
137+
138+ def test_update_sig (self ):
139+ T = TypeVar ('T' , int , str )
140+ S = TypeVar ('S' , float , bool )
141+
142+ sig = {'a' : T , 'b' : Dict [T , S ]}
143+ expected = [{'a' : T , 'b' : Dict [T , float ]},
144+ {'a' : T , 'b' : Dict [T , bool ]}]
145+ result = type_annotations .update_sig (sig , S )
146+
147+ self .assertEqual (result , expected )
148+
149+ def test_expand_typevars (self ):
150+ T = TypeVar ('T' , int , str )
151+ S = TypeVar ('S' , float , bool )
152+
153+ sig = {'a' : T , 'b' : Dict [T , S ], 'c' : int }
154+ unique_typevars = {T , S }
155+ expected = [{'a' : int , 'b' : Dict [int , float ], 'c' : int },
156+ {'a' : int , 'b' : Dict [int , bool ], 'c' : int },
157+ {'a' : str , 'b' : Dict [str , float ], 'c' : int },
158+ {'a' : str , 'b' : Dict [str , bool ], 'c' : int }]
159+
160+ result = type_annotations .expand_typevars (sig , unique_typevars )
161+ self .assertEqual (result , expected )
162+
163+ def test_product_annotations (self ):
164+
165+ T = TypeVar ('T' , int , str )
166+ S = TypeVar ('S' , float , bool )
167+
168+ annotations = ({'a' : [T ], 'b' : [Dict [T , S ]],
169+ 'c' : [T , bool ], 'd' : [int ]}, {'d' : 3 })
170+
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 }]]
179+
180+
181+ result = type_annotations .product_annotations (annotations )
182+ #print(result)
183+
58184 self .assertEqual (result , expected )
59185
60186
0 commit comments