diff --git a/mesa/agent.py b/mesa/agent.py index ec98efaad08..4d28f4742b1 100644 --- a/mesa/agent.py +++ b/mesa/agent.py @@ -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.""" diff --git a/tests/test_agent.py b/tests/test_agent.py index a0837de71a1..ce8412f8015 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 @@ -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