Skip to content

Commit d2f5110

Browse files
authored
Merge pull request #202 from Rust-Data-Science/wip-zero-div
Wip zero div
2 parents 0ec862d + 16473f5 commit d2f5110

File tree

14 files changed

+260
-43
lines changed

14 files changed

+260
-43
lines changed

tests/mypy.ini

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[mypy]
2+
warn_return_any = True
3+
warn_unused_configs = True
4+
disallow_untyped_defs = True
5+
ignore_missing_imports = True
6+
follow_imports = silent

tests/test_base.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@
153153
154154
('count_na', 'bool', [True, False, True], 0),
155155
('count_na', 'int', [1, 0, 1], 0),
156+
('count_na', 'float', [1.0, 0.0, 1.0], 0),
156157
('count_na', 'string', ['foo', 'bar', 'foo'], 0),
157158
('count_na', 'string', ['foo', None, 'foo'], 1),
158159
@@ -161,6 +162,12 @@
161162
('counter', 'string', ['foo', 'bar', 'foo'], {'foo': 2, 'bar': 1}),
162163
('counter', 'string', ['foo', None, 'foo'], {'foo': 2, None: 1}),
163164
165+
('has_na', 'bool', [True, False, True], False),
166+
('has_na', 'int', [1, 0, 1], False),
167+
('has_na', 'float', [1.0, 0.0, 1.0], False),
168+
('has_na', 'string', ['foo', 'bar', 'foo'], False),
169+
('has_na', 'string', ['foo', None, 'foo'], True),
170+
164171
('mean', 'float', [1.0, 2.0, 3.0, 4.0, 5.0], 3.0),
165172
('mean', 'int', [1, 2, 3, 4, 5], 3.0),
166173
('mean', 'bool', [True, False, True, False], 0.5),

tests/test_exceptions.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
_ARR1._values = [0, 1, 2] # type: ignore
88
_ARR2 = ul.from_seq([], dtype='int')
99
_ARR3 = ul.from_seq([None, None], dtype='int')
10+
_ARR4 = ul.from_seq(range(3), dtype='int')
1011

1112

1213
class _Foo:
@@ -58,6 +59,18 @@ class _Foo:
5859
ValueError
5960
),
6061
62+
(
63+
_ARR4.div,
64+
{"other": ul.repeat(0, 3), "zero_div": False},
65+
ValueError
66+
),
67+
68+
(
69+
_ARR4.div_scala,
70+
{"elem": 0, "zero_div": False},
71+
ValueError
72+
),
73+
6174
(
6275
ul.from_seq,
6376
{"obj": [1, 2], "dtype": "foo"},

tests/test_misc.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Any
1+
from typing import Any, Sequence, Type
22

33
import pytest
44
from ulist.utils import (MAX_DTYPE_LEN, MAX_ITEM_LEN, Benchmarker,
@@ -72,7 +72,7 @@ def test_benchmark_score() -> None:
7272
bench.display(False)
7373

7474

75-
def _test_bench_marker(bench_marker):
75+
def _test_bench_marker(bench_marker: Type[Benchmarker]) -> None:
7676
bench = bench_marker(debug=True)
7777
bench.n_runs = (1, 1, 1, 1, 1)
7878
bench.run()
@@ -88,10 +88,10 @@ def cases(self) -> list:
8888
([0], 1),
8989
]
9090

91-
def ulist_fn(self, args) -> None:
91+
def ulist_fn(self, args: Sequence[Any]) -> None:
9292
pass
9393

94-
def other_fn(self, args) -> None:
94+
def other_fn(self, args: Sequence[Any]) -> None:
9595
pass
9696

9797

@@ -105,10 +105,10 @@ def cases(self) -> list:
105105
([0.0],),
106106
]
107107

108-
def ulist_fn(self, args) -> None:
108+
def ulist_fn(self, args: Sequence[Any]) -> None:
109109
pass
110110

111-
def other_fn(self, args) -> None:
111+
def other_fn(self, args: Sequence[Any]) -> None:
112112
pass
113113

114114

@@ -122,10 +122,10 @@ def cases(self) -> list:
122122
([True],),
123123
]
124124

