Skip to content

Commit

Permalink
Fix broken doctest and tests on accessors (#46)
Browse files Browse the repository at this point in the history
* Fix doctest on cupy_xarray.accessors.CupyDataArrayAccessor.as_cupy

Output type is now just `cupy.ndarray` instead of `cupy.core.core.ndarray`. Also updated pytest command in contributing.rst to include `--doctest-modules` flag.

* Fix ModuleNotFoundError: No module named 'xarray.core.pycompat'

Xref https://github.com/pydata/xarray/blob/v2024.06.0/xarray/namedarray/pycompat.py#L21-L43

* Use try-except instead of xr.namedarray.pycompat to get dask_array_type

---------

Co-authored-by: Deepak Cherian <[email protected]>
  • Loading branch information
weiji14 and dcherian authored Jun 28, 2024
1 parent 7fc3df5 commit 791c62d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
11 changes: 7 additions & 4 deletions cupy_xarray/accessors.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
register_dataarray_accessor,
register_dataset_accessor,
)
from xarray.namedarray.pycompat import DuckArrayModule

dsk = DuckArrayModule("dask")
dask_array_type = dsk.type
try:
import dask.array

dask_array_type = dask.array.Array
except ImportError:
dask_array_type = None


@register_dataarray_accessor("cupy")
Expand Down Expand Up @@ -55,7 +58,7 @@ def as_cupy(self):
>>> da = xr.tutorial.load_dataset("air_temperature").air
>>> gda = da.cupy.as_cupy()
>>> type(gda.data)
<class 'cupy.core.core.ndarray'>
<class 'cupy.ndarray'>
"""
if isinstance(self.da.data, dask_array_type):
Expand Down
8 changes: 7 additions & 1 deletion cupy_xarray/tests/test_accessors.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import numpy as np
import pytest
import xarray as xr
from xarray.core.pycompat import dask_array_type

import cupy_xarray # noqa: F401

try:
import dask.array

dask_array_type = dask.array.Array
except ImportError:
dask_array_type = None


@pytest.fixture
def tutorial_ds_air():
Expand Down
2 changes: 1 addition & 1 deletion docs/source/contributing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ Running the test suite
*cupy-xarray* uses the `pytest <https://docs.pytest.org/en/latest/contents.html>`_
framework for testing. You can run the test suite using::

pytest cupy-xarray
pytest --doctest-modules cupy_xarray

Contributing documentation
==========================
Expand Down

0 comments on commit 791c62d

Please sign in to comment.