Skip to content

Refactor Cell.__getstate__ to use modern super() implementation#3086

Merged
quaquel merged 2 commits intomesa:mainfrom
falloficarus22:refactor/cell-getstate-modernization
Jan 7, 2026
Merged

Refactor Cell.__getstate__ to use modern super() implementation#3086
quaquel merged 2 commits intomesa:mainfrom
falloficarus22:refactor/cell-getstate-modernization

Conversation

@falloficarus22
Copy link
Copy Markdown
Contributor

@falloficarus22 falloficarus22 commented Jan 7, 2026

Summary

This PR refactors the Cell class in mesa/discrete_space/cell.py to use super().__getstate__() for serialization, replacing a legacy manual dictionary construction.

Motive

The previous implementation of getstate contained a manual workaround for older Python versions, explicitly marked with a FIXME comment: # fixme, once we shift to 3.11, replace this with super. __getstate__.

Since the project now requires Python>=3.12, we can safely modernize this method to use the standard super() implementation, reducing technical debt and simplifying the codebase.

Implementation

  • Removed the manual construction of the state tuple using self.__dict__ and self.__slots__.
  • Adopted super().__getstate__() to retrieve the default state.
  • Preserved the critical logic that clears the connections dictionary in the serialized state to prevent infinite recursion during deepcopy/pickling operations.

Usage Examples

This is a refactor, so the external behavior remains identical. The Cell objects continue to support pickling and deepcopying without recursion errors.

import pickle
import copy
from mesa.discrete_space.cell import Cell

# Setup
c1 = Cell((0, 0))
c2 = Cell((0, 1))
c1.connect(c2)

# Pickling works as expected (connections are cleared to avoid recursion)
dumped = pickle.dumps(c1)
loaded = pickle.loads(dumped)
assert loaded.connections == {}

# Deepcopy works as expected
copied = copy.deepcopy(c1)
assert copied.connections == {}

Additional Notes

This change addresses the specific technical debt item identified in the source code comments. No behavior changes are expected

Closes #3084

The Cell class serialization was relying on a manual dictionary
construction as a workaround for older Python versions. Now that
the project requires Python>=3.12, we can modernize this.

This commit replaces the manual __dict__ manipulation with
super().__getstate__() while preserving the logic that clears
connections to prevent infinte recursion during the deepcopy operations.
@github-actions
Copy link
Copy Markdown

github-actions bot commented Jan 7, 2026

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 -0.6% [-1.1%, -0.3%] 🔵 -0.2% [-0.4%, -0.0%]
BoltzmannWealth large 🔵 +0.1% [-0.3%, +0.6%] 🔵 -2.7% [-4.4%, -0.8%]
Schelling small 🔵 -1.1% [-2.1%, +0.0%] 🟢 -5.9% [-7.2%, -4.5%]
Schelling large 🔵 -0.6% [-1.0%, -0.2%] 🔵 -4.7% [-8.6%, -0.3%]
WolfSheep small 🔵 -2.8% [-3.2%, -2.3%] 🔵 -3.1% [-3.5%, -2.8%]
WolfSheep large 🔵 -4.2% [-7.7%, -1.3%] 🟢 -6.4% [-8.6%, -4.2%]
BoidFlockers small 🔵 +0.6% [+0.2%, +1.1%] 🔵 +0.9% [+0.6%, +1.1%]
BoidFlockers large 🔵 -0.4% [-1.1%, +0.2%] 🔵 +0.0% [-0.3%, +0.4%]

@quaquel quaquel added the ignore-for-release PRs that aren't included in the release notes label Jan 7, 2026
@quaquel quaquel merged commit 58b28d8 into mesa:main Jan 7, 2026
12 of 13 checks passed
@falloficarus22 falloficarus22 deleted the refactor/cell-getstate-modernization branch January 7, 2026 17:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ignore-for-release PRs that aren't included in the release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Refactor Cell.__getstate__ to use modern super() implementation

2 participants