Skip to content

Commit 8ad831a

Browse files
committed
Throw error if Model super class isn't initialized
You should always run super().__init__() after initializing your model class that inherits from Mesa Model.
1 parent eaa5872 commit 8ad831a

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

mesa/agent.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,13 @@ def __init__(self, unique_id: int, model: Model) -> None:
4848
self.pos: Position | None = None
4949

5050
# register agent
51-
self.model.agents_[type(self)][self] = None
51+
try:
52+
self.model.agents_[type(self)][self] = None
53+
except AttributeError:
54+
# model super has not been called
55+
raise RuntimeError(
56+
"The Mesa Model class was not initialized. You must explicitly initialize the Model by calling super().__init__() on initialization."
57+
)
5258

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

0 commit comments

Comments
 (0)