Repository navigation
Fix Grid.create_property_layer() silently overwriting Cell attributes - #3840
Conversation
Grid._attach_property_layer() only checked collisions against the Grid's own class and the Cell's __slots__, so a property layer named e.g. is_empty or is_full silently replaced the live Cell.is_empty / Cell.is_full properties instead of raising ValueError. Since DiscreteSpace.empties and Grid.cells_with_capacity rely on those properties, this silently corrupted occupancy/capacity queries. Extend the check to also reject names colliding with any attribute on self.cell_klass.__mro__, with an exception for empty since Grid.__init__ itself installs that name as a property layer over Cell.empty. Fixes mesa#3839
|
Performance benchmarks:
|
dietcoke-17
left a comment
There was a problem hiding this comment.
Nice fix — this closes a real gap (#3839). _attach_property_layer() previously only checked new names against Grid's own attributes and cell_klass.__slots__. Since is_empty/is_full are @property methods living in the class __dict__ rather than __slots__, naming a layer is_empty would silently shadow the live property with a static array — no error, just quietly wrong DiscreteSpace.empties / Grid.cells_with_capacity from then on. Walking cell_klass.__mro__ and collecting __dict__ keys correctly catches properties, plain attributes, and slots alike.
Tests are great: test_property_layer_rejects_cell_attribute_collision covers both property- and plain-attribute collisions and checks real post-rejection behavior (grid.empties, cell.is_full, cells_with_capacity), not just the exception. test_property_layer_empty_bootstrap_is_not_blocked is a good, minimal guard on the internal bootstrap.
Use self.cell_klass.__name__ in the conflict error instead of a hardcoded Cell, so the message names the actual class for custom cell_klass types like Network/Voronoi grids. Add a comment explaining why the empty name is exempt from the collision check.
8f553ad to
1800ad0
Compare
Summary
Grid._attach_property_layer()only checked name collisions against the Grid's own class and the Cell's__slots__, so creating a property layer namedis_empty,is_full,neighborhood, orpositionsilently overwrote those liveCellproperties instead of raisingValueError. SinceDiscreteSpace.emptiesandGrid.cells_with_capacitydepend on them, this silently corrupted occupancy/capacity queries with no error.Fixes #3839.
Fix
Also reject names colliding with any attribute on
self.cell_klass.__mro__, with an exception for"empty"(whichGrid.__init__itself installs as a property layer overCell.empty).Test plan
test_property_layer_rejects_cell_attribute_collision(parametrized overis_empty/is_full/neighborhood/position) andtest_property_layer_empty_bootstrap_is_not_blockedruff check/ruff format --check: clean