Skip to content

Commit cb1c3f7

Browse files
authored
update Agent.__init__ to remove deprecation warning (#2328)
1 parent 191d977 commit cb1c3f7

File tree

2 files changed

+13
-40
lines changed

2 files changed

+13
-40
lines changed

mesa/agent.py

+12-26
Original file line numberDiff line numberDiff line change
@@ -46,39 +46,25 @@ class Agent:
4646
# so, unique_id is unique relative to a model, and counting starts from 1
4747
_ids = defaultdict(functools.partial(itertools.count, 1))
4848

49-
def __init__(self, *args, **kwargs) -> None:
49+
def __init__(self, model: Model, *args, **kwargs) -> None:
5050
"""Create a new agent.
5151
5252
Args:
5353
model (Model): The model instance in which the agent exists.
54-
args: currently ignored, to be fixed in 3.1
55-
kwargs: currently ignored, to be fixed in 3.1
56-
"""
57-
# TODO: Cleanup in future Mesa version (3.1+)
58-
match args:
59-
# Case 1: Only the model is provided. The new correct behavior.
60-
case [model]:
61-
self.model = model
62-
self.unique_id = next(self._ids[model])
63-
# Case 2: Both unique_id and model are provided, deprecated
64-
case [_, model]:
65-
warnings.warn(
66-
"unique ids are assigned automatically to Agents in Mesa 3. The use of custom unique_id is "
67-
"deprecated. Only input a model when calling `super()__init__(model)`. The unique_id inputted is not used.",
68-
DeprecationWarning,
69-
stacklevel=2,
70-
)
71-
self.model = model
72-
self.unique_id = next(self._ids[model])
73-
# Case 3: Anything else, raise an error
74-
case _:
75-
raise ValueError(
76-
"Invalid arguments provided to initialize the Agent. Only input a model: `super()__init__(model)`."
77-
)
54+
args: passed on to super
55+
kwargs: passed on to super
7856
79-
self.pos: Position | None = None
57+
Notes:
58+
to make proper use of python's super, in each class remove the arguments and
59+
keyword arguments you need and pass on the rest to super
8060
61+
"""
62+
super().__init__(*args, **kwargs)
63+
64+
self.model: Model = model
8165
self.model.register_agent(self)
66+
self.unique_id: int = next(self._ids[model])
67+
self.pos: Position | None = None
8268

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

tests/test_agent.py

+1-14
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ def remove_function(agent):
301301
def test_agentset_get():
302302
"""Test AgentSet.get."""
303303
model = Model()
304-
_ = [TestAgent(i, model) for i in range(10)]
304+
_ = [TestAgent(model) for i in range(10)]
305305

306306
agentset = model.agents
307307

@@ -627,16 +627,3 @@ def custom_agg(values):
627627
assert custom_result[False] == custom_agg(
628628
[agent.value for agent in agents if not agent.even]
629629
)
630-
631-
632-
def test_oldstyle_agent_instantiation():
633-
"""Old behavior of Agent creation with unique_id and model as positional arguments.
634-
635-
Can be removed/updated in the future.
636-
"""
637-
model = Model()
638-
agent = Agent("some weird unique id", model)
639-
assert isinstance(agent.unique_id, int)
640-
assert agent.model == model
641-
assert isinstance(agent.model, Model)
642-
assert agent.unique_id == 1 # test that we ignore unique ID that is passed

0 commit comments

Comments
 (0)