diff --git a/xarray/core/formatting.py b/xarray/core/formatting.py index 31a1b777f43..c3e7187abed 100644 --- a/xarray/core/formatting.py +++ b/xarray/core/formatting.py @@ -11,6 +11,7 @@ from pandas.errors import OutOfBoundsDatetime from .duck_array_ops import array_equiv +from .indexing import MemoryCachedArray from .options import OPTIONS, _get_boolean_with_default from .pycompat import dask_array_type, sparse_array_type from .utils import is_duck_array @@ -507,10 +508,12 @@ def array_repr(arr): else: name_str = "" - if _get_boolean_with_default("display_expand_data", default=True): + if _get_boolean_with_default("display_expand_data", default=True) or isinstance( + arr.variable._data, MemoryCachedArray + ): data_repr = short_data_repr(arr) else: - data_repr = inline_variable_array_repr(arr, OPTIONS["display_width"]) + data_repr = inline_variable_array_repr(arr.variable, OPTIONS["display_width"]) summary = [ "".format(type(arr).__name__, name_str, dim_summary(arr)), diff --git a/xarray/tests/test_formatting.py b/xarray/tests/test_formatting.py index 1e2a968debe..e823a67f2db 100644 --- a/xarray/tests/test_formatting.py +++ b/xarray/tests/test_formatting.py @@ -8,6 +8,8 @@ import xarray as xr from xarray.core import formatting +from . import requires_netCDF4 + class TestFormatting: def test_get_indexer_at_least_n_items(self): @@ -472,6 +474,25 @@ def test_large_array_repr_length(): assert len(result) < 50 +@requires_netCDF4 +def test_repr_file_collapsed(tmp_path): + arr = xr.DataArray(np.arange(300), dims="test") + arr.to_netcdf(tmp_path / "test.nc", engine="netcdf4") + + with xr.open_dataarray(tmp_path / "test.nc") as arr, xr.set_options( + display_expand_data=False + ): + actual = formatting.array_repr(arr) + expected = dedent( + """\ + + array([ 0, 1, 2, ..., 297, 298, 299]) + Dimensions without coordinates: test""" + ) + + assert actual == expected + + @pytest.mark.parametrize( "display_max_rows, n_vars, n_attr", [(50, 40, 30), (35, 40, 30), (11, 40, 30), (1, 40, 30)],