Skip to content

Deprecated AgentSet indexing in meta_agents.extract_class function #3240

@Jayantparashar10

Description

@Jayantparashar10

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_combination and find_combinations functions within the same file
  • However, the extract_class function 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] with agents_by_type[...].to_list()[0]

I am working on this issue and will submit a PR to fix it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions