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

Commit 1700c4c

Browse files
committed
fix comments
1 parent 92a8a8c commit 1700c4c

File tree

2 files changed

+29
-29
lines changed

2 files changed

+29
-29
lines changed

numba_typing/tests/test_type_annotations.py

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,37 +9,42 @@ def test_get_func_annotations_exception(self):
99

1010
def foo(a: int, b, c: str = "string"):
1111
pass
12-
self.assertRaises(SyntaxError, type_annotations.get_func_annotations, foo)
12+
with self.assertRaises(SyntaxError) as raises:
13+
type_annotations.get_func_annotations(foo)
14+
self.assertIn('Not found annotation for parameter b', str(raises.exception))
1315

1416
def test_get_cls_annotations(self):
15-
class MyClass(object):
17+
class TestClass(object):
1618
x: int = 3
1719
y: str = "string"
1820

19-
def __init__(self, x: str, y: int):
21+
def __init__(self, x, y):
2022
self.x = x
2123
self.y = y
2224

23-
self.assertEqual(type_annotations.get_cls_annotations(MyClass), ({'x': int, 'y': str}, {}))
25+
result = type_annotations.get_cls_annotations(TestClass)
26+
expected = ({'x': [int], 'y': [str]}, {})
27+
self.assertEqual(result, expected)
2428

2529
def test_get_func_annotations(self):
30+
2631
def func_one(a: int, b: Union[int, float], c: str):
2732
pass
28-
with self.subTest("annotations"):
29-
self.assertEqual(type_annotations.get_func_annotations(func_one),
30-
({'a': int, 'b': [int, float], 'c': str}, {}))
3133

3234
def func_two(a: int = 2, b: str = "string", c: List[int] = [1, 2, 3]):
3335
pass
34-
with self.subTest("annotations and all default values"):
35-
self.assertEqual(type_annotations.get_func_annotations(func_two),
36-
({'a': int, 'b': str, 'c': List[int]}, {'a': 2, 'b': 'string', 'c': [1, 2, 3]}))
3736

3837
def func_three(a: Dict[int, str], b: str = "string", c: int = 1):
3938
pass
40-
with self.subTest("annotations and not all default values"):
41-
self.assertEqual(type_annotations.get_func_annotations(func_three),
42-
({'a': Dict[int, str], 'b': str, 'c': int}, {'b': 'string', 'c': 1}))
39+
40+
expected_results = {
41+
func_one: ({'a': [int], 'b': [int, float], 'c': [str]}, {}),
42+
func_two: ({'a': [int], 'b': [str], 'c': [List[int]]}, {'a': 2, 'b': 'string', 'c': [1, 2, 3]}),
43+
func_three: ({'a': [Dict[int, str]], 'b': [str], 'c': [int]}, {'b': 'string', 'c': 1}),
44+
}
45+
for f, expected in expected_results.items():
46+
with self.subTest(func=f.__name__):
47+
self.assertEqual(type_annotations.get_func_annotations(f), expected)
4348

4449

4550
if __name__ == '__main__':

numba_typing/type_annotations.py

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,8 @@ def get_func_annotations(func):
1111
for name, param in sig.parameters.items():
1212
if param.annotation == sig.empty:
1313
raise SyntaxError(f'Not found annotation for parameter {name}')
14-
annot = param.annotation
15-
if get_args_union(annot):
16-
annotations[name] = get_args_union(annot)
17-
else:
18-
annotations[name] = annot
14+
15+
annotations[name] = get_annotation_types(param.annotation)
1916
if param.default != sig.empty:
2017
defaults[name] = param.default
2118

@@ -26,18 +23,16 @@ def get_cls_annotations(cls):
2623
"""Get annotations of class attributes."""
2724
annotations = get_type_hints(cls)
2825
for x in annotations:
29-
if get_args_union(annotations[x]):
30-
annotations[x] = get_args_union(annotations[x])
26+
annotations[x] = get_annotation_types(annotations[x])
3127
return annotations, {}
3228

3329

34-
def get_args_union(annot):
30+
def get_annotation_types(annotation):
31+
"""Get types of passed annotation."""
3532
try:
36-
annot.__origin__
37-
except:
38-
return None
39-
else:
40-
if annot.__origin__ is Union:
41-
return list(annot.__args__)
42-
else:
43-
return None
33+
if annotation.__origin__ is Union:
34+
return list(annotation.__args__)
35+
except AttributeError:
36+
pass
37+
38+
return [annotation, ]

0 commit comments

Comments
 (0)