From 398ff5f4597395fcf91f1df1b2b2d479ca95cbaa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kai=20M=C3=BChlbauer?= Date: Tue, 19 Mar 2024 08:39:30 +0100 Subject: [PATCH 1/3] 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 --- xarray/backends/scipy_.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/xarray/backends/scipy_.py b/xarray/backends/scipy_.py index 154d82bb871..6f8999c1f40 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 +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 NUMPY_2_0 and copy is False else copy + return np.array(data, dtype=self.dtype, copy=copy) def __setitem__(self, key, value): From 83c647022d21ed9247346a4b0038aa1e4c1c901c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kai=20M=C3=BChlbauer?= Date: Tue, 19 Mar 2024 09:13:42 +0100 Subject: [PATCH 2/3] Add whats-new.rst entry --- doc/whats-new.rst | 2 ++ 1 file changed, 2 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 From 879122411b62928245091cb102787501834fa8e7 Mon Sep 17 00:00:00 2001 From: Deepak Cherian Date: Tue, 19 Mar 2024 07:36:21 -0600 Subject: [PATCH 3/3] Apply suggestions from code review --- xarray/backends/scipy_.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/xarray/backends/scipy_.py b/xarray/backends/scipy_.py index 6f8999c1f40..f8c486e512c 100644 --- a/xarray/backends/scipy_.py +++ b/xarray/backends/scipy_.py @@ -40,7 +40,7 @@ from xarray.core.dataset import Dataset -NUMPY_2_0 = module_available("numpy", minversion="2.0.0.dev0") +HAS_NUMPY_2_0 = module_available("numpy", minversion="2.0.0.dev0") def _decode_string(s): @@ -84,7 +84,7 @@ def __getitem__(self, key): # 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 NUMPY_2_0 and copy is False else copy + copy = None if HAS_NUMPY_2_0 and copy is False else copy return np.array(data, dtype=self.dtype, copy=copy)