From 0b6716d12dec5628618137c8a34a120b60ba9c30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kai=20M=C3=BChlbauer?= Date: Wed, 20 Mar 2024 15:47:45 +0100 Subject: [PATCH] FIX: adapt handling of copy keyword argument in scipy backend for numpy >= 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 --- doc/whats-new.rst | 2 ++ xarray/backends/scipy_.py | 10 ++++++++++ 2 files changed, 12 insertions(+) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 6bde6504a7f..cd01f0adaf1 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -59,6 +59,8 @@ Bug fixes By `Kai Mühlbauer `_. - 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 `_. Documentation diff --git a/xarray/backends/scipy_.py b/xarray/backends/scipy_.py index 154d82bb871..f8c486e512c 100644 --- a/xarray/backends/scipy_.py +++ b/xarray/backends/scipy_.py @@ -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 @@ -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") @@ -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):