From ce391cac77b769bccbb62629bfee062f3598fa25 Mon Sep 17 00:00:00 2001 From: Ewout ter Hoeven Date: Wed, 13 Nov 2024 17:02:16 +0100 Subject: [PATCH] cell_space: Allow CellCollection to be empty Sometimes it's useful to have any empty cell collection in the cell space, which can happen after selection with no cells that meet the selection requirements. However, self._capacity would give an error, because there were no cell to derive the capacity from. This PR resolves that error. --- mesa/experimental/cell_space/cell_collection.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/mesa/experimental/cell_space/cell_collection.py b/mesa/experimental/cell_space/cell_collection.py index ead67021532..ca49a472ed0 100644 --- a/mesa/experimental/cell_space/cell_collection.py +++ b/mesa/experimental/cell_space/cell_collection.py @@ -48,8 +48,10 @@ def __init__( else: self._cells = {cell: cell.agents for cell in cells} - # - self._capacity: int = next(iter(self._cells.keys())).capacity + # Get capacity from first cell if collection is not empty + self._capacity: int | None = ( + next(iter(self._cells.keys())).capacity if self._cells else None + ) if random is None: warnings.warn(