From 91002753eb1628b3b684c05696b0d3f61b107733 Mon Sep 17 00:00:00 2001 From: Justus Magin Date: Thu, 23 Oct 2025 23:04:06 +0200 Subject: [PATCH 1/3] convert the `array_type` property to a static method This will allow choosing the return type depending on the operation. --- xarray_array_testing/base.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/xarray_array_testing/base.py b/xarray_array_testing/base.py index 2d04dbd..945e7e3 100644 --- a/xarray_array_testing/base.py +++ b/xarray_array_testing/base.py @@ -12,9 +12,8 @@ class DuckArrayTestMixin(ABC): def xp() -> ModuleType: pass - @property - @abc.abstractmethod - def array_type(self) -> type[duckarray]: + @staticmethod + def array_type(op: str) -> type[duckarray]: pass @staticmethod From d1bafdd3c2578d91ee2debae4109efe7017adff6 Mon Sep 17 00:00:00 2001 From: Justus Magin Date: Thu, 23 Oct 2025 23:07:20 +0200 Subject: [PATCH 2/3] call `array_type` with `op` --- xarray_array_testing/creation.py | 2 +- xarray_array_testing/indexing.py | 8 ++++++-- xarray_array_testing/reduction.py | 8 ++++---- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/xarray_array_testing/creation.py b/xarray_array_testing/creation.py index 291e082..0d56b89 100644 --- a/xarray_array_testing/creation.py +++ b/xarray_array_testing/creation.py @@ -10,4 +10,4 @@ class CreationTests(DuckArrayTestMixin): def test_create_variable(self, data): variable = data.draw(xrst.variables(array_strategy_fn=self.array_strategy_fn)) - assert isinstance(variable.data, self.array_type) + assert isinstance(variable.data, self.array_type("__init__")) diff --git a/xarray_array_testing/indexing.py b/xarray_array_testing/indexing.py index 0dc0c09..6b3529b 100644 --- a/xarray_array_testing/indexing.py +++ b/xarray_array_testing/indexing.py @@ -95,7 +95,9 @@ def test_variable_isel_orthogonal(self, data): raw_indexers = {dim: idx.get(dim, slice(None)) for dim in variable.dims} expected = variable.data[*raw_indexers.values()] - assert isinstance(actual, self.array_type), f"wrong type: {type(actual)}" + assert isinstance( + actual, self.array_type("orthogonal_indexing") + ), f"wrong type: {type(actual)}" self.assert_equal(actual, expected) @given(st.data()) @@ -109,5 +111,7 @@ def test_variable_isel_vectorized(self, data): raw_indexers = {dim: idx.get(dim, slice(None)) for dim in variable.dims} expected = variable.data[*raw_indexers.values()] - assert isinstance(actual, self.array_type), f"wrong type: {type(actual)}" + assert isinstance( + actual, self.array_type("vectorized_indexing") + ), f"wrong type: {type(actual)}" self.assert_equal(actual, expected) diff --git a/xarray_array_testing/reduction.py b/xarray_array_testing/reduction.py index 38b37e9..1fa0f23 100644 --- a/xarray_array_testing/reduction.py +++ b/xarray_array_testing/reduction.py @@ -25,7 +25,7 @@ def test_variable_numerical_reduce(self, op, data): # compute using xp.(array) expected = getattr(self.xp, op)(variable.data) - assert isinstance(actual, self.array_type), f"wrong type: {type(actual)}" + assert isinstance(actual, self.array_type(op)), f"wrong type: {type(actual)}" self.assert_equal(actual, expected) @pytest.mark.parametrize("op", ["all", "any"]) @@ -39,7 +39,7 @@ def test_variable_boolean_reduce(self, op, data): # compute using xp.(array) expected = getattr(self.xp, op)(variable.data) - assert isinstance(actual, self.array_type), f"wrong type: {type(actual)}" + assert isinstance(actual, self.array_type(op)), f"wrong type: {type(actual)}" self.assert_equal(actual, expected) @pytest.mark.parametrize("op", ["max", "min"]) @@ -53,7 +53,7 @@ def test_variable_order_reduce(self, op, data): # compute using xp.(array) expected = getattr(self.xp, op)(variable.data) - assert isinstance(actual, self.array_type), f"wrong type: {type(actual)}" + assert isinstance(actual, self.array_type(op)), f"wrong type: {type(actual)}" self.assert_equal(actual, expected) @pytest.mark.parametrize("op", ["argmax", "argmin"]) @@ -96,5 +96,5 @@ def test_variable_cumulative_reduce(self, op, data): for axis in range(variable.ndim): expected = getattr(self.xp, array_api_names[op])(expected, axis=axis) - assert isinstance(actual, self.array_type), f"wrong type: {type(actual)}" + assert isinstance(actual, self.array_type(op)), f"wrong type: {type(actual)}" self.assert_equal(actual, expected) From 2c56e897eae4910a25245f16f8826623f081a4fe Mon Sep 17 00:00:00 2001 From: Justus Magin Date: Thu, 23 Oct 2025 23:46:38 +0200 Subject: [PATCH 3/3] change the numpy tests, as well --- xarray_array_testing/tests/test_numpy.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/xarray_array_testing/tests/test_numpy.py b/xarray_array_testing/tests/test_numpy.py index 648f69d..d2f0f1d 100644 --- a/xarray_array_testing/tests/test_numpy.py +++ b/xarray_array_testing/tests/test_numpy.py @@ -18,8 +18,8 @@ class NumpyTestMixin(DuckArrayTestMixin): def xp(self) -> ModuleType: return np - @property - def array_type(self) -> type[np.ndarray]: + @staticmethod + def array_type(op: str) -> type[np.ndarray]: return np.ndarray @staticmethod