Skip to content

Commit

Permalink
Throw error if Model super class isn't initialized
Browse files Browse the repository at this point in the history
You should always run super().__init__() after initializing your model class that inherits from Mesa Model.
  • Loading branch information
EwoutH committed Aug 18, 2024
1 parent eaa5872 commit 8ad831a
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion mesa/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,13 @@ def __init__(self, unique_id: int, model: Model) -> None:
self.pos: Position | None = None

# register agent
self.model.agents_[type(self)][self] = None
try:
self.model.agents_[type(self)][self] = None
except AttributeError:
# model super has not been called
raise RuntimeError(
"The Mesa Model class was not initialized. You must explicitly initialize the Model by calling super().__init__() on initialization."
)

def remove(self) -> None:
"""Remove and delete the agent from the model."""
Expand Down

0 comments on commit 8ad831a

Please sign in to comment.