Thanks to visit codestin.com
Credit goes to Github.com

Skip to content

Bug: Cell.is_full type comparison error with capacity=None #2980

@Nithin9585

Description

@Nithin9585

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

Image

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions