Skip to content

FIX: Cells.values() and items() read each value by its stored key - #281

Merged
fumitoh merged 2 commits into
mainfrom
claude/fix-cells-values-items
Oct 12, 2026
Merged

fumitoh merged 2 commits into
mainfrom
claude/fix-cells-values-items

Conversation

@fumitoh

@fumitoh fumitoh commented Oct 12, 2026 •

Copy link
Copy Markdown
Owner

Based on main after #278 was merged (2026-10-12). This PR was first opened on top of #278's branch; its two commits were then replayed onto main unchanged (same tree).

Approved by the owner on 2026-10-12, including the four behaviour changes listed below.

The bug

Cells took values() and items() from collections.abc.Mapping, which looks each key that iter(cells) yields up again as cells[key]. Cells.__getitem__ reads a tuple as the tuple of all the arguments, so for a Cells h(x) whose argument is a tuple:

  • with h((1, 2)) stored, values() and items() raised TypeError;
  • with only h((1,)) stored, they ran the formula for h(1), stored it, and raised RuntimeError;
  • with both stored, they returned h(1)'s value for the key (1,).

For a Cells f(a, *args) they ran the formula and raised RuntimeError whatever the arguments.

The fix

Cells gets its own values() and items(), returning CellsValuesView and CellsItemsView, which are subclasses of collections.abc.ValuesView and ItemsView. They iterate the stored keys and read each value with CellsImpl.get_value_from_key.

  • The hot path is untouched. get_node and its all-positional fast path, tuplize_key, Cells.__call__, Cells.__getitem__ and CellsImpl.call/get_value are byte-identical (source, AST and bytecode compared). A micro-benchmark of h(x) and h[x] showed no difference.
  • Dependencies are kept. get_value_from_key goes through executor.eval_node, so a formula that reads values() through a Reference still records an edge from each value it reads, and is cleared when one of them changes.
  • It's faster. Over 100,000 values, list(h.values()) takes about 53 ms instead of 95 ms, and list(h.items()) about 70 ms instead of 103 ms.

Behaviour changes (approved)

  1. values() and items() return the stored values in the cases above, instead of raising or returning another argument's value.
  2. (key, value) in cells.items() reads only stored values. It is False when nothing is stored for key; before, it ran the formula and stored the value.
  3. (key, value) in cells.items() applies no default arguments, like key in cells. For g(t, k=1) holding g(1), ((1,), (1, 1)) in g.items() goes from True to False.
  4. The types of values() and items() become CellsValuesView and CellsItemsView; isinstance checks against the collections.abc views still pass.

Changes 2 and 3 are under "Backward Incompatible Changes" in the draft release notes.

What stays broken

key in cells, cells[key], cells.get(key), cells[key] = value and dict(cells) still read a tuple key as all the arguments. Fixing them needs a change to __getitem__, which is on the hot path. Two alternatives were prototyped and rejected:

  • making in follow iteration breaks the "check, then read" pattern: with only h((1,)) stored, if (1,) in h: h[(1,)] would run h(1);
  • changing what iteration yields makes [h(k) for k in h] return wrong values without any error.

The Known limitation box in cells.rst now covers exactly these cases, *args Cells included, and names the workarounds: dict(cells.items()), and h[((1, 2),)] or h.node((1, 2)).value.

Files

  • modelx/core/cells.py: the two view classes, Cells.values() and Cells.items() with versionchanged notes.
  • modelx/tests/core/cells/test_cells_views.py: 12 tests covering the three cases, 0/1/2 parameters, defaults, *args, uncached, ItemSpace, that no formula runs, and dependencies through a Reference. With Mapping's views restored, 10 of them fail.
  • doc/source/reference/cells.rst: the Value operations section, with values and items added to its table so their docstrings are published.
  • doc/source/releases/relnotes_v0_34_0.rst (new draft) and doc/source/whatsnew.rst.

Checked

  • Full modelx suite (Python 3.11, Linux): 1,570 passed, 8 skipped, 1 failed. The failure is test_lifelib.py::test_assets, which needs QuantLib and fails the same way on main.
  • The new tests on Python 3.9, 3.11, 3.13 and 3.14 (an earlier, code-identical version). The changed files parse as Python 3.9.
  • Downstream suites against this modelx: modelx-bridge 835 passed, modelx-mcp 975 passed.
  • Fresh Sphinx build (-E): only the 4 warnings main already has. The new pages and the release note's links render.
  • Every statement in the Known limitation box and every example in the release note was checked by a probe on base and on this branch.

Found, not fixed here

  • series and frame raise ValueError (pandas builds a MultiIndex) when every stored argument of a one-parameter Cells is a tuple and one has two or more elements.
  • to_series(args) and to_frame(args) read a tuple argument as the whole argument tuple.
  • A formula with *args receives its arguments wrapped once more: va(1, 2) with lambda *args: args returns ((1, 2),).

🤖 Generated with Claude Code

https://claude.ai/code/session_01LMVwDKYrkyW1zAbzKSK8Zc

@fumitoh
fumitoh marked this pull request as ready for review October 12, 2026 10:41
@fumitoh
fumitoh changed the base branch from claude/stoic-bardeen-qpv3a0 to main October 12, 2026 10:44
…under

