Skip to content

Commit

Permalink
FIX: adapt handling of copy keyword argument in scipy backend for num…
Browse files Browse the repository at this point in the history
…py >= 2.0dev (#8851)

* FIX: adapt handling of copy keyword argument in scipy backend for numpy >= 2.0dev

FIX: adapt handling of copy keyword argument in scipy backend for numpy >= 2.0dev

* Add whats-new.rst entry

* Apply suggestions from code review

---------

Co-authored-by: Deepak Cherian <[email protected]>
  • Loading branch information
kmuehlbauer and dcherian authored Mar 20, 2024
1 parent 61ffc38 commit 0b6716d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
2 changes: 2 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ Bug fixes
By `Kai Mühlbauer <https://github.com/kmuehlbauer>`_.
- do not cast `_FillValue`/`missing_value` in `CFMaskCoder` if `_Unsigned` is provided
(:issue:`8844`, :pull:`8852`).
- Adapt handling of copy keyword argument in scipy backend for numpy >= 2.0dev
(:issue:`8844`, :pull:`8851`).
By `Kai Mühlbauer <https://github.com/kmuehlbauer>`_.

Documentation
Expand Down
10 changes: 10 additions & 0 deletions xarray/backends/scipy_.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
Frozen,
FrozenDict,
close_on_error,
module_available,
try_read_magic_number_from_file_or_path,
)
from xarray.core.variable import Variable
Expand All @@ -39,6 +40,9 @@
from xarray.core.dataset import Dataset


HAS_NUMPY_2_0 = module_available("numpy", minversion="2.0.0.dev0")


def _decode_string(s):
if isinstance(s, bytes):
return s.decode("utf-8", "replace")
Expand Down Expand Up @@ -76,6 +80,12 @@ def __getitem__(self, key):
# with the netCDF4 library by ensuring we can safely read arrays even
# after closing associated files.
copy = self.datastore.ds.use_mmap

# adapt handling of copy-kwarg to numpy 2.0
# see https://github.com/numpy/numpy/issues/25916
# and https://github.com/numpy/numpy/pull/25922
copy = None if HAS_NUMPY_2_0 and copy is False else copy

return np.array(data, dtype=self.dtype, copy=copy)

def __setitem__(self, key, value):
Expand Down

0 comments on commit 0b6716d

Please sign in to comment.