Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Require Mesa models to be initialized with super().__init__() #2218

Merged
merged 4 commits into from
Aug 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 4 additions & 14 deletions mesa/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@
import contextlib
import copy
import operator
import warnings
import weakref
from collections import defaultdict
from collections.abc import Callable, Iterable, Iterator, MutableSet, Sequence
from random import Random

Expand Down Expand Up @@ -52,17 +50,11 @@
# register agent
try:
self.model.agents_[type(self)][self] = None
except AttributeError:
except AttributeError as err:

Check warning on line 53 in mesa/agent.py

View check run for this annotation

Codecov / codecov/patch

mesa/agent.py#L53

Added line #L53 was not covered by tests
# model super has not been called
self.model.agents_ = defaultdict(dict)
self.model.agents_[type(self)][self] = None
self.model.agentset_experimental_warning_given = False

warnings.warn(
"The Mesa Model class was not initialized. In the future, you need to explicitly initialize the Model by calling super().__init__() on initialization.",
FutureWarning,
stacklevel=2,
)
raise RuntimeError(

Check warning on line 55 in mesa/agent.py

View check run for this annotation

Codecov / codecov/patch

mesa/agent.py#L55

Added line #L55 was not covered by tests
"The Mesa Model class was not initialized. You must explicitly initialize the Model by calling super().__init__() on initialization."
) from err

def remove(self) -> None:
"""Remove and delete the agent from the model."""
Expand Down Expand Up @@ -100,8 +92,6 @@
which means that agents not referenced elsewhere in the program may be automatically removed from the AgentSet.
"""

agentset_experimental_warning_given = False

def __init__(self, agents: Iterable[Agent], model: Model):
"""
Initializes the AgentSet with a collection of agents and a reference to the model.
Expand Down
6 changes: 3 additions & 3 deletions mesa/space.py
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ class PropertyLayer:
aggregate_property(operation): Performs an aggregate operation over all cells.
"""

agentset_experimental_warning_given = False
propertylayer_experimental_warning_given = False

def __init__(
self, name: str, width: int, height: int, default_value, dtype=np.float64
Expand Down Expand Up @@ -633,14 +633,14 @@ def __init__(

self.data = np.full((width, height), default_value, dtype=dtype)

if not self.__class__.agentset_experimental_warning_given:
if not self.__class__.propertylayer_experimental_warning_given:
warnings.warn(
"The new PropertyLayer and _PropertyGrid classes experimental. It may be changed or removed in any and all future releases, including patch releases.\n"
"We would love to hear what you think about this new feature. If you have any thoughts, share them with us here: https://github.com/projectmesa/mesa/discussions/1932",
FutureWarning,
stacklevel=2,
)
self.__class__.agentset_experimental_warning_given = True
self.__class__.propertylayer_experimental_warning_given = True

def set_cell(self, position: Coordinate, value):
"""
Expand Down