From 2d03278d87f58008c48803daaa771dc2528aa6b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kai=20M=C3=BChlbauer?= Date: Thu, 12 Jun 2025 16:28:59 +0200 Subject: [PATCH 1/3] Fix Pydap test_cmp_local_file for numpy 2.3.0 changes --- doc/whats-new.rst | 2 ++ xarray/tests/test_backends.py | 11 +++++++---- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index c8384e3f1eb..2b03cac049f 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -24,6 +24,8 @@ Deprecations Bug fixes ~~~~~~~~~ +- Fix Pydap test_cmp_local_file for numpy 2.3.0 changes, 1. do always return arrays for all versions and 2. skip astype(str) for numpy >= 2.3.0 for expected data. + By `Kai Mühlbauer `_. Documentation diff --git a/xarray/tests/test_backends.py b/xarray/tests/test_backends.py index e40213e6f46..3782834a0f2 100644 --- a/xarray/tests/test_backends.py +++ b/xarray/tests/test_backends.py @@ -5417,11 +5417,12 @@ def convert_to_pydap_dataset(self, original): @contextlib.contextmanager def create_datasets(self, **kwargs): with open_example_dataset("bears.nc") as expected: + # print("QQ0:", expected["bears"].load()) pydap_ds = self.convert_to_pydap_dataset(expected) actual = open_dataset(PydapDataStore(pydap_ds)) - # TODO solve this workaround: - # netcdf converts string to byte not unicode - expected["bears"] = expected["bears"].astype(str) + if Version(np.__version__) < Version("2.3.0"): + # netcdf converts string to byte not unicode + expected["bears"] = expected["bears"].astype(str) yield actual, expected def test_cmp_local_file(self) -> None: @@ -5441,7 +5442,9 @@ def test_cmp_local_file(self) -> None: assert_equal(actual[{"l": 2}], expected[{"l": 2}]) with self.create_datasets() as (actual, expected): - assert_equal(actual.isel(i=0, j=-1), expected.isel(i=0, j=-1)) + # always return arrays and not scalars + # scalars will be promoted to unicode for numpy >= 2.3.0 + assert_equal(actual.isel(i=[0], j=[-1]), expected.isel(i=[0], j=[-1])) with self.create_datasets() as (actual, expected): assert_equal(actual.isel(j=slice(1, 2)), expected.isel(j=slice(1, 2))) From 24047737ff6ef0995215b3f182c34e34262157dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kai=20M=C3=BChlbauer?= Date: Thu, 12 Jun 2025 16:31:13 +0200 Subject: [PATCH 2/3] Apply suggestions from code review --- doc/whats-new.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 2b03cac049f..618fc72763d 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -24,7 +24,7 @@ Deprecations Bug fixes ~~~~~~~~~ -- Fix Pydap test_cmp_local_file for numpy 2.3.0 changes, 1. do always return arrays for all versions and 2. skip astype(str) for numpy >= 2.3.0 for expected data. +- Fix Pydap test_cmp_local_file for numpy 2.3.0 changes, 1. do always return arrays for all versions and 2. skip astype(str) for numpy >= 2.3.0 for expected data. (:pull:`10421`) By `Kai Mühlbauer `_. From 94ac9676456632e329e205678199f08073fbbf52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kai=20M=C3=BChlbauer?= Date: Thu, 12 Jun 2025 17:36:14 +0200 Subject: [PATCH 3/3] fix another test --- xarray/tests/test_backends.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/xarray/tests/test_backends.py b/xarray/tests/test_backends.py index 3782834a0f2..68ff9233080 100644 --- a/xarray/tests/test_backends.py +++ b/xarray/tests/test_backends.py @@ -5466,7 +5466,9 @@ def test_compatible_to_netcdf(self) -> None: with create_tmp_file() as tmp_file: actual.to_netcdf(tmp_file) with open_dataset(tmp_file) as actual2: - actual2["bears"] = actual2["bears"].astype(str) + if Version(np.__version__) < Version("2.3.0"): + # netcdf converts string to byte not unicode + actual2["bears"] = actual2["bears"].astype(str) assert_equal(actual2, expected) @requires_dask