refactor(common): clarify coords-entry rules and tighten error labels#733
Merged
FBumann merged 3 commits intoMay 27, 2026
Merged
Conversation
Stacks on top of #732. Three small follow-ups from PR review: - Remove dead `broadcast_mask` (claimed removed in #732, was still present). - `as_dataarray`: normalize bare-tuple coord entries to lists so `coords=[(0, 1, 2)]` behaves identically to `coords=[[0, 1, 2]]` (xarray reads `(a, b)` as `(dim_name, values)` and would otherwise raise a confusing error). - `align_to_coords`: pre-validate coords via `_coords_to_dict` so TypeErrors from a bad `coords` argument propagate with their own message instead of being relabeled "<label> could not be aligned to coords: ...", which previously misdirected users to inspect the bound/mask. Docs: replace the prose paragraph in `_coords_to_dict`'s docstring with an explicit rules table covering every container form and sequence-entry case (named/unnamed `pd.Index`, `pd.MultiIndex`, bare sequences, with/without positional `dims=`). Tests: new `TestCoordsToDictRules` class in `test_common.py` mirrors the docstring table one-test-per-rule so the executable spec stays visibly aligned with the documented contract. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Mirrors the existing rule for unnamed pd.Index: an unnamed MultiIndex paired with a positional dims=[i] entry now gets its flat .name set to dims[i] on a shallow copy (caller's MultiIndex is not mutated). Per-level names are preserved. Removes the asymmetry between Index and MultiIndex in _coords_to_dict: both can now be named either inline (.name) or by position (dims=[i]). An unnamed MultiIndex with no positional dims still raises TypeError since xarray requires a single flat name. Adds one rule-table row and two tests (test_unnamed_multiindex_with_dims_uses_dims, test_unnamed_multiindex_without_dims_raises). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
…ple entries The previous `not isinstance(coords, Coordinates | Mapping)` form was broad and rebuilt `coords` as a fresh list on every call (even when no tuple entries were present). Switch to a positive `isinstance(coords, list | tuple)` guard with a short-circuit `any(isinstance(c, tuple) for c in coords)` check, so the comprehension only runs when there is actually a tuple to normalize. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
FabianHofmann
approved these changes
May 27, 2026
7 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Stacks on top of #732. Small follow-ups from review.
What changes
broadcast_mask— refactor: unify coords-as-truth handling in add_variables/add_constraints #732 claimed it was removed, but the function was still present inlinopy/common.pywith the old FutureWarning semantics that the rest of the PR explicitly replaced.coords=[(0, 1, 2)]previously slipped through_coords_to_dictas a size-only entry but then tripped xarray's(dim_name, values)tuple convention with a confusing error. It now behaves identically tocoords=[[0, 1, 2]].align_to_coordsno longer relabels coords-parsing errors. ATypeErrorfrom a badcoordsentry (e.g. a strayxarray.DataArray) previously came out as"lower bound could not be aligned to coords: ...", misdirecting users to inspect the bound. It now propagates with its original message; only failures convertingvalueget the labeled wrapper.dims=can name an unnamedpd.MultiIndex(parity withpd.Index). The flat.namexarray needs is set on a shallow copy, so the caller's MultiIndex is not mutated. An unnamed MultiIndex with no positionaldims=still raises.Behavior table
This is the spec the docstring of
_coords_to_dictand theTestCoordsToDictRulestest class both follow (one test per row).Container forms (top-level
coords):coordsis...xarray.CoordinatesMappingdictcopy.Sequence-entry rules (
iis the position;dims[i]is the matchingdims=entry when one exists). An entry is unlabeled if it's an unnamedpd.Indexor a barelist/tuple/range/ndarray.pd.Indexwith.name.namedims[i]dims[i])dim_0etc. downstreampd.MultiIndexwith.name.namepd.MultiIndexwithout.namedims[i]pd.MultiIndexwithout.namedims[i])DataArray)Tests
TestCoordsToDictRulesintest/test_common.pymirrors the table one-test-per-rule (22 tests, parametrized overlist/tuple/range/ndarrayfor the bare-sequence cases). Each test name is the rule, so the executable spec stays visibly aligned with the docstring.Test plan
pytest test/test_common.py::TestCoordsToDictRules— 22 passedlinopy/common.pyandlinopy/model.py— pass🤖 Generated with Claude Code