Skip to content

Fix Grid.create_property_layer() silently overwriting Cell attributes - #3840

Merged
quaquel merged 3 commits into
mesa:mainfrom
0xnithinmys:fix-property-layer-cell-attribute-collision
Sep 21, 2026
Merged

quaquel merged 3 commits into
mesa:mainfrom
0xnithinmys:fix-property-layer-cell-attribute-collision

Conversation

@0xnithinmys

Copy link
Copy Markdown
Contributor

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 named is_empty, is_full, neighborhood, or position silently overwrote those live Cell properties instead of raising ValueError. Since DiscreteSpace.empties and Grid.cells_with_capacity depend 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" (which Grid.__init__ itself installs as a property layer over Cell.empty).

Test plan

  • Added test_property_layer_rejects_cell_attribute_collision (parametrized over is_empty/is_full/neighborhood/position) and test_property_layer_empty_bootstrap_is_not_blocked
  • Full suite passes: 805 passed
  • ruff check / ruff format --check: clean

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
@github-actions github-actions Bot added the testing Release notes label label Sep 6, 2026
@github-actions

github-actions Bot commented Sep 6, 2026

Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 -2.5% [-2.9%, -2.0%] 🔵 -1.9% [-2.1%, -1.7%]
BoltzmannWealth large 🔵 -2.4% [-3.0%, -1.8%] 🟢 -4.9% [-6.9%, -3.1%]
Schelling small 🔵 -0.8% [-1.2%, -0.4%] 🔵 -0.9% [-1.6%, -0.2%]
Schelling large 🔵 -0.6% [-1.3%, -0.0%] 🔵 -1.1% [-2.6%, +0.3%]
WolfSheep small 🔵 -0.5% [-1.0%, -0.1%] 🔵 -2.4% [-5.9%, +1.3%]
WolfSheep large 🔵 -0.5% [-0.9%, -0.1%] 🔵 +0.4% [-1.5%, +2.2%]
SugarscapeG1mt small 🔵 -0.5% [-1.0%, +0.1%] 🔵 -0.4% [-2.0%, +1.1%]
SugarscapeG1mt large 🔵 +0.4% [-0.7%, +1.4%] 🔵 +0.4% [-1.3%, +2.3%]
BoidFlockers small 🔵 +0.3% [+0.1%, +0.6%] 🔵 +0.4% [+0.0%, +0.8%]
BoidFlockers large 🔵 +0.4% [+0.0%, +0.8%] 🔵 +0.2% [-0.0%, +0.4%]
MultiLevelAllianceModel small 🔵 -0.7% [-2.2%, +0.8%] 🔵 +0.4% [+0.1%, +0.7%]
MultiLevelAllianceModel large 🔵 +1.1% [+0.1%, +2.0%] 🔵 +0.6% [-1.1%, +2.0%]

@dietcoke-17 dietcoke-17 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread mesa/discrete_space/grid.py Resolved
Comment thread mesa/discrete_space/grid.py Resolved
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.
Comment thread mesa/discrete_space/grid.py Resolved
@0xnithinmys
0xnithinmys force-pushed the fix-property-layer-cell-attribute-collision branch from 8f553ad to 1800ad0 Compare September 7, 2026 13:05
@quaquel
quaquel merged commit 45c25fe into mesa:main Sep 21, 2026
15 of 16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

testing Release notes label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Property layers can break

3 participants