Skip to content

Fix AttributeError when accessing Cell.empty before adding/removing agents - #3726

Merged
quaquel merged 4 commits into
mesa:mainfrom
AshNicolus:fix-uninitialized-cell-empty
Jun 8, 2026
Merged

quaquel merged 4 commits into
mesa:mainfrom
AshNicolus:fix-uninitialized-cell-empty

Conversation

@AshNicolus

Copy link
Copy Markdown
Contributor

What

Accessing cell.empty on a freshly created Cell raises:

AttributeError: 'Cell' object has no attribute '_empty'

_empty is declared in Cell.__slots__ and the empty property getter returns
self._empty, but Cell.__init__ never initializes it. The attribute only comes
into existence after the first add_agent/remove_agent call (which assign
self.empty).

This affects every cell that is not a grid cell — i.e. cells in Network
and VoronoiGrid. GridCell is unaffected because the grid attaches an "empty"
property-layer accessor that overrides the attribute.

Reproduction

import random
from mesa.discrete_space import Cell

c = Cell((0,), random=random.Random())
c.empty          # AttributeError: 'Cell' object has no attribute '_empty'

@github-actions

Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 -1.7% [-2.6%, -0.8%] 🔵 -2.0% [-2.3%, -1.7%]
BoltzmannWealth large 🔵 +0.6% [-0.3%, +1.3%] 🔵 -1.6% [-4.9%, +2.0%]
Schelling small 🔵 +0.7% [+0.1%, +1.3%] 🔵 -0.2% [-0.8%, +0.4%]
Schelling large 🔵 +0.8% [+0.2%, +1.5%] 🔵 -1.6% [-3.7%, +0.1%]
WolfSheep small 🔵 +1.6% [+1.0%, +2.2%] 🔵 +0.3% [-3.3%, +4.7%]
WolfSheep large 🔵 -1.3% [-2.6%, +0.1%] 🔵 -3.9% [-7.0%, -1.0%]
SugarscapeG1mt small 🔵 -0.6% [-1.5%, +0.2%] 🔵 -1.0% [-2.8%, +0.9%]
SugarscapeG1mt large 🔵 -5.2% [-7.7%, -2.3%] 🔵 -4.7% [-7.2%, -2.0%]
BoidFlockers small 🔵 -0.2% [-0.6%, +0.2%] 🔵 +0.6% [+0.2%, +1.0%]
BoidFlockers large 🔵 +0.1% [-0.3%, +0.5%] 🔵 +0.3% [-0.0%, +0.5%]

@quaquel

quaquel commented May 31, 2026

Copy link
Copy Markdown
Member

The attribute only comes into existence after the first add_agent/remove_agent call (which assign self.empty).

As you indicate, the empty attribute is controlled via properties that are assigned in the grid class when the empty property layer is created. See here. Moreover, this PR also is related to #3072. Basically, we have 2 mechanisms for tracking empty cells: the _empties attribute and the Grid.empty property layer. As part of the larger overhaul of spaces, we plan to make the empty property layer work across all discrete spaces, not just grids.

Can you elaborate on the use case where you ran into the error that is fixed by this PR.

@AshNicolus

Copy link
Copy Markdown
Contributor Author

Thanks, that context helps. I didn't know about the overhaul or #3072.

To be honest, I didn't hit this in an actual model. I found it while going through the discrete_space code looking at how the different spaces line up. The thing that caught my eye is that Cell.empty is a public property with a getter and setter, and add_agent/remove_agent keep it updated. On grid cells it works because of the property-layer accessor you linked, but on a plain Cell (so any cell in a Network or VoronoiGrid) reading .empty before you've added or removed an agent throws AttributeError, since _empty is in __slots__ but never set in __init__.

import random, networkx as nx
from mesa.discrete_space import Network

net = Network(nx.path_graph(3), random=random.Random(0))
net._cells[0].empty   # AttributeError: 'Cell' object has no attribute '_empty'

So this is really just stopping an existing public property from crashing on non-grid spaces. It doesn't get into the _empties vs property-layer side of things.

Since you're planning to unify the empty property layer across all spaces anyway, I'm fine either way. If the overhaul is going to replace this, feel free to close it. If a small fix is useful in the meantime, this is about as minimal as it gets. Up to you.

@quaquel quaquel left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Let's merge this now so at least the bug is gone.

@quaquel quaquel added the bug Release notes label label Jun 8, 2026
@quaquel
quaquel merged commit 19625da into mesa:main Jun 8, 2026
12 checks passed
EwoutH pushed a commit that referenced this pull request Oct 28, 2026
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.

2 participants