Skip to content

Commit b2db0dc

Browse files
committed
move the configuration hooks to a abstract base class
1 parent 4b50664 commit b2db0dc

File tree

5 files changed

+45
-26
lines changed

5 files changed

+45
-26
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ ignore = [
5151
"E501", # E501: line too long - let black worry about that
5252
"E731", # E731: do not assign a lambda expression, use a def
5353
]
54-
fixable = ["I"]
54+
fixable = ["I", "TID"]
5555
extend-safe-fixes = [
5656
"TID252", # absolute imports
5757
]

xarray_array_testing/base.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import abc
2+
from abc import ABC
3+
from types import ModuleType
4+
5+
import numpy.testing as npt
6+
7+
8+
class DuckArrayTestMixin(ABC):
9+
@property
10+
@abc.abstractmethod
11+
def xp() -> ModuleType:
12+
pass
13+
14+
@property
15+
@abc.abstractmethod
16+
def array_type(self) -> type:
17+
pass
18+
19+
@staticmethod
20+
@abc.abstractmethod
21+
def array_strategy_fn(*, shape, dtype):
22+
raise NotImplementedError("has to be overridden")
23+
24+
@staticmethod
25+
def assert_equal(a, b):
26+
npt.assert_equal(a, b)

xarray_array_testing/creation.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,10 @@
22
import xarray.testing.strategies as xrst
33
from hypothesis import given
44

5+
from xarray_array_testing.base import DuckArrayTestMixin
56

6-
class CreationTests:
7-
array_type: type
8-
9-
@staticmethod
10-
def array_strategy_fn(*, shape, dtype):
11-
raise NotImplementedError
127

8+
class CreationTests(DuckArrayTestMixin):
139
@given(st.data())
1410
def test_create_variable(self, data):
1511
variable = data.draw(xrst.variables(array_strategy_fn=self.array_strategy_fn))

xarray_array_testing/reduction.py

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,13 @@
11
from contextlib import nullcontext
2-
from types import ModuleType
32

43
import hypothesis.strategies as st
5-
import numpy as np
64
import xarray.testing.strategies as xrst
75
from hypothesis import given
86

7+
from xarray_array_testing.base import DuckArrayTestMixin
98

10-
class ReductionTests:
11-
xp: ModuleType
12-
13-
@staticmethod
14-
def array_strategy_fn(*, shape, dtype):
15-
raise NotImplementedError
16-
17-
@staticmethod
18-
def assert_equal(a, b):
19-
np.testing.assert_allclose(a, b)
209

10+
class ReductionTests(DuckArrayTestMixin):
2111
@staticmethod
2212
def expected_errors(op, **parameters):
2313
return nullcontext()
Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import hypothesis.strategies as st
22
import numpy as np
33

4+
from xarray_array_testing.base import DuckArrayTestMixin
45
from xarray_array_testing.creation import CreationTests
56
from xarray_array_testing.reduction import ReductionTests
67

@@ -9,17 +10,23 @@ def create_numpy_array(*, shape, dtype):
910
return st.builds(np.ones, shape=st.just(shape), dtype=st.just(dtype))
1011

1112

12-
class TestCreationNumpy(CreationTests):
13-
array_type = np.ndarray
13+
class NumpyTestMixin(DuckArrayTestMixin):
14+
@property
15+
def xp(self):
16+
return np
17+
18+
@property
19+
def array_type(self):
20+
return np.ndarray
1421

1522
@staticmethod
1623
def array_strategy_fn(*, shape, dtype):
1724
return create_numpy_array(shape=shape, dtype=dtype)
1825

1926

20-
class TestReductionNumpy(ReductionTests):
21-
xp = np
27+
class TestCreationNumpy(CreationTests, NumpyTestMixin):
28+
pass
2229

23-
@staticmethod
24-
def array_strategy_fn(*, shape, dtype):
25-
return create_numpy_array(shape=shape, dtype=dtype)
30+
31+
class TestReductionNumpy(ReductionTests, NumpyTestMixin):
32+
pass

0 commit comments

Comments
 (0)