Skip to content

fix(spec): check otherwise only where no case applies - #1163

Merged
FabianHofmann merged 9 commits into
masterfrom
ccr-1868f888-l1tlxg
Nov 18, 2026
Merged

FabianHofmann merged 9 commits into
masterfrom
ccr-1868f888-l1tlxg

Conversation

@FabianHofmann

@FabianHofmann FabianHofmann commented Nov 16, 2026 •

Copy link
Copy Markdown
Collaborator

Closes #1148.

Changes proposed in this Pull Request

Note

The following content was generated by AI.

#1173 is merged; this PR now targets master.

Model.from_spec evaluated each case value, otherwise: included, on every row and masked it to its region only afterwards. The finiteness check fired in that first evaluation, so a guard like cap == inf could not stop otherwise: cap * pu from being refused at t=0 (inf * 0).

  • linopy/spec/evaluator.py: each case value is evaluated under rows narrowed to its region (_narrowed). With fix(spec): a where does not guard what a shift, window or lookup reads elsewhere #1173, widened (in linopy/spec/context.py) widens that narrowing again along the axis a shift, window, sum or at moves. So a value read from another coordinate is still checked there.
  • A case that applies nowhere is skipped: its value is not evaluated and its memo uses are consumed (memo.skipped). A zero over the dimensions of the cases stands in for it, as a term when the cases carry a variable.
  • _data_under takes the cases' path through _regions and returns early; _data_of evaluates one operand.
  • test/test_spec_builder.py: the case value is refused where a shift or window reads inf * 0 from t=1 (3 new parameters); a nowhere case with a window wider than its axis builds; a nowhere case over a dimension the others lack is zero there; the fold path (linopy.spec.evaluate) folds a guarded expression.
  • doc/release-notes.md: one line.

Taken from 135609f (spec/prune-unbuilt): _regions with the skip via memo.skipped, the zero fill, the early return in _data_under with _data_of, and the name _narrowed. Not taken: its "nowhere" test (it also passes without the skip) and its duplicate fixture. Changed: its fill returned data for cases that carry a variable when the variable case was skipped, which crashed the build ('DataArray' object has no attribute 'coord_dims'). Here such a value becomes a constant term, in a build only.

The repro from the issue builds with x[0] <= 1 and x[1] <= 5.

Validation and reviewer notes
  • pytest test/test_spec*.py: 584 passed, 6 skipped. ruff check ., ruff format --check ., mypy linopy: clean.
  • Which code each new test needs:
    • shift/window parameters fail on the old head 5c7ace3 (the row at t=1 was silently dropped, or x[1] <= 4).
    • the nowhere "unread" parameter fails without the skip (window of 5 on a 3-step axis).
    • the nowhere "zero" parameter fails without the zero fill (KeyError: 's') and without the term conversion.
  • The skip tests the region alone (rows.any()), not ctx.rows & region, so the memo never holds a value that depends on the context.
  • Caching: a value made while a refusal was masked is not memoised (the existing memo.masked rule). A guarded Cases is therefore rebuilt at each use. This costs time, not correctness.
  • In a fold (linopy.spec.evaluate), ctx.rows is None and ctx.solved is set, so only the region narrows and no constant term is made.

Checklist

  • AI-generated content is marked (see AGENTS.md).
  • Code changes are sufficiently documented; i.e. new functions contain docstrings and further explanations may be given in doc.
  • Unit tests for new features were added (if applicable).
  • A note for the release notes doc/release-notes.md of the upcoming release is included.
  • I consent to the release of this PR's code under the MIT license.

Generated by Claude Code

A case value, and the otherwise value, was evaluated over every row and
masked to its region only afterwards, so the finiteness check fired at
rows another case claims (e.g. `cap * pu` where a case guards
`cap == inf`). Each case value is now evaluated with the context's `rows`
narrowed to its region, the mechanism that already scopes the check to a
constraint's built rows, both when building and when reading the data
under an unbuilt sum over an empty dimension.

Fixes #1148

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01F4nx8L63c3o8u6QQvr6TGN
@codspeed

codspeed Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

1 note

ℹ️ 181 benchmarks have no flamegraph

The flamegraph wasn't generated: the symbols of the profiling trace could not be resolved, because no information about the language frames was collected.

See affected benchmarks

✅ 181 untouched benchmarks
⏩ 181 skipped benchmarks1


Comparing ccr-1868f888-l1tlxg (2b4be75) with master (61583ef)

Open in CodSpeed

Footnotes

  1. 181 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

Copy link
Copy Markdown
Collaborator Author

Note

The following content was generated by AI.

Two things this PR does not cover yet. Both are on branch spec/prune-unbuilt:


Generated by Claude Code

…s elsewhere

The rows a constraint's where keeps narrow the finiteness checks of
what it reads. A sum, a window, a shift and a lookup read their operand
at other coordinates along the axes they move. So the narrowing is
widened along those axes to every row there. It stays along the other
axes, where they read their own coordinate.

Before, where: b > 0 with x <= shift(a / b, edge=0) built x[1] <= inf
without a word.

The widening reads only ctx.rows, so it applies to any narrowing.
…o ccr-1868f888-l1tlxg

# Conflicts:
#	doc/release-notes.md
#	linopy/spec/evaluator.py
A case whose region holds no row is not evaluated, and its uses are
consumed unread. A zero over the dimensions of the cases stands in for it,
a term when the cases carry a variable. Its data is no longer checked, so
a window wider than its axis there no longer raises.

Tests pin that a shift or window in a case value is still refused where it
reads a non-finite value, and cover the fold path. The release note is one
line naming this PR.
@FabianHofmann
FabianHofmann changed the base branch from master to fix/1171-guard-inside-shift November 18, 2026 10:12
…d condition reads elsewhere

A shift, an at() and a count of a condition read their operand at other
coordinates, as their expression forms do. So the rows a where keeps are
widened along the axes they move, the same way.

Before, where: b > 0 with a case on shift(a / b > 0.1, along=t, offset=1)
read the NaN of a / b at t=0 as false and built row t=1 without a word.

The widening moves to context.py, so the evaluator and the conditions
share it.
… keeps

A guard over t and s keeps column p out of a shift and a lookup along t,
so dropping the guard fails. The lookup keyed by t and s also fails when
it widens along the key column it carries.

A shift summed over an empty dimension and a where: t > 1 around a shift
are refused, the second as mathspec's literal rule asks.
…o ccr-1868f888-l1tlxg

# Conflicts:
#	doc/release-notes.md
#	linopy/spec/evaluator.py
@FabianHofmann
FabianHofmann marked this pull request as ready for review November 18, 2026 10:54
Base automatically changed from fix/1171-guard-inside-shift to master November 18, 2026 11:49
# Conflicts:
#	doc/release-notes.md
#	linopy/spec/evaluator.py
@FabianHofmann
FabianHofmann merged commit 8dce76b into master Nov 18, 2026
23 checks passed
@FabianHofmann
FabianHofmann deleted the ccr-1868f888-l1tlxg branch November 18, 2026 12:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

spec: otherwise is checked where a case already applies

2 participants