Skip to content

Commit

Permalink
update Agent.__init__ to remove deprecation warning (#2328)
Browse files Browse the repository at this point in the history
  • Loading branch information
quaquel authored Sep 25, 2024
1 parent 191d977 commit cb1c3f7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 40 deletions.
38 changes: 12 additions & 26 deletions mesa/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,39 +46,25 @@ class Agent:
# so, unique_id is unique relative to a model, and counting starts from 1
_ids = defaultdict(functools.partial(itertools.count, 1))

def __init__(self, *args, **kwargs) -> None:
def __init__(self, model: Model, *args, **kwargs) -> None:
"""Create a new agent.
Args:
model (Model): The model instance in which the agent exists.
args: currently ignored, to be fixed in 3.1
kwargs: currently ignored, to be fixed in 3.1
"""
# TODO: Cleanup in future Mesa version (3.1+)
match args:
# Case 1: Only the model is provided. The new correct behavior.
case [model]:
self.model = model
self.unique_id = next(self._ids[model])
# Case 2: Both unique_id and model are provided, deprecated
case [_, model]:
warnings.warn(
"unique ids are assigned automatically to Agents in Mesa 3. The use of custom unique_id is "
"deprecated. Only input a model when calling `super()__init__(model)`. The unique_id inputted is not used.",
DeprecationWarning,
stacklevel=2,
)
self.model = model
self.unique_id = next(self._ids[model])
# Case 3: Anything else, raise an error
case _:
raise ValueError(
"Invalid arguments provided to initialize the Agent. Only input a model: `super()__init__(model)`."
)
args: passed on to super
kwargs: passed on to super
self.pos: Position | None = None
Notes:
to make proper use of python's super, in each class remove the arguments and
keyword arguments you need and pass on the rest to super
"""
super().__init__(*args, **kwargs)

self.model: Model = model
self.model.register_agent(self)
self.unique_id: int = next(self._ids[model])
self.pos: Position | None = None

def remove(self) -> None:
"""Remove and delete the agent from the model."""
Expand Down
15 changes: 1 addition & 14 deletions tests/test_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ def remove_function(agent):
def test_agentset_get():
"""Test AgentSet.get."""
model = Model()
_ = [TestAgent(i, model) for i in range(10)]
_ = [TestAgent(model) for i in range(10)]

agentset = model.agents

Expand Down Expand Up @@ -627,16 +627,3 @@ def custom_agg(values):
assert custom_result[False] == custom_agg(
[agent.value for agent in agents if not agent.even]
)


def test_oldstyle_agent_instantiation():
"""Old behavior of Agent creation with unique_id and model as positional arguments.
Can be removed/updated in the future.
"""
model = Model()
agent = Agent("some weird unique id", model)
assert isinstance(agent.unique_id, int)
assert agent.model == model
assert isinstance(agent.model, Model)
assert agent.unique_id == 1 # test that we ignore unique ID that is passed

0 comments on commit cb1c3f7

Please sign in to comment.