Skip to content

Commit 5ac8394

Browse files
authored
adjust repr tests to account for different platforms (#9127) (#9128)
* 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. * Try combining Unix and Windows dtype repr tests
1 parent 599b779 commit 5ac8394

File tree

3 files changed

+30
-69
lines changed

3 files changed

+30
-69
lines changed

xarray/tests/test_dataarray.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,14 @@ def test_repr(self) -> None:
110110
assert expected == repr(data_array)
111111

112112
def test_repr_multiindex(self) -> None:
113+
obj_size = np.dtype("O").itemsize
113114
expected = dedent(
114-
"""\
115+
f"""\
115116
<xarray.DataArray (x: 4)> Size: 32B
116117
array([0, 1, 2, 3], dtype=uint64)
117118
Coordinates:
118-
* x (x) object 32B MultiIndex
119-
* level_1 (x) object 32B 'a' 'a' 'b' 'b'
119+
* x (x) object {4 * obj_size}B MultiIndex
120+
* level_1 (x) object {4 * obj_size}B 'a' 'a' 'b' 'b'
120121
* level_2 (x) int64 32B 1 2 1 2"""
121122
)
122123
assert expected == repr(self.mda)
@@ -129,15 +130,16 @@ def test_repr_multiindex_long(self) -> None:
129130
mda_long = DataArray(
130131
list(range(32)), coords={"x": mindex_long}, dims="x"
131132
).astype(np.uint64)
133+
obj_size = np.dtype("O").itemsize
132134
expected = dedent(
133-
"""\
135+
f"""\
134136
<xarray.DataArray (x: 32)> Size: 256B
135137
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
136138
17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31],
137139
dtype=uint64)
138140
Coordinates:
139-
* x (x) object 256B MultiIndex
140-
* level_1 (x) object 256B 'a' 'a' 'a' 'a' 'a' 'a' ... 'd' 'd' 'd' 'd' 'd' 'd'
141+
* x (x) object {32 * obj_size}B MultiIndex
142+
* level_1 (x) object {32 * obj_size}B 'a' 'a' 'a' 'a' 'a' 'a' ... 'd' 'd' 'd' 'd' 'd' 'd'
141143
* 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"""
142144
)
143145
assert expected == repr(mda_long)

xarray/tests/test_dataset.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -335,13 +335,14 @@ def test_repr(self) -> None:
335335

336336
def test_repr_multiindex(self) -> None:
337337
data = create_test_multiindex()
338+
obj_size = np.dtype("O").itemsize
338339
expected = dedent(
339-
"""\
340-
<xarray.Dataset> Size: 96B
340+
f"""\
341+
<xarray.Dataset> Size: {8 * obj_size + 32}B
341342
Dimensions: (x: 4)
342343
Coordinates:
343-
* x (x) object 32B MultiIndex
344-
* level_1 (x) object 32B 'a' 'a' 'b' 'b'
344+
* x (x) object {4 * obj_size}B MultiIndex
345+
* level_1 (x) object {4 * obj_size}B 'a' 'a' 'b' 'b'
345346
* level_2 (x) int64 32B 1 2 1 2
346347
Data variables:
347348
*empty*"""
@@ -357,12 +358,12 @@ def test_repr_multiindex(self) -> None:
357358
midx_coords = Coordinates.from_pandas_multiindex(midx, "x")
358359
data = Dataset({}, midx_coords)
359360
expected = dedent(
360-
"""\
361-
<xarray.Dataset> Size: 96B
361+
f"""\
362+
<xarray.Dataset> Size: {8 * obj_size + 32}B
362363
Dimensions: (x: 4)
363364
Coordinates:
364-
* x (x) object 32B MultiIndex
365-
* a_quite_long_level_name (x) object 32B 'a' 'a' 'b' 'b'
365+
* x (x) object {4 * obj_size}B MultiIndex
366+
* a_quite_long_level_name (x) object {4 * obj_size}B 'a' 'a' 'b' 'b'
366367
* level_2 (x) int64 32B 1 2 1 2
367368
Data variables:
368369
*empty*"""

xarray/tests/test_formatting.py

Lines changed: 13 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
from xarray.core.datatree import DataTree # TODO: Remove when can do xr.DataTree
1313
from xarray.tests import requires_cftime, requires_dask, requires_netCDF4
1414

15-
ON_WINDOWS = sys.platform == "win32"
16-
1715

1816
class TestFormatting:
1917
def test_get_indexer_at_least_n_items(self) -> None:
@@ -1071,74 +1069,34 @@ def test_array_repr_dtypes():
10711069
""".strip()
10721070
assert actual == expected
10731071

1074-
1075-
@pytest.mark.skipif(
1076-
ON_WINDOWS,
1077-
reason="Default numpy's dtypes vary according to OS",
1078-
)
1079-
def test_array_repr_dtypes_unix() -> None:
1080-
10811072
# Signed integer dtypes
10821073

1083-
ds = xr.DataArray(np.array([0]), dims="x")
1074+
array = np.array([0])
1075+
ds = xr.DataArray(array, dims="x")
10841076
actual = repr(ds)
1085-
expected = """
1086-
<xarray.DataArray (x: 1)> Size: 8B
1087-
array([0])
1077+
expected = f"""
1078+
<xarray.DataArray (x: 1)> Size: {array.dtype.itemsize}B
1079+
{repr(array)}
10881080
Dimensions without coordinates: x
10891081
""".strip()
10901082
assert actual == expected
10911083

1092-
ds = xr.DataArray(np.array([0], dtype="int32"), dims="x")
1084+
array = np.array([0], dtype="int32")
1085+
ds = xr.DataArray(array, dims="x")
10931086
actual = repr(ds)
1094-
expected = """
1087+
expected = f"""
10951088
<xarray.DataArray (x: 1)> Size: 4B
1096-
array([0], dtype=int32)
1089+
{repr(array)}
10971090
Dimensions without coordinates: x
10981091
""".strip()
10991092
assert actual == expected
11001093

1101-
ds = xr.DataArray(np.array([0], dtype="int64"), dims="x")
1094+
array = np.array([0], dtype="int64")
1095+
ds = xr.DataArray(array, dims="x")
11021096
actual = repr(ds)
1103-
expected = """
1104-
<xarray.DataArray (x: 1)> Size: 8B
1105-
array([0])
1106-
Dimensions without coordinates: x
1107-
""".strip()
1108-
assert actual == expected
1109-
1110-
1111-
@pytest.mark.skipif(
1112-
not ON_WINDOWS,
1113-
reason="Default numpy's dtypes vary according to OS",
1114-
)
1115-
def test_array_repr_dtypes_on_windows() -> None:
1116-
1117-
# Integer dtypes
1118-
1119-
ds = xr.DataArray(np.array([0]), dims="x")
1120-
actual = repr(ds)
1121-
expected = """
1122-
<xarray.DataArray (x: 1)> Size: 4B
1123-
array([0])
1124-
Dimensions without coordinates: x
1125-
""".strip()
1126-
assert actual == expected
1127-
1128-
ds = xr.DataArray(np.array([0], dtype="int32"), dims="x")
1129-
actual = repr(ds)
1130-
expected = """
1131-
<xarray.DataArray (x: 1)> Size: 4B
1132-
array([0])
1133-
Dimensions without coordinates: x
1134-
""".strip()
1135-
assert actual == expected
1136-
1137-
ds = xr.DataArray(np.array([0], dtype="int64"), dims="x")
1138-
actual = repr(ds)
1139-
expected = """
1097+
expected = f"""
11401098
<xarray.DataArray (x: 1)> Size: 8B
1141-
array([0], dtype=int64)
1099+
{repr(array)}
11421100
Dimensions without coordinates: x
11431101
""".strip()
11441102
assert actual == expected

0 commit comments

Comments
 (0)