125-
def ulist_fn(self, args) -> None:
125+
def ulist_fn(self, args: Sequence[Any]) -> None:
126126
pass
127127

128-
def other_fn(self, args) -> None:
128+
def other_fn(self, args: Sequence[Any]) -> None:
129129
pass
130130

131131

@@ -139,10 +139,10 @@ def cases(self) -> list:
139139
(['a'],),
140140
]
141141

142-
def ulist_fn(self, args) -> None:
142+
def ulist_fn(self, args: Sequence[Any]) -> None:
143143
pass
144144

145-
def other_fn(self, args) -> None:
145+
def other_fn(self, args: Sequence[Any]) -> None:
146146
pass
147147

148148

@@ -155,5 +155,5 @@ def other_fn(self, args) -> None:
155155
_String,
156156
],
157157
)
158-
def test_bench_marker(test_class: Benchmarker) -> None:
158+
def test_bench_marker(test_class: Type[Benchmarker]) -> None:
159159
_test_bench_marker(test_class)

tests/test_numerical.py

Lines changed: 140 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,19 @@
4949
('argmin', 'float', [None, 0.0, 1.0, -1.0], 3),
5050
('argmin', 'float', [None, -2.0, None, -1.0], 1),
5151
52+
('has_zero', 'float', [0.0, 0.0], True),
53+
('has_zero', 'float', [0.0, 1.0], True),
54+
('has_zero', 'float', [0.0, 1.0], True),
55+
('has_zero', 'float', [1.0, 1.0], False),
56+
('has_zero', 'float', [None, 1.0], False),
57+
('has_zero', 'float', [None, 0.0], True),
58+
('has_zero', 'int', [0, 0], True),
59+
('has_zero', 'int', [0, 1], True),
60+
('has_zero', 'int', [0, 1], True),
61+
('has_zero', 'int', [1, 1], False),
62+
('has_zero', 'int', [None, 1], False),
63+
('has_zero', 'int', [None, 0], True),
64+
5265
('max', 'float', [1.0, 2.0, 3.0, 4.0, 5.0], 5.0),
5366
('max', 'float', [5.0, 1.0, 2.0, 3.0, 4.0], 5.0),
5467
('max', 'float', [1.0, 2.0, 5.0, 3.0, 4.0], 5.0),
@@ -171,7 +184,7 @@ def test_statistics_methods(
171184
'float',
172185
[1.0, 2.0, 3.0, 4.0, 5.0],
173186
[1.0, 1.0, 1.0, 1.0, 1.0],
174-
{'other': [1, 2, 3, 4, 5]},
187+
{'other': [1, 2, 3, 4, 5], },
175188
),
176189
(
177190
'div',
@@ -208,6 +221,69 @@ def test_statistics_methods(
208221
[1.0, None, 1.0, None, 1.0],
209222
{'other': [1, 2, 3, None, 5]},
210223
),
224+
(
225+
'div',
226+
'int',
227+
[1, 2, 3, 4, 5],
228+
[1.0, 1.0, 1.0, 1.0, 1.0],
229+
{'other': [1, 2, 3, 4, 5], 'zero_div': False},
230+
),
231+
(
232+
'div',
233+
'int',
234+
[1, 2, 3, 4, 5],
235+
[1.0, 1.0, 1.0, 1.0, 1.0],
236+
{'other': [1, 2, 3, 4, 5], 'zero_div': True},
237+
),
238+
(
239+
'div',
240+
'int',
241+
[1, 2, 3, 4, 5],
242+
[float('inf'), 1.0, 1.0, 1.0, 1.0],
243+
{'other': [0, 2, 3, 4, 5], 'zero_div': True},
244+
),
245+
(
246+
'div',
247+
'int',
248+
[1, 2, 3, 4, 5],
249+
[1.0, float('inf'), 1.0, 1.0, 1.0],
250+
{'other': [1, 0, 3, 4, 5], 'zero_div': True},
251+
),
252+
(
253+
'div',
254+
'int',
255+
[1, 2, None, 4, 5],
256+
[1.0, 1.0, None, 1.0, 1.0],
257+
{'other': [1, 2, 0, 4, 5], 'zero_div': True},
258+
),
259+
(
260+
'div',
261+
'int',
262+
[1, 0, 3, 4, 5],
263+
[float('inf'), float('nan'), 1.0, 1.0, 1.0],
264+
{'other': [0, 0, 3, 4, 5], 'zero_div': True},
265+
),
266+
(
267+
'div',
268+
'int',
269+
[1, 2, None, 4, 5],
270+
[float('inf'), 1.0, None, 1.0, 1.0],
271+
{'other': [0, 2, 0, 4, 5], 'zero_div': True},
272+
),
273+
(
274+
'div',
275+
'int',
276+
[1, 0, None, 4, 5],
277+
[1.0, float('nan'), None, 1.0, 1.0],
278+
{'other': [1, 0, 0, 4, 5], 'zero_div': True},
279+
),
280+
(
281+
'div',
282+
'int',
283+
[1, 0, None, 4, 5],
284+
[float('inf'), float('nan'), None, 1.0, 1.0],
285+
{'other': [0, 0, 0, 4, 5], 'zero_div': True},
286+
),
211287
212288
(
213289
'div_scala',
@@ -230,6 +306,67 @@ def test_statistics_methods(
230306
[0.5, 1.0, None, 2.0, 2.5],
231307
{'elem': 2}
232308
),
309+
(
310+
'div_scala',
311+
'int',
312+
[1, 2, 3, 4, 5],
313+
[0.5, 1.0, 1.5, 2.0, 2.5],
314+
{'elem': 2, 'zero_div': False},
315+
),
316+
(
317+
'div_scala',
318+
'int',
319+
[1, 2, 3, 4, 5],
320+
[0.5, 1.0, 1.5, 2.0, 2.5],
321+
{'elem': 2, 'zero_div': True},
322+
),
323+
(
324+
'div_scala',
325+
'int',
326+
[1, 2, 3, 4, 5],
327+
[float('inf'), float('inf'), float(
328+
'inf'), float('inf'), float('inf')],
329+
{'elem': 0, 'zero_div': True},
330+
),
331+
(
332+
'div_scala',
333+
'int',
334+
[0, 0, 0, 0, 0],
335+
[float('nan'), float('nan'), float(
336+
'nan'), float('nan'), float('nan')],
337+
{'elem': 0, 'zero_div': True},
338+
),
339+
(
340+
'div_scala',
341+
'int',
342+
[None, None, None, None, None],
343+
[None, None, None, None, None],
344+
{'elem': 0, 'zero_div': True},
345+
),
346+
(
347+
'div_scala',
348+
'int',
349+
[0, 2, 3, 4, 5],
350+
[float('nan'), float('inf'), float(
351+
'inf'), float('inf'), float('inf')],
352+
{'elem': 0, 'zero_div': True},
353+
),
354+
(
355+
'div_scala',
356+
'int',
357+
[1, 2, None, 4, 5],
358+
[float('inf'), float('inf'), None,
359+
float('inf'), float('inf')],
360+
{'elem': 0, 'zero_div': True},
361+
),
362+
(
363+
'div_scala',
364+
'int',
365+
[0, 2, None, 4, 5],
366+
[float('nan'), float('inf'), None,
367+
float('inf'), float('inf')],
368+
{'elem': 0, 'zero_div': True},
369+
),
233370
234371
(
235372
"greater_than",
@@ -564,13 +701,9 @@ def test_arithmetic_methods(
564701
) -> None:
565702
arr = ul.from_seq(nums, dtype)
566703
if not test_method.endswith("_scala"):
567-
fn = getattr(arr, test_method)
568704
if isinstance(kwargs["other"], list):
569-
result = fn(ul.from_seq(kwargs["other"], dtype))
570-
else:
571-
result = fn(kwargs["other"])
572-
else:
573-
result = getattr(arr, test_method)(**kwargs)
705+
kwargs["other"] = ul.from_seq(kwargs["other"], dtype)
706+
result = getattr(arr, test_method)(**kwargs)
574707
check_test_result(dtype, test_method, result, expected_value)
575708

576709

0 commit comments

Comments
 (0)