-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Deprecated AgentSet indexing in meta_agents.extract_class function #3240
Description
Describe the bug
The extract_class function in mesa/experimental/meta_agents/meta_agent.py (line 133) still uses deprecated AgentSet.__getitem__ syntax, which was deprecated in #3208 and will be removed in Mesa 4.0.
Current Code (Line 133)
return type(agents_by_type[agent_type_names[new_agent_class]][0])Expected behavior
Should use AgentSet.to_list()[index] as recommended in the migration guide (#3218).
Expected Code
return type(agents_by_type[agent_type_names[new_agent_class]].to_list()[0])To Reproduce
The issue can be observed by examining the code at line 133 of mesa/experimental/meta_agents/meta_agent.py:
return type(agents_by_type[agent_type_names[new_agent_class]][0])This line uses AgentSet[0] syntax, which is deprecated. While it currently works because model.agents_by_type returns _HardKeyAgentSet instances (which don't emit the warning), this code violates the Mesa 4.0 migration guidelines and should be updated to use the recommended to_list() method for consistency and future compatibility.
The function is called internally when creating meta-agents:
from mesa import Model, Agent
from mesa.experimental.meta_agents.meta_agent import create_meta_agent
class CustomAgent(Agent):
pass
model = Model()
agents = [CustomAgent(model) for _ in range(3)]
# First call creates the MetaAgentClass
meta1 = create_meta_agent(model, "MetaAgentClass", agents[:2], Agent)
# Second call uses extract_class to find existing MetaAgentClass
# This is where line 133 executes with the deprecated syntax
meta2 = create_meta_agent(model, "MetaAgentClass", agents[2:], Agent)Additional context
- This issue was partially addressed in PR fix: update alliance_formation example to use AgentSet.to_list() #3235, which fixed similar deprecation warnings in the
evaluate_combinationandfind_combinationsfunctions within the same file - However, the
extract_classfunction on line 133 was missed and still uses the deprecated syntax - This needs to be fixed to ensure compatibility with Mesa 4.0 where
AgentSet.__getitem__will be removed - The fix is straightforward: replace
agents_by_type[...][0]withagents_by_type[...].to_list()[0]
I am working on this issue and will submit a PR to fix it.