Cells took values() and items() from collections.abc.Mapping, which
looks each key that iter(cells) yields up again as cells[key].
Cells.__getitem__ reads a tuple as the tuple of all the arguments, so for
a Cells h(x) whose argument is a tuple the key was read as other
arguments: with h((1, 2)) stored, values() and items() raised TypeError;
with only h((1,)) stored, they ran the formula for h(1), stored it and
raised RuntimeError (dictionary changed size during iteration); with
both stored, they gave h(1)'s value for the key (1,).

Cells now has its own values() and items(), returning CellsValuesView and
CellsItemsView. They iterate the stored keys and read each value with
CellsImpl.get_value_from_key, which goes through executor.eval_node but
not through get_node or tuplize_key. So a formula that reads them through
a Reference still depends on each value it reads, and the all-positional
fast path in get_node, Cells.__call__ and Cells.__getitem__ are unchanged.
Over 100,000 stored values, list(h.values()) takes about 53 ms instead of
95 ms, and list(h.items()) about 70 ms instead of 103 ms.

(key, value) in cells.items() now reads only stored values too: it is
False when nothing is stored for key, where it ran the formula and stored
the value, and, like key in cells, it applies no default arguments.

key in cells, cells[key], cells.get(key), cells[key] = value and
dict(cells) still read a tuple key as all the arguments; fixing them needs
a change to __getitem__. cells.rst keeps them in a narrowed Known
limitation box, and lists Cells.values and Cells.items in the Value
operations table so their docstrings are published. Adds a draft of the
v0.34.0 release notes with the fix under Bug Fixes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LMVwDKYrkyW1zAbzKSK8Zc
Review of the fix found that it also repairs a Cells with *args after
another parameter, where Mapping's values() and items() read every key
as other arguments, ran the formula and raised RuntimeError, and that
the limitation that stays covers *args Cells too: for f(*args),
key in f is False for every key that iter(f) yields; for f(a, *args),
f[key], f.get(key), dict(f) and f.node(*key) read other arguments.
The release note, the versionchanged notes and the Known limitation box
now say so, and the node(*args) recipe below the box excludes *args
Cells. Each statement was checked by a probe on this branch.

The release note moves the two changes to (key, value) in cells.items()
under Backward Incompatible Changes, with an example of the default
arguments it no longer applies, and no longer claims to be complete.
The comment in _iter_stored quotes the same timing as the release note
(53 ms against 95 ms) and no longer says that every change during
iteration raises RuntimeError.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LMVwDKYrkyW1zAbzKSK8Zc
@fumitoh
fumitoh force-pushed the claude/fix-cells-values-items branch from 2b35afb to 2e40fb5 Compare October 12, 2026 10:44
@fumitoh
fumitoh merged commit 1b90682 into main Oct 12, 2026
19 checks passed
fumitoh pushed a commit that referenced this pull request Oct 17, 2026
The owner decided on 2026-10-17 that tools tell modelx objects apart by
class name, after checking that the class is modelx's own
(type(v).__module__.startswith("modelx.")), and that no _kind class
attribute is added. The names of the ten classes T1 lists become frozen
tool API, with the promise that each lives in the modelx package; the
module paths stay internal. The module check is part of the recipe
because a Reference can hold a user's own class named Model or Cells
(fix-bridge-adoption/b01b).

T1 is rewritten as that recipe, re-measured on main at 1b90682: right for
every kind, nested items, a DynamicSpace, deleted objects and a closed
model's objects, None for user classes, views, nodes and plain values,
at 0.13-0.20 us. A frozen class name cannot be deprecated with a warning,
so a rename is a breaking change with a release note. The decision
moves one line of PR 1's toolapi.rst, which PR 2 changes.

Everything that read _kind follows: T5, T10's is_ref, F11's example, the
C3 wire table, the coverage table, section 5, the PR 2 plan and tests, and
the bridge and spyder adoption notes. The progress line records
#278 and #281 as merged and #282 and #283 as filed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LMVwDKYrkyW1zAbzKSK8Zc
fumitoh pushed a commit that referenced this pull request Oct 17, 2026
Two reviewers checked the decision 14 revision against the whole document
and against the code on main and on 0.33.0, where the recipe gives
identical answers.

The recipe now checks that __module__ is a str before calling startswith:
wrapt's pure-Python ObjectProxy copies a __module__ property into every
proxy class, and the unguarded check raised AttributeError on it, so a
Reference holding one would have failed to encode (names14/probe_edge.py).

A frozen class name cannot warn, and the revision let a rename through
with only a release note, which bypassed decision 2. It is now
recommended, for the owner to confirm, that a listed name follows
decision 2 without the warning step: marked deprecated two minor releases
ahead, renamed only when the downstream job is green. Section 5 records
the exception and section 9 adds it as risk 14.

PR 1's page changes in three places, not one: the class-name example
becomes an unlisted class, the imports bullet covers modelx.io, and the
Deprecation section gains the exception. Costs are restated from one
harness for both the recipe and _kind. Also corrected: the summary count
of member sets, the section 3.0 example that used _kind, the 'IOSpec'
kind left in section 4, the internal list in section 5, PR 2's file list,
the bridge table that still said doc.get would send 'Reference', the
section 8 transition note, the ExcelRange edge case, and what #281 did to
cells.rst.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LMVwDKYrkyW1zAbzKSK8Zc
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.

2 participants