From 4db3edcb455df2ec5312efde7e5e512e584306d5 Mon Sep 17 00:00:00 2001 From: FBumann <117816358+FBumann@users.noreply.github.com> Date: Mon, 20 Jul 2026 10:02:34 +0200 Subject: [PATCH] test(v1): use public accessors + scoped option in groupby tests Polish for the #717 "polish the tests" checklist, scoped to the v1/legacy groupby-MultiIndex divergence tests in test_linear_expression.py: - Replace the manual try/finally toggle of options["semantics"] in test_group_multiindex_reset_index_matches_v1 with the `with options as o` context manager, so semantics state is restored even if the assertion fails (it was the only test bypassing the conftest marker mechanism). - Swap internal `.data.indexes` / `.data.coords` reaches for the public `.indexes` / `.coords` accessors, matching the already-public sibling tests in the same class. No behaviour change; 584 passed, ruff clean. Co-Authored-By: Claude Opus 4.8 (1M context) --- test/test_linear_expression.py | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/test/test_linear_expression.py b/test/test_linear_expression.py index 964c6ae2e..ae80839fb 100644 --- a/test/test_linear_expression.py +++ b/test/test_linear_expression.py @@ -1449,7 +1449,7 @@ def test_separate_dims_not_stacked(self) -> None: assert {"period", "season"} <= set(grouped.dims) assert "group" not in grouped.dims - assert not isinstance(grouped.data.indexes.get("period"), pd.MultiIndex) + assert not isinstance(grouped.indexes.get("period"), pd.MultiIndex) def test_sparse_combination_filled(self) -> None: # (2020, "s") never occurs -> empty term in the grid @@ -1468,7 +1468,7 @@ def test_dataframe_grouper_stays_compact(self) -> None: df = expr.data[["period", "season"]].to_dataframe()[["period", "season"]] with pytest.warns(LinopySemanticsWarning, match=r"stacked `group` MultiIndex"): grouped = expr.groupby(df).sum() - assert isinstance(grouped.data.indexes["group"], pd.MultiIndex) + assert isinstance(grouped.indexes["group"], pd.MultiIndex) assert grouped.sizes["group"] == 3 # observed, not the 2x2=4 grid @pytest.mark.v1 @@ -1477,8 +1477,8 @@ def test_dataframe_grouper_stays_compact_v1(self) -> None: expr = self._expr([2020, 2020, 2030, 2030], list("wwws")) df = expr.data[["period", "season"]].to_dataframe()[["period", "season"]] grouped = expr.groupby(df).sum() - assert not isinstance(grouped.data.indexes["group"], pd.MultiIndex) - assert {"period", "season"} <= set(grouped.data.coords) + assert not isinstance(grouped.indexes["group"], pd.MultiIndex) + assert {"period", "season"} <= set(grouped.coords) assert grouped.sizes["group"] == 3 @pytest.mark.legacy @@ -1489,14 +1489,14 @@ def test_namelist_observed_warns_legacy(self) -> None: expr = self._expr([2020, 2020, 2030, 2030], list("wwws")) with pytest.warns(LinopySemanticsWarning, match=r"stacked `group` MultiIndex"): grouped = expr.groupby(["period", "season"]).sum(observed=True) - assert isinstance(grouped.data.indexes["group"], pd.MultiIndex) + assert isinstance(grouped.indexes["group"], pd.MultiIndex) @pytest.mark.v1 def test_namelist_observed_flat_v1(self) -> None: expr = self._expr([2020, 2020, 2030, 2030], list("wwws")) grouped = expr.groupby(["period", "season"]).sum(observed=True) - assert not isinstance(grouped.data.indexes["group"], pd.MultiIndex) - assert {"period", "season"} <= set(grouped.data.coords) + assert not isinstance(grouped.indexes["group"], pd.MultiIndex) + assert {"period", "season"} <= set(grouped.coords) @pytest.mark.legacy def test_group_multiindex_reset_index_matches_v1(self) -> None: @@ -1508,13 +1508,10 @@ def test_group_multiindex_reset_index_matches_v1(self) -> None: warnings.simplefilter("ignore") legacy_mi = expr.groupby(["period", "season"]).sum(observed=True) converted = legacy_mi.reset_index("group") - prev = options["semantics"] - try: - options["semantics"] = "v1" + with options as o: + o.set_value(semantics="v1") v1_flat = expr.groupby(["period", "season"]).sum(observed=True) - finally: - options["semantics"] = prev - assert not isinstance(converted.data.indexes["group"], pd.MultiIndex) + assert not isinstance(converted.indexes["group"], pd.MultiIndex) assert_linequal(converted, v1_flat) def test_blowup_warns_when_sparse(self) -> None: