From b8ad15de641a5de46a14c9a3ee55717196e29368 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20G=C3=B3rny?= Date: Sat, 15 Jun 2024 07:10:58 +0200 Subject: [PATCH] adjust repr tests to account for different platforms (#9127) Adjust the expectations in repr tests to account for different object sizes and numpy type representations across platforms, particularly fixing the tests on 32-bit platforms. Firstly, this involves getting the object type size from NumPy and using it to adjust the expectations in DataArray and Dataset tests. The tests were already using int64 type consistently, so only the sizes used for Python objects needed to be adjusted. Secondly, this involves fixing `test_array_repr_dtypes_unix`. The test specifically focuses on testing a 32-bit, 64-bit and "native" data type, which affect both size and actual representation (NumPy skips the dtype attribute for the native data type). Get the expected size from NumPy for the native int type, and reuse `repr()` from NumPy for all array types. --- xarray/tests/test_dataarray.py | 14 ++++++++------ xarray/tests/test_dataset.py | 17 +++++++++-------- xarray/tests/test_formatting.py | 23 +++++++++++++---------- 3 files changed, 30 insertions(+), 24 deletions(-) diff --git a/xarray/tests/test_dataarray.py b/xarray/tests/test_dataarray.py index ece2ddf8144..659c7c168a5 100644 --- a/xarray/tests/test_dataarray.py +++ b/xarray/tests/test_dataarray.py @@ -110,13 +110,14 @@ def test_repr(self) -> None: assert expected == repr(data_array) def test_repr_multiindex(self) -> None: + obj_size = np.dtype("O").itemsize expected = dedent( - """\ + f"""\ Size: 32B array([0, 1, 2, 3], dtype=uint64) Coordinates: - * x (x) object 32B MultiIndex - * level_1 (x) object 32B 'a' 'a' 'b' 'b' + * x (x) object {4 * obj_size}B MultiIndex + * level_1 (x) object {4 * obj_size}B 'a' 'a' 'b' 'b' * level_2 (x) int64 32B 1 2 1 2""" ) assert expected == repr(self.mda) @@ -129,15 +130,16 @@ def test_repr_multiindex_long(self) -> None: mda_long = DataArray( list(range(32)), coords={"x": mindex_long}, dims="x" ).astype(np.uint64) + obj_size = np.dtype("O").itemsize expected = dedent( - """\ + f"""\ Size: 256B array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31], dtype=uint64) Coordinates: - * x (x) object 256B MultiIndex - * level_1 (x) object 256B 'a' 'a' 'a' 'a' 'a' 'a' ... 'd' 'd' 'd' 'd' 'd' 'd' + * x (x) object {32 * obj_size}B MultiIndex + * level_1 (x) object {32 * obj_size}B 'a' 'a' 'a' 'a' 'a' 'a' ... 'd' 'd' 'd' 'd' 'd' 'd' * level_2 (x) int64 256B 1 2 3 4 5 6 7 8 1 2 3 4 ... 5 6 7 8 1 2 3 4 5 6 7 8""" ) assert expected == repr(mda_long) diff --git a/xarray/tests/test_dataset.py b/xarray/tests/test_dataset.py index 81b27da8d5f..f6829861776 100644 --- a/xarray/tests/test_dataset.py +++ b/xarray/tests/test_dataset.py @@ -335,13 +335,14 @@ def test_repr(self) -> None: def test_repr_multiindex(self) -> None: data = create_test_multiindex() + obj_size = np.dtype("O").itemsize expected = dedent( - """\ - Size: 96B + f"""\ + Size: {8 * obj_size + 32}B Dimensions: (x: 4) Coordinates: - * x (x) object 32B MultiIndex - * level_1 (x) object 32B 'a' 'a' 'b' 'b' + * x (x) object {4 * obj_size}B MultiIndex + * level_1 (x) object {4 * obj_size}B 'a' 'a' 'b' 'b' * level_2 (x) int64 32B 1 2 1 2 Data variables: *empty*""" @@ -357,12 +358,12 @@ def test_repr_multiindex(self) -> None: midx_coords = Coordinates.from_pandas_multiindex(midx, "x") data = Dataset({}, midx_coords) expected = dedent( - """\ - Size: 96B + f"""\ + Size: {8 * obj_size + 32}B Dimensions: (x: 4) Coordinates: - * x (x) object 32B MultiIndex - * a_quite_long_level_name (x) object 32B 'a' 'a' 'b' 'b' + * x (x) object {4 * obj_size}B MultiIndex + * a_quite_long_level_name (x) object {4 * obj_size}B 'a' 'a' 'b' 'b' * level_2 (x) int64 32B 1 2 1 2 Data variables: *empty*""" diff --git a/xarray/tests/test_formatting.py b/xarray/tests/test_formatting.py index 2c40ac88f98..2e4c912fe65 100644 --- a/xarray/tests/test_formatting.py +++ b/xarray/tests/test_formatting.py @@ -1080,29 +1080,32 @@ def test_array_repr_dtypes_unix() -> None: # Signed integer dtypes - ds = xr.DataArray(np.array([0]), dims="x") + array = np.array([0]) + ds = xr.DataArray(array, dims="x") actual = repr(ds) - expected = """ - Size: 8B -array([0]) + expected = f""" + Size: {array.dtype.itemsize}B +{repr(array)} Dimensions without coordinates: x """.strip() assert actual == expected - ds = xr.DataArray(np.array([0], dtype="int32"), dims="x") + array = np.array([0], dtype="int32") + ds = xr.DataArray(array, dims="x") actual = repr(ds) - expected = """ + expected = f""" Size: 4B -array([0], dtype=int32) +{repr(array)} Dimensions without coordinates: x """.strip() assert actual == expected - ds = xr.DataArray(np.array([0], dtype="int64"), dims="x") + array = np.array([0], dtype="int64") + ds = xr.DataArray(array, dims="x") actual = repr(ds) - expected = """ + expected = f""" Size: 8B -array([0]) +{repr(array)} Dimensions without coordinates: x """.strip() assert actual == expected