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

Commit c6e67f6

Browse files
committed
fix comments
1 parent cc09ebb commit c6e67f6

File tree

2 files changed

+43
-45
lines changed

2 files changed

+43
-45
lines changed

numba_typing/tests/test_type_annotations.py

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import unittest
22
import type_annotations
33
from typing import Union, Dict, List, TypeVar
4+
from collections import namedtuple
45

56

67
def 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+
1531
class 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

185199
if __name__ == '__main__':

numba_typing/type_annotations.py

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from typing import get_type_hints, Union, TypeVar, _GenericAlias
33
from itertools import product
44
from copy import deepcopy
5+
from collections import namedtuple
56

67

78
def get_func_annotations(func):
@@ -45,7 +46,6 @@ def product_annotations(annotations):
4546
types, vals = annotations
4647
list_of_sig = convert_to_sig_list(types)
4748
signature = []
48-
#unique_typevars = get_internal_typevars(list_of_sig)
4949

5050
for sig in list_of_sig:
5151
signature.extend(get_internal_typevars(sig))
@@ -55,13 +55,10 @@ def product_annotations(annotations):
5555

5656
def add_vals_to_signature(signature, vals):
5757
'''Add default values ​​to all signatures'''
58-
result = []
59-
for sig in signature:
60-
annotation = []
61-
annotation.append(sig)
62-
annotation.append(vals)
63-
result.append(annotation)
64-
return result
58+
Annotations = namedtuple('Annotations', ['parameters', 'defaults'])
59+
annotations = Annotations(signature, vals)
60+
61+
return annotations
6562

6663

6764
def convert_to_sig_list(types):
@@ -123,7 +120,7 @@ def update_sig(temp_sig, typevar):
123120
for constr_type in typevar.__constraints__:
124121
sig = {}
125122
for name, typ in temp_sig.items():
126-
if True in exist_typevar(typ, typevar):
123+
if typevar in get_typevars(typ):
127124
sig[name] = replace_typevar(typ, typevar, constr_type)
128125
else:
129126
sig[name] = typ
@@ -133,19 +130,6 @@ def update_sig(temp_sig, typevar):
133130
return result
134131

135132

136-
def exist_typevar(typ, typevar):
137-
'''Сheck if there is a typevar in type (container)'''
138-
if typ == typevar:
139-
return {True, }
140-
elif isinstance(typ, _GenericAlias):
141-
result = set()
142-
for arg in typ.__args__:
143-
result.update(exist_typevar(arg, typevar))
144-
return result
145-
146-
return {False, }
147-
148-
149133
def replace_typevar(typ, typevar, final_typ):
150134
'''Replace typevar with type in container
151135
For example:

0 commit comments

Comments
 (0)