Skip to content

Commit cde720f

Browse files
authored
Fix NamedArray html repr crashing (#9553)
* Fix namedarray repr crashing * fix mypy
1 parent 44c7c15 commit cde720f

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

xarray/core/formatting.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ def inline_variable_array_repr(var, max_width):
303303
"""Build a one-line summary of a variable's data."""
304304
if hasattr(var._data, "_repr_inline_"):
305305
return var._data._repr_inline_(max_width)
306-
if var._in_memory:
306+
if getattr(var, "_in_memory", False):
307307
return format_array_flat(var, max_width)
308308
dask_array_type = array_type("dask")
309309
if isinstance(var._data, dask_array_type):

xarray/tests/test_namedarray.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,3 +591,15 @@ def test_broadcast_to_errors(
591591
def test_warn_on_repeated_dimension_names(self) -> None:
592592
with pytest.warns(UserWarning, match="Duplicate dimension names"):
593593
NamedArray(("x", "x"), np.arange(4).reshape(2, 2))
594+
595+
596+
def test_repr() -> None:
597+
x: NamedArray[Any, np.dtype[np.uint64]]
598+
x = NamedArray(("x",), np.array([0], dtype=np.uint64))
599+
600+
# Reprs should not crash:
601+
r = x.__repr__()
602+
x._repr_html_()
603+
604+
# Basic comparison:
605+
assert r == "<xarray.NamedArray (x: 1)> Size: 8B\narray([0], dtype=uint64)"

0 commit comments

Comments
 (0)