cell attribute in CellAgent and FixedAgent
Thecell property defined in HasCell is not available in the signature of the subclass of CellAgent or FixedAgent
Reason - This is because CellAgent, FixedAgent or HasCell doesn't initialise the property
Effect - This causes AttributeError or TypeError when the subclass of those classes doesn't define the cell attribute in their signature or consume it.
This feels counterintuitive as both CellAgent and FixedAgent says they have cell attribute and when a subclass tries to access it, it throws error.
My doubt is, was this intentional? If it's not, I can make a PR for the solution (only after the issue is aligned).
To reproduce the error:
from mesa import Model
from mesa.discrete_space import Cell, CellAgent, Network
import networkx as nx
class MockAgent(CellAgent):
"""Mock Agent with no custom step"""
def __init__(self, model, **kwargs):
# Causes TypeError when the call hits the python object as cell was never consumed downstream
# MRO: CellAgent -> Agent -> HasAgent -> BaseMovement -> object
super().__init__(model, **kwargs)
# This works as the user consumes `cell` but counterintuitive
"""
def __init__(self, model, cell):
super().__init__(model)
self.cell = cell # @property in HasCell
"""
class MockModel(Model):
"""Mock model with no custom step"""
N = 2
M = MockModel()
main_network = nx.complete_graph(N)
main_layout = nx.random_layout(main_network)
grid = Network(
G = main_network,
capacity = N,
layout = main_layout,
)
agents = list(MockAgent.create_agents(
model = M,
n = N,
cell = list(grid.all_cells)
))
I caught this behaviour while developing a model in my repo
@quaquel, I'd appreciate your attention to this behaviour
Should CellAgent and FixedAgent accept a cell keyword and assign self.cell during construction, or should subclasses keep consuming cell themselves and the docs clarify that requirement?
I am not sure. First, any subclass of CellAgent or FixedAgent can access self.cell. Is should just return None if it has not been set. So
when a subclass tries to access it, it throws error.
does not seem correct to me.
Including cell as an argument or keyword argument in CellAgent and FixedAgent would force a usage pattern that is not always convenient. Sometimes, it is easier to just create a bunch of agents first and then assign them all to their respective cells. In other cases, you can handle it in the init of your own subclass. However, I can also see your point about it being potentially confusing, and 8 out 10 times in my own usage, I do pass cell to the init of my subclass.
I ran the reproducer to check the two readings in this thread.
@quaquel is right that reading the attribute works. HasCell declares _mesa_cell as a class attribute defaulting to None, so a subclass that never sets it still gets None back:
class Plain(CellAgent): pass
class PlainFixed(FixedAgent): pass
Plain(model).cell -> None
PlainFixed(model).cell -> None
So the "throws error" description is not accurate for reads, and the title is a little misleading.
What fails is passing it:
CellAgent(model, cell=c)
TypeError: object.__init__() takes exactly one argument (the instance to initialize)
and the same through a subclass that forwards **kwargs, which is the case in the report. inspect.signature(CellAgent.__init__) is ['self', 'model', 'args', 'kwargs'], so the signature accepts cell= and then forwards it up the MRO to object.__init__. The error names object rather than cell, which is why this reads as a missing attribute instead of an unconsumed keyword.
On the concern that accepting it would force a usage pattern: making it optional forces nothing, and the repo already wants it. Five agent classes across four bundled examples repeat the same three lines, declaring cell, calling super().__init__(model), then assigning self.cell = cell:
examples/advanced/wolf_sheep/agents.py, Animal at 7, 19, 23 and GrassPatch at 129, 138, 141examples/advanced/pd_grid/agents.pyat 7, 16, 18examples/basic/boltzmann_wealth_model/agents.pyat 14, 20, 21examples/basic/conways_game_of_life/agents.pyat 18, 20, 21
I have a patch adding cell as an optional keyword-only argument to CellAgent and FixedAgent, assigned after Agent.__init__ returns. That ordering is needed because Agent.__init__ calls super() before it sets self.model and calls register_agent, so assigning any earlier would add an unregistered agent to a cell. Omitting cell is unchanged, and subclasses that consume it themselves are unaffected.
The new test fails without the change with the TypeError above and passes with it. Full suite 836 passed, ruff clean.
I can open that if it is the direction you want, or drop it and document the requirement instead.