Skip to content
This repository was archived by the owner on Feb 2, 2024. It is now read-only.

Commit 718a114

Browse files
committed
add function and tests
1 parent 2728ef0 commit 718a114

File tree

2 files changed

+146
-10
lines changed

2 files changed

+146
-10
lines changed

numba_typing/tests/test_type_annotations.py

Lines changed: 127 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

numba_typing/type_annotations.py

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from inspect import signature
2-
from typing import get_type_hints, Union, TypeVar
2+
from typing import get_type_hints, Union, TypeVar,_GenericAlias
33
from itertools import product
44
from copy import deepcopy
55

@@ -40,17 +40,27 @@ def get_annotation_types(annotation):
4040
return [annotation, ]
4141

4242

43-
def product_annotatios(annotations):
43+
def product_annotations(annotations):
4444
'''Get all variants of annotations.'''
4545
types, vals = annotations
46-
list_of_sig = convert_to_sig_list(types)
47-
result_product = []
46+
list_of_sig = convert_to_sig_list(types)
47+
signature = []
4848
#unique_typevars = get_internal_typevars(list_of_sig)
4949

5050
for sig in list_of_sig:
51-
result_product.extend(get_internal_typevars(sig))
51+
signature.extend(get_internal_typevars(sig))
52+
53+
return add_vals_to_signature(signature, vals)
5254

53-
return result_product
55+
def add_vals_to_signature(signature, vals):
56+
'''Add default values ​​to all signatures'''
57+
result = []
58+
for sig in signature:
59+
annotation = []
60+
annotation.append(sig)
61+
annotation.append(vals)
62+
result.append(annotation)
63+
return result
5464

5565

5666
def convert_to_sig_list(types):
@@ -112,7 +122,7 @@ def update_sig(temp_sig, typevar):
112122
for constr_type in typevar.__constraints__:
113123
sig = {}
114124
for name, typ in temp_sig.items():
115-
if True in exsist_typevar(typ, typevar):
125+
if True in exist_typevar(typ, typevar):
116126
sig[name] = replace_typevar(typ, typevar, constr_type)
117127
else:
118128
sig[name] = typ
@@ -122,14 +132,14 @@ def update_sig(temp_sig, typevar):
122132
return result
123133

124134

125-
def exsist_typevar(typ, typevar):
135+
def exist_typevar(typ, typevar):
126136
'''Сheck if there is a typevar in type (container)'''
127137
if typ == typevar:
128138
return {True, }
129139
elif isinstance(typ, _GenericAlias):
130140
result = set()
131141
for arg in typ.__args__:
132-
result.update(exsist_typevar(arg, typevar))
142+
result.update(exist_typevar(arg, typevar))
133143
return result
134144

135145
return {False, }

0 commit comments

Comments
 (0)