Repository navigation
Fix AttributeError when accessing Cell.empty before adding/removing agents - #3726
Conversation
|
Performance benchmarks:
|
As you indicate, the Can you elaborate on the use case where you ran into the error that is fixed by this PR. |
|
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 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 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
left a comment
There was a problem hiding this comment.
Let's merge this now so at least the bug is gone.
What
Accessing
cell.emptyon a freshly createdCellraises:_emptyis declared inCell.__slots__and theemptyproperty getter returnsself._empty, butCell.__init__never initializes it. The attribute only comesinto existence after the first
add_agent/remove_agentcall (which assignself.empty).This affects every cell that is not a grid cell — i.e. cells in
Networkand
VoronoiGrid.GridCellis unaffected because the grid attaches an"empty"property-layer accessor that overrides the attribute.
Reproduction