mesa icon indicating copy to clipboard operation
mesa copied to clipboard

CellCollection.cells exposes a mutable cached list, causing inconsistent collection behavior

Open sangitabera opened this issue 2 weeks ago • 2 comments

Describe the bug The CellCollection.cells property uses @cached_property and returns a mutable list of the collection's cells.

Because the list is cached, modifying the returned list affects subsequent accesses to collection.cells, even though the underlying collection (_cells) remains unchanged.

Expected behavior Modifying the list returned by collection.cells should not modify the result of subsequent accesses or affect operations on the collection.

To Reproduce

cell1 = Cell((0, 0))
cell2 = Cell((0, 1))

collection = CellCollection([cell1, cell2])

cells = collection.cells
cells.clear()

print(len(collection))         # 2
print(len(collection.cells))   # 0

sangitabera avatar Nov 19 '26 04:11 sangitabera

Look at the docstring for CellCollection: "An immutable collection of cells". So, I don't see a bug here.

quaquel avatar Nov 20 '26 09:11 quaquel

I think I begin to understand the claimed bug. Closing it might be premature. However, the proper fix would be to to let the property return a tuple rather than a list. Since this is a bug fix, the fact that it might be break stuff does not matter for semver.

quaquel avatar Nov 20 '26 12:11 quaquel