Skip to content

Commit

Permalink
Fix NamedArray html repr crashing (#9553)
Browse files Browse the repository at this point in the history
* Fix namedarray repr crashing

* fix mypy
  • Loading branch information
Illviljan committed Sep 27, 2024
1 parent 44c7c15 commit cde720f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion xarray/core/formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ def inline_variable_array_repr(var, max_width):
"""Build a one-line summary of a variable's data."""
if hasattr(var._data, "_repr_inline_"):
return var._data._repr_inline_(max_width)
if var._in_memory:
if getattr(var, "_in_memory", False):
return format_array_flat(var, max_width)
dask_array_type = array_type("dask")
if isinstance(var._data, dask_array_type):
Expand Down
12 changes: 12 additions & 0 deletions xarray/tests/test_namedarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -591,3 +591,15 @@ def test_broadcast_to_errors(
def test_warn_on_repeated_dimension_names(self) -> None:
with pytest.warns(UserWarning, match="Duplicate dimension names"):
NamedArray(("x", "x"), np.arange(4).reshape(2, 2))


def test_repr() -> None:
x: NamedArray[Any, np.dtype[np.uint64]]
x = NamedArray(("x",), np.array([0], dtype=np.uint64))

# Reprs should not crash:
r = x.__repr__()
x._repr_html_()

# Basic comparison:
assert r == "<xarray.NamedArray (x: 1)> Size: 8B\narray([0], dtype=uint64)"

0 comments on commit cde720f

Please sign in to comment.