Skip to content

Fix Grid property layer instance attribute collisions - #3954

Merged
quaquel merged 1 commit into
mesa:mainfrom
KaguraTart:fix/grid-property-layer-attribute-collisions
Oct 22, 2026
Merged

quaquel merged 1 commit into
mesa:mainfrom
KaguraTart:fix/grid-property-layer-attribute-collisions

Conversation

@KaguraTart

Copy link
Copy Markdown
Contributor

Summary

Prevent property layers from overwriting existing Grid instance attributes, such as dimensions and torus.

Also remove the corresponding Grid attribute when deleting a property layer, allowing its name to be reused under the new collision check.

Bug / Issue

Related background:

Issue #3839 was resolved by PR #3840, which added protection against overwriting attributes defined on Cell classes, such as is_empty, is_full, and position.

This PR addresses a separate collision involving attributes stored on the Grid instance itself:

Attribute location Examples Protection
Grid class hierarchy width, height Already checked
Cell class hierarchy is_empty, is_full, position Added by #3840
Grid instance dictionary dimensions, torus, _cells Added by this PR

Grid._attach_property_layer() checked the Grid class hierarchy and Cell attributes, but omitted self.__dict__. Consequently, setattr(self, name, array) could replace existing Grid state with a NumPy array.

The following reproduction was confirmed on the pre-fix main baseline at 2b2c9251, which already includes the merged PR #3840:

from random import Random

from mesa.discrete_space import OrthogonalMooreGrid

grid = OrthogonalMooreGrid((3, 3), random=Random(42))
grid.create_property_layer("dimensions", default_value=0)
grid.find_nearest_cell([1.2, 1.2])

Creating the layer succeeds, but the subsequent query raises a NumPy ambiguous-truth-value ValueError because grid.dimensions has been replaced by the layer array.

A layer named torus similarly replaces the boolean configuration with an array, causing the if self.torus check to fail. Both create_property_layer() and add_property_layer() are affected.

Expected behavior: reject a conflicting name with a clear ValueError before modifying the layer registry, Grid attributes, or Cell accessors, leaving the grid usable.

Implementation

  • Check the Grid instance dictionary in addition to attributes defined on the Grid class hierarchy.
  • Check duplicate layer names first, preserving the existing already exists error.
  • Remove the corresponding Grid attribute in remove_property_layer(). This prevents a stale attribute from blocking recreation of a previously removed layer.
  • Preserve the existing Cell collision checks introduced by Fix Grid.create_property_layer() silently overwriting Cell attributes #3840 and the internal empty layer initialization.

Testing

Added 16 parameterized regression cases covering:

  • Six existing Grid instance attributes through both layer-attachment entry points.
  • Preservation of the original attributes, layer registries, spatial lookup, and occupancy behavior after rejection.
  • Preservation of the original array and read-only Cell accessor when a duplicate layer is rejected.
  • Complete removal followed by successful recreation of a writable layer with the same name.

Local validation on macOS with Python 3.14.5:

  • Against the pre-fix baseline: 14 regression cases failed, 2 passed.
  • After the fix: all 16 regression cases passed.
  • Discrete-space tests: 123 passed.
  • Full repository test suite, including browser visualization tests: 891 passed, 0 failed, 0 skipped.
  • Ruff 0.16.10 lint and formatting checks passed for both changed files.
  • git diff --check passed.

Additional Notes

Issue #3839 is already closed and PR #3840 is already merged. They are referenced as background for the Cell-level protection that remains in place. This PR adds protection for the distinct Grid-instance collision demonstrated above.

Copilot AI balanced review requested due to automatic review settings October 19, 2026 15:27

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@github-actions github-actions Bot added the testing Release notes label label Oct 19, 2026
@github-actions

Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 +0.1% [-0.4%, +0.7%] 🔵 +2.1% [+1.9%, +2.3%]
BoltzmannWealth large 🔵 -1.0% [-1.6%, -0.3%] 🔵 +0.8% [-1.0%, +2.9%]
Schelling small 🔵 -0.2% [-0.7%, +0.3%] 🔵 -0.0% [-0.8%, +0.7%]
Schelling large 🔵 +1.4% [+0.5%, +2.2%] 🔴 +5.2% [+3.4%, +6.7%]
WolfSheep small 🔵 +2.0% [+1.5%, +2.5%] 🔵 +2.0% [-1.8%, +6.1%]
WolfSheep large 🔵 +1.3% [+0.3%, +2.3%] 🔴 +6.1% [+3.8%, +8.2%]
SugarscapeG1mt small 🔵 -0.1% [-0.5%, +0.4%] 🔵 +2.1% [+0.7%, +3.5%]
SugarscapeG1mt large 🔵 -1.3% [-2.2%, -0.4%] 🔵 +1.7% [+0.6%, +2.6%]
BoidFlockers small 🔵 +1.4% [+1.0%, +1.8%] 🔵 +1.8% [+1.3%, +2.2%]
BoidFlockers large 🔵 +1.1% [+0.8%, +1.4%] 🔵 +0.5% [+0.3%, +0.8%]
MultiLevelAllianceModel small 🔵 -2.6% [-4.1%, -1.0%] 🔵 -0.5% [-0.8%, -0.2%]
MultiLevelAllianceModel large 🔵 -2.6% [-3.2%, -2.0%] 🔵 -4.1% [-5.5%, -2.6%]

@quaquel quaquel added the bug Release notes label label Oct 22, 2026
@quaquel
quaquel merged commit c7eb9b7 into mesa:main Oct 22, 2026
16 of 18 checks passed
@KaguraTart
KaguraTart deleted the fix/grid-property-layer-attribute-collisions branch October 22, 2026 11:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Release notes label testing Release notes label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants