-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Description
Describe the bug
The Cell.is_full property compares an integer to None when checking if a cell with infinite capacity is full. This happens because cells default to capacity=None for infinite capacity, and the code does
len(self.agents) == self.capacity
, which evaluates to something like 100 == None. While this returns the correct result (False), it's a type comparison error that violates type safety.
Location:
mesa/discrete_space/cell.py
lines 135-137
Expected behavior
The method should explicitly handle the None case instead of relying on int-to-None comparison. When capacity=None, it should return False without comparing types.
To Reproduce
from mesa.discrete_space import Cell
from mesa import Model
from mesa.discrete_space import CellAgent
# cell with infinite capacity
cell = Cell((0, 0), capacity=None)
# Add agents
model = Model()
for i in range(100):
agent = CellAgent(model)
agent._mesa_cell = cell
cell._agents.append(agent)
# This does: len(cell.agents) == cell.capacity
# Which is: 100 == None
print(f"is_full: {cell.is_full}")
Additional context
Currently works by accident since int == None returns False, but should be fixed for code clarity and type safety. Also changed == to >= in the fix to handle edge cases where agents might exceed capacity.