Repository navigation
FIX: Cells.values() and items() read each value by its stored key - #281
Merged
Merged
Conversation
fumitoh
marked this pull request as ready for review
October 12, 2026 10:41
…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
force-pushed
the
claude/fix-cells-values-items
branch
from
October 12, 2026 10:44
2b35afb to
2e40fb5
Compare
This was referenced Oct 12, 2026
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
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.
Based on
mainafter #278 was merged (2026-10-12). This PR was first opened on top of #278's branch; its two commits were then replayed ontomainunchanged (same tree).Approved by the owner on 2026-10-12, including the four behaviour changes listed below.
The bug
Cellstookvalues()anditems()fromcollections.abc.Mapping, which looks each key thatiter(cells)yields up again ascells[key].Cells.__getitem__reads a tuple as the tuple of all the arguments, so for a Cellsh(x)whose argument is a tuple:h((1, 2))stored,values()anditems()raisedTypeError;h((1,))stored, they ran the formula forh(1), stored it, and raisedRuntimeError;h(1)'s value for the key(1,).For a Cells
f(a, *args)they ran the formula and raisedRuntimeErrorwhatever the arguments.The fix
Cellsgets its ownvalues()anditems(), returningCellsValuesViewandCellsItemsView, which are subclasses ofcollections.abc.ValuesViewandItemsView. They iterate the stored keys and read each value withCellsImpl.get_value_from_key.get_nodeand its all-positional fast path,tuplize_key,Cells.__call__,Cells.__getitem__andCellsImpl.call/get_valueare byte-identical (source, AST and bytecode compared). A micro-benchmark ofh(x)andh[x]showed no difference.get_value_from_keygoes throughexecutor.eval_node, so a formula that readsvalues()through a Reference still records an edge from each value it reads, and is cleared when one of them changes.list(h.values())takes about 53 ms instead of 95 ms, andlist(h.items())about 70 ms instead of 103 ms.Behaviour changes (approved)
values()anditems()return the stored values in the cases above, instead of raising or returning another argument's value.(key, value) in cells.items()reads only stored values. It isFalsewhen nothing is stored forkey; before, it ran the formula and stored the value.(key, value) in cells.items()applies no default arguments, likekey in cells. Forg(t, k=1)holdingg(1),((1,), (1, 1)) in g.items()goes fromTruetoFalse.values()anditems()becomeCellsValuesViewandCellsItemsView;isinstancechecks against thecollections.abcviews 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] = valueanddict(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:infollow iteration breaks the "check, then read" pattern: with onlyh((1,))stored,if (1,) in h: h[(1,)]would runh(1);[h(k) for k in h]return wrong values without any error.The Known limitation box in
cells.rstnow covers exactly these cases,*argsCells included, and names the workarounds:dict(cells.items()), andh[((1, 2),)]orh.node((1, 2)).value.Files
modelx/core/cells.py: the two view classes,Cells.values()andCells.items()withversionchangednotes.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, withvaluesanditemsadded to its table so their docstrings are published.doc/source/releases/relnotes_v0_34_0.rst(new draft) anddoc/source/whatsnew.rst.Checked
test_lifelib.py::test_assets, which needs QuantLib and fails the same way onmain.-E): only the 4 warningsmainalready has. The new pages and the release note's links render.Found, not fixed here
seriesandframeraiseValueError(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)andto_frame(args)read a tuple argument as the whole argument tuple.*argsreceives its arguments wrapped once more:va(1, 2)withlambda *args: argsreturns((1, 2),).🤖 Generated with Claude Code
https://claude.ai/code/session_01LMVwDKYrkyW1zAbzKSK8Zc