Repository navigation
fix(discrete_space): make CellCollection.cells return a tuple (#4142) - #4146
Conversation
CellCollection.cells was a cached_property that handed out the cached list itself, so changing the returned list changed every later read of .cells, while len(collection) and iteration still used the real cells. Keep the cached list in a private property for select_random_cell, which needs it in O(1), and make cells return a new list on each access. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
|
Performance benchmarks:
|
|
Thanks for the reproducer and timings, @deba2k5. The cached-list inconsistency is reproducible, but #4142 gives no real-model example, and CI reports a 17.2% slowdown for large Schelling. @quaquel, should Automated note by an AI model; it can be wrong, and maintainers make the decisions. About this bot. |
|
The docstring seems clear: ""An immutable collection of cells.". So, it is correct that we use cached cells here. |
|
See also the issue, please change it to just retuning a tuple. |
|
hii @deba2k5 , I have seen that one of your test is failing. Could you fix this or else i will work on this issue. |
) CellCollection is documented as an immutable collection of cells, but cells was a cached list, so changing the returned list changed every later read of cells. Return a cached tuple instead, as the maintainers suggested on the issue. The tuple is built once, so access stays as cheap as before, and select_random_cell uses it directly again. Replace the tests for the copying approach with one that checks cells is a tuple that cannot be changed. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
|
Can you update the PR description. Otherwise it looks good. |
|
@quaquel updated PR description can you merge it now |
|
@sangitabera I have done the changes now you can check |
|
Performance benchmarks:
|
Note
Use this template for bug fixes only. For enhancements/new features, use the feature template and get maintainer approval in an issue/discussion before opening a PR.
Pre-PR Checklist
Summary
CellCollection.cellsreturned its cached list itself, so changing that list changed every later read of.cells, whilelen(collection)and iteration still used the real cells. The fix is to return a tuple instead of list.Bug / Issue
Fixes #4142. Reproduced on
main(7787437): aftercells = collection.cells; cells.clear(),len(collection)is 2 andlen(collection.cells)is 0. I have not seen this in a real model; it is the case reported in the issue.Implementation
_cell_list(a privatecached_property) holds the cells and is whatselect_random_celluses, so that path is unchanged.cellsis now a property that returns a new list on each access.Testing
Two new tests in
tests/discrete_space/test_discrete_space.py: the issue's reproduction (and that each access returns its own list), and thatselect_random_cellgives the same cells whether or not the caller reordered or cleared.cells. Both fail onmain.pytest tests --ignore=tests/examples -Werror -Wdefault::PendingDeprecationWarning: 1160 passed (Python 3.12, Windows).ruff checkandruff format --checkon the two changed files: clean. Not run: the browser example tests, which need Playwright.Additional Notes
Trade-off:
.cellsnow costs O(n) per access, measured withtimeiton this machine:select_random_cellis unchanged at about 1 µs. If a copy per access is too costly, alternatives are returning a cached tuple (no cost, but the type changes fromlist, so it needs the deprecation cycle) or only documenting that the list is shared. I chose the copy because it keeps thelisttype; I am happy to change it.AI use: the change, its tests and this description were written with Claude Code (an AI coding assistant), which also ran the tests and timings above.