Skip to content

fix(spec): a where does not guard what a shift, window or lookup reads elsewhere - #1173

Merged
FabianHofmann merged 4 commits into
masterfrom
fix/1171-guard-inside-shift
Nov 18, 2026
Merged

FabianHofmann merged 4 commits into
masterfrom
fix/1171-guard-inside-shift

Conversation

@FabianHofmann

@FabianHofmann FabianHofmann commented Nov 18, 2026 •

Copy link
Copy Markdown
Collaborator

Closes #1171.

Changes proposed in this Pull Request

Note

The following content was generated by AI.

  • A constraint's where narrows the finiteness check to the rows it keeps. A sum, a window (sum_back), a shift and an at() lookup read their operand at other coordinates. Their operand is now checked with the narrowing widened along the axes they move (rows.any(along)), as mathspec's rule says: "a sum, a window, a shift or a lookup drops every guard from outside it".
  • The same holds for the condition readers in a cases: when:: shift(cond), at(cond) and count(cond, over=...).
  • The narrowing stays on the axes they do not move, so where: b > 0 over s still guards a shift along t.
  • The widening reads only ctx.rows, not the where: itself, so a narrowing to a cases: region (fix(spec): check otherwise only where no case applies #1163) widens the same way.
  • Based on df4b2ac from spec/prune-unbuilt, adapted to master. It adds sum over a frame dimension (it leaked too) and uses dropped_dims for a lookup.

Revision (after review):

  • Condition readers shift, at and count now widen too. Before, a NaN of a / b they read under a where silently read as false.
  • The widening helper moved to linopy/spec/context.py as widened, so evaluator.py and where.py share it.
  • The 2-D test now guards over [t, s], so dropping the guard fails it. It gained a lookup keyed by [t, s], which fails if a lookup widens along joined_dims.
  • New tests: a shift summed over an empty dimension (the _data_under path), and the strict rule (where: t > 1 around a shift by 1 is refused).
Validation and reviewer notes

On master, with where: b > 0, b = [0, 2, 2], a = [1, 1, 1], each of these built a row with inf or dropped a term, without an error. On this branch each raises SpecDataError: ... divide of 'a', 'b' is not finite at t=0:

expression master
x <= shift(a / b, along=t, offset=1, edge=0) rhs [0, inf, 0.5]
x <= sum_back(a / b, along=t, window=2) rhs [inf, inf, 1]
x <= sum(a / b, over=t) rhs [inf, inf, inf]
x <= at(a / b, by=succ[u]) (u: t relation) rhs [0.5, inf, 0.5]
shift(a / b * x, along=t, offset=1, edge=0) <= 1 the term of x[0] dropped silently
x <= r, r a case on shift(a / b > 0.1, along=t, offset=1) row t=1 built from the non-finite a / b at t=0

Tests in test/test_spec_builder.py:

  • test_a_where_does_not_guard_what_a_row_reads_at_another_coordinate: the five expression cases above. All fail on master.
  • test_a_where_does_not_guard_what_a_condition_reads_at_another_coordinate: shift, at and count of a condition in a when:. All fail on master and on the first revision.
  • test_a_where_does_not_guard_a_shift_summed_over_an_empty_dimension: fails on master.
  • test_a_where_drops_every_guard_along_the_axis_a_reader_moves: where: t > 1 around a shift by 1 is refused. Builds on master.
  • test_a_where_guards_what_a_row_reads_at_its_own_coordinate: shift(a) / b, a / b + shift(a), sum(a) / b still build, row t=0 masked.
  • test_a_where_guards_a_reader_along_the_axes_it_does_not_move: 2-D shift and lookup, the guard on s holds.

Mutants checked against test/test_spec*.py: dropping the guard entirely, widening a lookup along joined_dims, and no widening in _data_under each fail at least one test.

Checks: pytest test/test_spec*.py (580 passed, 6 skipped), ruff check ., ruff format --check . (ruff 0.16.10), mypy linopy.

Reviewer notes:

  • Decision for the maintainer: the widening is strict, as mathspec's literal rule. Along a moved axis every row counts as read once one row there is kept. So a guard that excludes exactly the rows reading the bad coordinate is refused too, e.g. where: t > 1 around a shift by 1, or a divisor that is zero only in the last period with shift(..., offset=1). Master built these correctly. Exact read sets (the rows mask moved back through each reader) are possible, but more code. A test pins the strict rule, so a change is deliberate.
  • A lookup widens along dropped_dims only. A key column the call does not name is read at its own label, so it keeps its guard. The source commit widened along all joined dims.
  • fix(spec): check otherwise only where no case applies #1163 also edits the loop in _data_under. Stacked, its default scope [ctx] * n becomes [widened(node, ctx)] * n; the case scopes stay. A trial merge resolved this way passes test/test_spec*.py (585 passed, 6 skipped), and a shift of a condition inside nested cases is refused there too.

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

…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.
@codspeed

codspeed Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 181 untouched benchmarks
⏩ 181 skipped benchmarks1


Comparing fix/1171-guard-inside-shift (2a52459) with master (cfedd1e)

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. ↩

…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.
@FabianHofmann
FabianHofmann marked this pull request as ready for review November 18, 2026 10:54
@FabianHofmann
FabianHofmann merged commit 61583ef into master Nov 18, 2026
23 checks passed
@FabianHofmann
FabianHofmann deleted the fix/1171-guard-inside-shift branch November 18, 2026 11:49
FabianHofmann added a commit that referenced this pull request Nov 18, 2026
* fix(spec): check otherwise only where no case applies

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

* fix(spec): a where does not guard what a shift, window or lookup reads 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.

* docs: name #1173 in the release note

* fix(spec): skip a case that applies nowhere

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.

* fix(spec): a where does not guard what a shifted, looked-up or counted 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.

* test(spec): pin what a where guards along the axes a reader moves and 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.

---------

Co-authored-by: Claude <noreply@anthropic.com>
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.

Model.from_spec: a where: guard reaches inside shift, so an infinity enters the model

2 participants