From 5d9eb83783d9b3b569499ad83739186aa1368d2a Mon Sep 17 00:00:00 2001 From: Mathias Hauser Date: Tue, 5 Dec 2023 09:28:48 +0100 Subject: [PATCH 1/6] test and fix empty xindexes repr --- xarray/core/formatting.py | 2 +- xarray/tests/test_formatting.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/xarray/core/formatting.py b/xarray/core/formatting.py index ea0e6275fb6..fc977410094 100644 --- a/xarray/core/formatting.py +++ b/xarray/core/formatting.py @@ -490,7 +490,7 @@ def filter_nondefault_indexes(indexes, filter_indexes: bool): def indexes_repr(indexes, max_rows: int | None = None) -> str: - col_width = _calculate_col_width(chain.from_iterable(indexes)) + col_width = _calculate_col_width(indexes) return _mapping_repr( indexes, diff --git a/xarray/tests/test_formatting.py b/xarray/tests/test_formatting.py index 96bb9c8a3a7..7e79ca5fd6d 100644 --- a/xarray/tests/test_formatting.py +++ b/xarray/tests/test_formatting.py @@ -773,3 +773,33 @@ def __array__(self, dtype=None): # These will crash if var.data are converted to numpy arrays: var.__repr__() var._repr_html_() + + +@pytest.mark.parametrize("as_dataset", (False, True)) +def test_format_xindexes_none(as_dataset): + # ensure repr for empty xindexes can be displayed #8367 + + expected = """\ + Indexes: + *empty*""" + expected = dedent(expected) + + obj = xr.DataArray() + obj = obj._to_temp_dataset() if as_dataset else obj + + actual = obj.xindexes.__repr__() + assert actual == expected + + +@pytest.mark.parametrize("as_dataset", (False, True)) +def test_format_xindexes(as_dataset): + expected = """\ + Indexes: + x PandasIndex""" + expected = dedent(expected) + + obj = xr.DataArray([1], coords={"x": [1]}) + obj = obj._to_temp_dataset() if as_dataset else obj + + actual = obj.xindexes.__repr__() + assert actual == expected From 3f3045db047eace7ec34d6d5b7df81245617a6d2 Mon Sep 17 00:00:00 2001 From: Mathias Hauser Date: Tue, 5 Dec 2023 09:47:50 +0100 Subject: [PATCH 2/6] fix spaces --- xarray/tests/test_formatting.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xarray/tests/test_formatting.py b/xarray/tests/test_formatting.py index 7e79ca5fd6d..cc2c4d78bfa 100644 --- a/xarray/tests/test_formatting.py +++ b/xarray/tests/test_formatting.py @@ -795,7 +795,7 @@ def test_format_xindexes_none(as_dataset): def test_format_xindexes(as_dataset): expected = """\ Indexes: - x PandasIndex""" + x PandasIndex""" expected = dedent(expected) obj = xr.DataArray([1], coords={"x": [1]}) From d7a256694d8fa9a1deb6e6161852789ec7c364c2 Mon Sep 17 00:00:00 2001 From: Mathias Hauser Date: Tue, 5 Dec 2023 09:48:55 +0100 Subject: [PATCH 3/6] max: use default --- xarray/core/formatting.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/xarray/core/formatting.py b/xarray/core/formatting.py index fc977410094..11b60f3d1fe 100644 --- a/xarray/core/formatting.py +++ b/xarray/core/formatting.py @@ -357,7 +357,7 @@ def summarize_attr(key, value, col_width=None): def _calculate_col_width(col_items): - max_name_length = max(len(str(s)) for s in col_items) if col_items else 0 + max_name_length = max((len(str(s)) for s in col_items), default=0) col_width = max(max_name_length, 7) + 6 return col_width @@ -490,7 +490,7 @@ def filter_nondefault_indexes(indexes, filter_indexes: bool): def indexes_repr(indexes, max_rows: int | None = None) -> str: - col_width = _calculate_col_width(indexes) + col_width = _calculate_col_width(chain.from_iterable(indexes)) return _mapping_repr( indexes, From e3f747ab1b8625875abfaa1f20611b7b0281e028 Mon Sep 17 00:00:00 2001 From: Mathias Hauser Date: Tue, 5 Dec 2023 10:08:24 +0100 Subject: [PATCH 4/6] ignore typing --- xarray/tests/test_formatting.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/xarray/tests/test_formatting.py b/xarray/tests/test_formatting.py index cc2c4d78bfa..1281dd0c355 100644 --- a/xarray/tests/test_formatting.py +++ b/xarray/tests/test_formatting.py @@ -785,7 +785,7 @@ def test_format_xindexes_none(as_dataset): expected = dedent(expected) obj = xr.DataArray() - obj = obj._to_temp_dataset() if as_dataset else obj + obj = obj._to_temp_dataset() if as_dataset else obj # type: ignore[assignment] actual = obj.xindexes.__repr__() assert actual == expected @@ -799,7 +799,7 @@ def test_format_xindexes(as_dataset): expected = dedent(expected) obj = xr.DataArray([1], coords={"x": [1]}) - obj = obj._to_temp_dataset() if as_dataset else obj + obj = obj._to_temp_dataset() if as_dataset else obj # type: ignore[assignment] actual = obj.xindexes.__repr__() assert actual == expected From 683bb9565e611c2c317523c965ee39fdcb11af1f Mon Sep 17 00:00:00 2001 From: Mathias Hauser Date: Tue, 5 Dec 2023 17:07:39 +0100 Subject: [PATCH 5/6] Apply suggestions from code review Co-authored-by: Michael Niklas --- xarray/tests/test_formatting.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/xarray/tests/test_formatting.py b/xarray/tests/test_formatting.py index 1281dd0c355..dd6767ed1a9 100644 --- a/xarray/tests/test_formatting.py +++ b/xarray/tests/test_formatting.py @@ -776,7 +776,7 @@ def __array__(self, dtype=None): @pytest.mark.parametrize("as_dataset", (False, True)) -def test_format_xindexes_none(as_dataset): +def test_format_xindexes_none(as_dataset: bool) -> None: # ensure repr for empty xindexes can be displayed #8367 expected = """\ @@ -784,22 +784,22 @@ def test_format_xindexes_none(as_dataset): *empty*""" expected = dedent(expected) - obj = xr.DataArray() + obj: xr.DataArray | xr.Dataset = xr.DataArray() obj = obj._to_temp_dataset() if as_dataset else obj # type: ignore[assignment] - actual = obj.xindexes.__repr__() + actual = repr(obj.xindexes) assert actual == expected @pytest.mark.parametrize("as_dataset", (False, True)) -def test_format_xindexes(as_dataset): +def test_format_xindexes(as_dataset: bool) -> None: expected = """\ Indexes: x PandasIndex""" expected = dedent(expected) - obj = xr.DataArray([1], coords={"x": [1]}) + obj: xr.DataArray | xr.Dataset = xr.DataArray([1], coords={"x": [1]}) obj = obj._to_temp_dataset() if as_dataset else obj # type: ignore[assignment] - actual = obj.xindexes.__repr__() + actual = repr(obj.xindexes) assert actual == expected From 03c7310eb425b2ffe865b606596bf63aaac7e40a Mon Sep 17 00:00:00 2001 From: Mathias Hauser Date: Wed, 6 Dec 2023 10:37:03 +0100 Subject: [PATCH 6/6] Apply suggestions from code review --- xarray/tests/test_formatting.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/xarray/tests/test_formatting.py b/xarray/tests/test_formatting.py index dd6767ed1a9..181b0205352 100644 --- a/xarray/tests/test_formatting.py +++ b/xarray/tests/test_formatting.py @@ -785,7 +785,7 @@ def test_format_xindexes_none(as_dataset: bool) -> None: expected = dedent(expected) obj: xr.DataArray | xr.Dataset = xr.DataArray() - obj = obj._to_temp_dataset() if as_dataset else obj # type: ignore[assignment] + obj = obj._to_temp_dataset() if as_dataset else obj actual = repr(obj.xindexes) assert actual == expected @@ -799,7 +799,7 @@ def test_format_xindexes(as_dataset: bool) -> None: expected = dedent(expected) obj: xr.DataArray | xr.Dataset = xr.DataArray([1], coords={"x": [1]}) - obj = obj._to_temp_dataset() if as_dataset else obj # type: ignore[assignment] + obj = obj._to_temp_dataset() if as_dataset else obj actual = repr(obj.xindexes) assert actual == expected