1
- from typing import ContextManager
2
1
from contextlib import nullcontext
2
+ from typing import ContextManager
3
3
4
- import pytest
4
+ import cubed
5
+ import cubed .random
5
6
import hypothesis .strategies as st
6
- from hypothesis import note
7
7
import numpy as np
8
8
import numpy .testing as npt
9
+ import pytest
10
+ from hypothesis import note
9
11
10
12
from xarray_array_testing .base import DuckArrayTestMixin
11
13
from xarray_array_testing .creation import CreationTests
12
14
from xarray_array_testing .reduction import ReductionTests
13
15
14
- import cubed
15
- import cubed .random
16
-
17
16
18
17
def cubed_random_array (shape : tuple [int ], dtype : np .dtype ) -> cubed .Array :
19
18
"""
20
19
Generates a random cubed array
21
-
20
+
22
21
Supports integer and float dtypes.
23
22
"""
24
23
# TODO hypothesis doesn't like us using random inside strategies
25
24
rng = np .random .default_rng ()
26
-
25
+
27
26
if np .issubdtype (dtype , np .integer ):
28
27
arr = rng .integers (low = 0 , high = + 3 , size = shape , dtype = dtype )
29
28
return cubed .from_array (arr )
@@ -34,7 +33,9 @@ def cubed_random_array(shape: tuple[int], dtype: np.dtype) -> cubed.Array:
34
33
35
34
36
35
def random_cubed_arrays_fn (
37
- * , shape : tuple [int , ...], dtype : np .dtype ,
36
+ * ,
37
+ shape : tuple [int , ...],
38
+ dtype : np .dtype ,
38
39
) -> st .SearchStrategy [cubed .Array ]:
39
40
return st .builds (cubed_random_array , shape = st .just (shape ), dtype = st .just (dtype ))
40
41
@@ -57,21 +58,26 @@ def assert_equal(a: cubed.Array, b: cubed.Array):
57
58
npt .assert_equal (a .compute (), b .compute ())
58
59
59
60
60
-
61
61
class TestCreationCubed (CreationTests , CubedTestMixin ):
62
62
pass
63
63
64
64
65
65
class TestReductionCubed (ReductionTests , CubedTestMixin ):
66
66
@staticmethod
67
67
def expected_errors (op , ** parameters ) -> ContextManager :
68
- var = parameters .get (' variable' )
68
+ var = parameters .get (" variable" )
69
69
70
70
note (f"op = { op } " )
71
71
note (f"dtype = { var .dtype } " )
72
72
note (f"is_integer = { cubed .array_api .isdtype (var .dtype , 'integral' )} " )
73
73
74
- if op == 'mean' and cubed .array_api .isdtype (var .dtype , "integral" ) or var .dtype == np .dtype ('float16' ):
75
- return pytest .raises (TypeError , match = 'Only real floating-point dtypes are allowed in mean' )
74
+ if (
75
+ op == "mean"
76
+ and cubed .array_api .isdtype (var .dtype , "integral" )
77
+ or var .dtype == np .dtype ("float16" )
78
+ ):
79
+ return pytest .raises (
80
+ TypeError , match = "Only real floating-point dtypes are allowed in mean"
81
+ )
76
82
else :
77
83
return nullcontext ()
0 commit comments