From 750c97091b10ee8c4f55a835a1d7a601ab863e21 Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Fri, 27 Sep 2024 19:50:18 +0200 Subject: [PATCH 1/3] Fix namedarray repr crashing --- xarray/core/formatting.py | 2 +- xarray/tests/test_namedarray.py | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/xarray/core/formatting.py b/xarray/core/formatting.py index 646e000a984..38dccfa2038 100644 --- a/xarray/core/formatting.py +++ b/xarray/core/formatting.py @@ -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): diff --git a/xarray/tests/test_namedarray.py b/xarray/tests/test_namedarray.py index 5e4a39ada73..d60c269c7f1 100644 --- a/xarray/tests/test_namedarray.py +++ b/xarray/tests/test_namedarray.py @@ -591,3 +591,14 @@ 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(("x",), np.array([1, 2, 3])) + + # Reprs should not crash: + r = x.__repr__() + x._repr_html_() + + # Basic comparison: + assert r == " Size: 24B\narray([1, 2, 3])" From 9e35d1d8be327b863152c926d9c886e881c50448 Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Fri, 27 Sep 2024 19:58:09 +0200 Subject: [PATCH 2/3] fix mypy --- xarray/tests/test_namedarray.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/xarray/tests/test_namedarray.py b/xarray/tests/test_namedarray.py index d60c269c7f1..9bd6b342170 100644 --- a/xarray/tests/test_namedarray.py +++ b/xarray/tests/test_namedarray.py @@ -594,7 +594,8 @@ def test_warn_on_repeated_dimension_names(self) -> None: def test_repr() -> None: - x = NamedArray(("x",), np.array([1, 2, 3])) + x: NamedArray[Any, np.dtype[np.int64]] + x = NamedArray(("x",), np.array([1, 2, 3], dtype=np.int64)) # Reprs should not crash: r = x.__repr__() From 188f59ee81787d713bf1784248d48ca77d24ba5d Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Fri, 27 Sep 2024 20:23:08 +0200 Subject: [PATCH 3/3] lets try a different dtype --- xarray/tests/test_namedarray.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/xarray/tests/test_namedarray.py b/xarray/tests/test_namedarray.py index 9bd6b342170..39ca08fa06c 100644 --- a/xarray/tests/test_namedarray.py +++ b/xarray/tests/test_namedarray.py @@ -594,12 +594,12 @@ def test_warn_on_repeated_dimension_names(self) -> None: def test_repr() -> None: - x: NamedArray[Any, np.dtype[np.int64]] - x = NamedArray(("x",), np.array([1, 2, 3], dtype=np.int64)) + 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 == " Size: 24B\narray([1, 2, 3])" + assert r == " Size: 8B\narray([0], dtype=uint64)"