Skip to content

Preserve read_only property layers across Grid pickling and deepcopy - #3731

Merged
quaquel merged 4 commits into
mesa:mainfrom
AshNicolus:fix-readonly-property-layer-pickle
Jun 8, 2026
Merged

quaquel merged 4 commits into
mesa:mainfrom
AshNicolus:fix-readonly-property-layer-pickle

Conversation

@AshNicolus

Copy link
Copy Markdown
Contributor

Closes #3730

What

Read-only property layers on a Grid became writable again after the grid was
pickled (or deepcopied). Grid.__setstate__ rebuilt every property layer with both
a getter and a setter, ignoring the original read_only flag, so the restriction
that _attach_property_layer set up was silently lost on the round trip.

Reproduction

import pickle
from mesa import Model
from mesa.discrete_space.grid import OrthogonalMooreGrid

grid = OrthogonalMooreGrid(dimensions=(3, 3), random=Model().random)
grid.create_property_layer("protected", default_value=0.0, read_only=True)

grid2 = pickle.loads(pickle.dumps(grid))
grid2._cells[(0, 0)].protected = 99.0   # expected AttributeError, but it succeeded

Fix

The read-only status is now carried through the pickle round trip:

  • __getstate__ records the set of read-only layers (those whose cell-class
    accessor has fset is None).
  • __setstate__ rebuilds each accessor as getter-only when the layer was read-only,
    and getter+setter otherwise, mirroring _attach_property_layer.

The change is confined to the two pickle methods. copy.deepcopy uses the same
methods, so it is fixed too.

Test

Added test_read_only_property_layer_survives_pickle_and_deepcopy: after both
deepcopy and pickle round trips, a read-only layer still raises AttributeError
on write, while a read-write layer remains writable.

Full tests/discrete_space/ suite passes under -Werror; ruff clean.

@github-actions

github-actions Bot commented Jun 4, 2026

Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 -1.7% [-2.6%, -0.8%] 🔵 -0.6% [-0.7%, -0.4%]
BoltzmannWealth large 🔵 -1.6% [-2.2%, -1.0%] 🟢 -5.5% [-7.9%, -3.4%]
Schelling small 🔵 -1.8% [-2.4%, -1.2%] 🔵 -1.1% [-1.9%, -0.4%]
Schelling large 🔵 +0.3% [-0.3%, +0.9%] 🔵 -0.3% [-1.3%, +0.6%]
WolfSheep small 🔵 -1.1% [-1.4%, -0.8%] 🔵 +2.8% [-1.2%, +6.7%]
WolfSheep large 🔵 -1.0% [-1.2%, -0.7%] 🔵 +1.9% [+0.9%, +2.8%]
SugarscapeG1mt small 🔵 -1.7% [-2.0%, -1.5%] 🔵 +0.8% [-0.8%, +2.5%]
SugarscapeG1mt large 🔵 -1.5% [-2.2%, -0.8%] 🔵 -1.0% [-2.8%, +0.6%]
BoidFlockers small 🔵 +0.6% [+0.3%, +0.9%] 🔵 +1.4% [+0.9%, +1.7%]
BoidFlockers large 🔵 +1.8% [+1.4%, +2.2%] 🔵 +0.9% [+0.7%, +1.1%]

@quaquel

quaquel commented Jun 4, 2026

Copy link
Copy Markdown
Member

The fix seems fine. However, I am wondering whether it would not be a good idea to store on the space which layers are read only and which ones are not.

@codebreaker32

Copy link
Copy Markdown
Collaborator

Thanks @AshNicolus for the PR and bug report. I agree with @quaquel that making the state explicit feels cleaner than inferring it via .fset reflection during __getstate__. We can just initialize a self._read_only_layers = set() in Grid.__init__, add the layer name to it when read_only=True is passed, and then read from that set in __setstate__

@AshNicolus

Copy link
Copy Markdown
Contributor Author

Good call, done. Switched to an explicit self._read_only_layers set: initialized in Grid.__init__, the layer name is added in _attach_property_layer when read_only=True, discarded in remove_property_layer, and read in __setstate__. __getstate__ is back to its original form since the set is part of __dict__ and pickles along with everything else. No more .fset reflection.

@codebreaker32 codebreaker32 added the bug Release notes label label Jun 5, 2026

@codebreaker32 codebreaker32 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM!

@dietcoke-17

Copy link
Copy Markdown
Contributor

hey @AshNicolus 1 minor Q: do we need any additional safeguards in setstate if _read_only_layers contains a layer name that is no longer present in property_layers, or is that state impossible given the current lifecycle of property layers?

@AshNicolus

AshNicolus commented Jun 8, 2026 •

Copy link
Copy Markdown
Contributor Author

@dietcoke-17 Good question. No extra safeguard is needed, for two reasons:

  1. The lifecycle keeps _read_only_layers a subset of property_layers. remove_property_layer discards the name from _read_only_layers as well, so the two never drift apart.

  2. Even if a stale name were somehow present, __setstate__ is driven by property_layers, not by _read_only_layers. The loop iterates self.property_layers.items() and only does a membership check (if name in read_only_layers), so a name in _read_only_layers that isn't in property_layers is simply never looked at. No KeyError, no effect.

I confirmed this: adding a bogus name to _read_only_layers and then pickling round-trips fine, the bogus name does not appear in property_layers, and read-write layers stay writable.

@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.

cool lgtm me too

@quaquel
quaquel merged commit 3c29dd8 into mesa:main Jun 8, 2026
11 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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Grid property layers lose read_only after pickling

4 participants