From 3348c9726d7a43b199f978a2535469684008ecfc Mon Sep 17 00:00:00 2001 From: Jan Kwakkel Date: Wed, 25 Sep 2024 16:47:06 +0200 Subject: [PATCH 1/2] update Agent.__init__ to remove deprecation warning --- mesa/agent.py | 37 ++++++++++++------------------------- tests/test_agent.py | 13 +------------ 2 files changed, 13 insertions(+), 37 deletions(-) diff --git a/mesa/agent.py b/mesa/agent.py index ec98efaad08..76822f3f68b 100644 --- a/mesa/agent.py +++ b/mesa/agent.py @@ -46,39 +46,26 @@ 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 + args: passed on to super + kwargs: passed on to super + + 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 + """ - # 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)`." - ) + 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 - self.model.register_agent(self) def remove(self) -> None: """Remove and delete the agent from the model.""" diff --git a/tests/test_agent.py b/tests/test_agent.py index a0837de71a1..469d23b32a7 100644 --- a/tests/test_agent.py +++ b/tests/test_agent.py @@ -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 @@ -629,14 +629,3 @@ def custom_agg(values): ) -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 From 51b984f160477601187c043ac00e7f901fb57a02 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 25 Sep 2024 14:49:15 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- mesa/agent.py | 1 - tests/test_agent.py | 2 -- 2 files changed, 3 deletions(-) diff --git a/mesa/agent.py b/mesa/agent.py index 76822f3f68b..4d28f4742b1 100644 --- a/mesa/agent.py +++ b/mesa/agent.py @@ -66,7 +66,6 @@ def __init__(self, model: Model, *args, **kwargs) -> None: 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.""" with contextlib.suppress(KeyError): diff --git a/tests/test_agent.py b/tests/test_agent.py index 469d23b32a7..ce8412f8015 100644 --- a/tests/test_agent.py +++ b/tests/test_agent.py @@ -627,5 +627,3 @@ def custom_agg(values): assert custom_result[False] == custom_agg( [agent.value for agent in agents if not agent.even] ) - -