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

update Agent.__init__ to remove deprecation warning #2328

Merged
merged 3 commits into from
Sep 25, 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
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
Loading