Skip to content

Disallow removing the built-in 'empty' property layer in Grid (#3981) - #3982

Merged
quaquel merged 1 commit into
mesa:mainfrom
Dilraj07:fix/protect-empty-property-layer
Oct 24, 2026
Merged

quaquel merged 1 commit into
mesa:mainfrom
Dilraj07:fix/protect-empty-property-layer

Conversation

@Dilraj07

Copy link
Copy Markdown
Contributor
  • Bug fix

Fixes #3981

Summary

Disallows removing the built-in "empty" property layer in Grid.remove_property_layer(), raising a ValueError.

Problem

In Grid.__init__, Mesa initializes the internal "empty" property layer (self.create_property_layer("empty", default_value=True, dtype=bool)), which backs grid.empty, cell.empty, and grid.select_random_empty_cell().

remove_property_layer() lacked a check preventing the deletion of this built-in layer. When remove_property_layer("empty") was called, it deleted grid.empty and cell_klass.empty, corrupting grid invariants and causing select_random_empty_cell() to fail with KeyError: 'empty' when full.

Solution

  • Added an eager check in Grid.remove_property_layer():
    if name == "empty":
        raise ValueError("Cannot remove the built-in 'empty' property layer.")
  • Documented Raises: ValueError in Grid.remove_property_layer() docstring.
  • Added regression test test_property_layer_cannot_remove_empty() in tests/discrete_space/test_discrete_space.py.

Tests

  • pytest tests/discrete_space/test_discrete_space.py: All 115 tests passed.
  • ruff check .: Passed with zero warnings.
  • ruff format --check .: Passed.

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

Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 -3.4% [-3.8%, -2.9%] 🔵 -1.2% [-1.4%, -1.0%]
BoltzmannWealth large 🔵 -2.0% [-2.2%, -1.7%] 🟢 -7.2% [-9.0%, -5.7%]
Schelling small 🟢 -4.9% [-5.5%, -4.4%] 🔵 -2.6% [-3.5%, -1.8%]
Schelling large 🔵 -1.8% [-2.4%, -1.2%] 🔵 -6.1% [-9.8%, -2.5%]
WolfSheep small 🔵 -1.0% [-1.4%, -0.6%] 🔵 +3.4% [-0.9%, +7.6%]
WolfSheep large 🔵 -1.6% [-2.4%, -0.7%] 🔵 -3.3% [-5.3%, -1.7%]
SugarscapeG1mt small 🔵 +0.7% [+0.5%, +0.9%] 🔵 +0.4% [-1.3%, +2.1%]
SugarscapeG1mt large 🔵 -0.9% [-1.6%, -0.1%] 🔵 +1.0% [-0.7%, +3.1%]
BoidFlockers small 🔵 -2.7% [-3.0%, -2.4%] 🟢 -3.6% [-4.0%, -3.2%]
BoidFlockers large 🔵 -3.4% [-4.0%, -2.9%] 🔵 -3.1% [-3.4%, -2.8%]
MultiLevelAllianceModel small 🔵 -2.9% [-4.2%, -1.7%] 🔵 +0.5% [+0.1%, +0.8%]
MultiLevelAllianceModel large 🔵 +1.2% [+0.9%, +1.4%] 🔵 +1.2% [+0.8%, +1.6%]

@quaquel quaquel added the bug Release notes label label Oct 24, 2026
@quaquel
quaquel merged commit 65dc82d into mesa:main Oct 24, 2026
16 of 17 checks passed
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.

Grid.remove_property_layer("empty") silently removes internal layer and corrupts grid state

2 participants