Skip to content

Commit

Permalink
adjust repr tests to account for different platforms (#9127) (#9128)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
mgorny authored Jun 16, 2024
1 parent 599b779 commit 5ac8394
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 69 deletions.
14 changes: 8 additions & 6 deletions xarray/tests/test_dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""\
<xarray.DataArray (x: 4)> 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)
Expand All @@ -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"""\
<xarray.DataArray (x: 32)> 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)
Expand Down
17 changes: 9 additions & 8 deletions xarray/tests/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
"""\
<xarray.Dataset> Size: 96B
f"""\
<xarray.Dataset> 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*"""
Expand All @@ -357,12 +358,12 @@ def test_repr_multiindex(self) -> None:
midx_coords = Coordinates.from_pandas_multiindex(midx, "x")
data = Dataset({}, midx_coords)
expected = dedent(
"""\
<xarray.Dataset> Size: 96B
f"""\
<xarray.Dataset> 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*"""
Expand Down
68 changes: 13 additions & 55 deletions xarray/tests/test_formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
from xarray.core.datatree import DataTree # TODO: Remove when can do xr.DataTree
from xarray.tests import requires_cftime, requires_dask, requires_netCDF4

ON_WINDOWS = sys.platform == "win32"


class TestFormatting:
def test_get_indexer_at_least_n_items(self) -> None:
Expand Down Expand Up @@ -1071,74 +1069,34 @@ def test_array_repr_dtypes():
""".strip()
assert actual == expected


@pytest.mark.skipif(
ON_WINDOWS,
reason="Default numpy's dtypes vary according to OS",
)
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 = """
<xarray.DataArray (x: 1)> Size: 8B
array([0])
expected = f"""
<xarray.DataArray (x: 1)> 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"""
<xarray.DataArray (x: 1)> 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 = """
<xarray.DataArray (x: 1)> Size: 8B
array([0])
Dimensions without coordinates: x
""".strip()
assert actual == expected


@pytest.mark.skipif(
not ON_WINDOWS,
reason="Default numpy's dtypes vary according to OS",
)
def test_array_repr_dtypes_on_windows() -> None:

# Integer dtypes

ds = xr.DataArray(np.array([0]), dims="x")
actual = repr(ds)
expected = """
<xarray.DataArray (x: 1)> Size: 4B
array([0])
Dimensions without coordinates: x
""".strip()
assert actual == expected

ds = xr.DataArray(np.array([0], dtype="int32"), dims="x")
actual = repr(ds)
expected = """
<xarray.DataArray (x: 1)> Size: 4B
array([0])
Dimensions without coordinates: x
""".strip()
assert actual == expected

ds = xr.DataArray(np.array([0], dtype="int64"), dims="x")
actual = repr(ds)
expected = """
expected = f"""
<xarray.DataArray (x: 1)> Size: 8B
array([0], dtype=int64)
{repr(array)}
Dimensions without coordinates: x
""".strip()
assert actual == expected

0 comments on commit 5ac8394

Please sign in to comment.