mesa icon indicating copy to clipboard operation
mesa copied to clipboard

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

Open Nithin9585 opened this issue 11 months ago โ€ข 2 comments

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.

Nithin9585 avatar Dec 21 '25 08:12 Nithin9585

๐Ÿ“ CodeRabbit Plan Mode

Generate an implementation plan and prompts that you can use with your favorite coding agent.

  • [ ] Create Plan
Examples

๐Ÿ”— Similar Issues

Related Issues

  • https://github.com/mesa/mesa/issues/2962
  • https://github.com/mesa/mesa/issues/2968
  • https://github.com/mesa/mesa/issues/2811
  • https://github.com/mesa/mesa/issues/2958
๐Ÿ‘ค Suggested Assignees

๐Ÿงช Issue enrichment is currently in open beta.

You can configure auto-planning by selecting labels in the issue_enrichment configuration.

To disable automatic issue enrichment, add the following to your .coderabbit.yaml:

issue_enrichment:
  auto_enrich:
    enabled: false

๐Ÿ’ฌ Have feedback or questions? Drop into our discord or schedule a call!

coderabbitai[bot] avatar Dec 21 '25 08:12 coderabbitai[bot]

good catch!

quaquel avatar Dec 21 '25 18:12 quaquel