Skip to content

Commit

Permalink
fix the variable repr with display_expand_data=False (pydata#5406)
Browse files Browse the repository at this point in the history
* make sure formatting.array_repr still works with variables

with display_expand_data=False

* always use the short data repr for variables

* update whats-new.rst [skip-ci]
  • Loading branch information
keewis authored May 31, 2021
1 parent 2b38adc commit 83eda1a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
3 changes: 3 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ Bug fixes
- Fix 1-level multi-index incorrectly converted to single index (:issue:`5384`,
:pull:`5385`).
By `Benoit Bovy <https://github.com/benbovy>`_.
- Fix the ``repr`` of :py:class:`Variable` objects with ``display_expand_data=True``
(:pull:`5406`)
By `Justus Magin <https://github.com/keewis>`_.


Documentation
Expand Down
8 changes: 6 additions & 2 deletions xarray/core/formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -502,14 +502,18 @@ def short_data_repr(array):


def array_repr(arr):
from .variable import Variable

# used for DataArray, Variable and IndexVariable
if hasattr(arr, "name") and arr.name is not None:
name_str = f"{arr.name!r} "
else:
name_str = ""

if _get_boolean_with_default("display_expand_data", default=True) or isinstance(
arr.variable._data, MemoryCachedArray
if (
isinstance(arr, Variable)
or _get_boolean_with_default("display_expand_data", default=True)
or isinstance(arr.variable._data, MemoryCachedArray)
):
data_repr = short_data_repr(arr)
else:
Expand Down
8 changes: 8 additions & 0 deletions xarray/tests/test_formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,14 @@ def test_array_repr(self):

assert actual == expected

def test_array_repr_variable(self):
var = xr.Variable("x", [0, 1])

formatting.array_repr(var)

with xr.set_options(display_expand_data=False):
formatting.array_repr(var)


def test_inline_variable_array_repr_custom_repr():
class CustomArray:
Expand Down

0 comments on commit 83eda1a

Please sign in to comment.