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

Fix NamedArray html repr crashing #9553

Merged
merged 3 commits into from
Sep 27, 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
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)"
Loading