Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adjust repr tests to account for different platforms (#9127) #9128

Merged
merged 2 commits into from
Jun 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading