Skip to content

fix: update meta_agents extract_class to use to_list()#3241

Merged
quaquel merged 2 commits intomesa:mainfrom
Jayantparashar10:fix/meta-agents-deprecated-indexing
Feb 4, 2026
Merged

fix: update meta_agents extract_class to use to_list()#3241
quaquel merged 2 commits intomesa:mainfrom
Jayantparashar10:fix/meta-agents-deprecated-indexing

Conversation

@Jayantparashar10
Copy link
Copy Markdown
Contributor

Summary

Updates the extract_class function in mesa/experimental/meta_agents/meta_agent.py to use AgentSet.to_list()[index] instead of deprecated AgentSet[index] syntax.

Bug / Issue

Fixes #3240

The extract_class function (line 133) uses deprecated AgentSet.__getitem__ syntax, which was deprecated in #3208 and will be removed in Mesa 4.0. This violates Mesa 4.0 migration guidelines established in #3218.

While the code currently works (since model.agents_by_type returns _HardKeyAgentSet instances), it should follow the recommended to_list() pattern for consistency and future compatibility.

Implementation

Changed line 133 in mesa/experimental/meta_agents/meta_agent.py:

# Before
return type(agents_by_type[agent_type_names[new_agent_class]][0])

# After
return type(agents_by_type[agent_type_names[new_agent_class]].to_list()[0])

Testing

Ran all meta_agents tests to verify the fix:

pytest tests/experimental/test_meta_agents.py -v
# Result: 16 passed in 0.71s

All tests pass successfully with the updated code.

Additional Notes

Replace deprecated AgentSet[index] with AgentSet.to_list()[index]
in extract_class function per Mesa 4.0 migration guidelines.

Fixes mesa#3240
Related to mesa#3208 and mesa#3218
Copilot AI review requested due to automatic review settings February 4, 2026 18:56
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes deprecated AgentSet indexing syntax in the extract_class function to comply with Mesa 4.0 migration guidelines. This is a follow-up to PR #3235, which fixed similar deprecation issues in the same file but missed this function.

Changes:

  • Updated extract_class function to use AgentSet.to_list()[index] instead of deprecated AgentSet[index] syntax

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.


if new_agent_class in agent_type_names:
return type(agents_by_type[agent_type_names[new_agent_class]][0])
return type(agents_by_type[agent_type_names[new_agent_class]].to_list()[0])
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the aim is to get the class, so you can better use the iter protocal here:

next(iter(agents_by_type[agent_type_names[new_agent_class]]))

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@quaquel Thank you for the feedback! You're absolutely right - using the iterator protocol is more efficient. I've updated the code to use next(iter(...)) instead of .to_list()[0].

All tests still pass:

pytest [test_meta_agents.py](http://_vscodecontentref_/2) -v 

16 passed in 0.72s

@quaquel quaquel added the deprecation When a new deprecation is introduced label Feb 4, 2026
@github-actions
Copy link
Copy Markdown

github-actions bot commented Feb 4, 2026

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 -1.5% [-2.3%, -0.7%] 🔵 -0.8% [-1.0%, -0.6%]
BoltzmannWealth large 🔵 +0.5% [-0.1%, +1.0%] 🔵 -1.6% [-2.9%, -0.2%]
Schelling small 🔵 +0.7% [+0.4%, +1.0%] 🔵 -0.1% [-0.3%, +0.1%]
Schelling large 🔵 -0.2% [-0.9%, +0.6%] 🔵 -2.8% [-4.2%, -1.2%]
WolfSheep small 🔵 -0.6% [-0.9%, -0.2%] 🔵 -1.4% [-1.6%, -1.3%]
WolfSheep large 🔵 -2.5% [-3.8%, -1.0%] 🔵 -4.0% [-5.4%, -2.6%]
BoidFlockers small 🔵 +2.3% [+2.0%, +2.7%] 🔵 +2.0% [+1.8%, +2.2%]
BoidFlockers large 🔵 +2.9% [+2.5%, +3.2%] 🔵 +0.9% [+0.6%, +1.3%]

Per maintainer feedback, use next(iter()) which is more efficient
than converting to list first.
@quaquel quaquel merged commit 352c46c into mesa:main Feb 4, 2026
14 checks passed
@Jayantparashar10 Jayantparashar10 deleted the fix/meta-agents-deprecated-indexing branch February 4, 2026 19:37
@EwoutH EwoutH added experimental Release notes label and removed deprecation When a new deprecation is introduced labels Feb 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

experimental Release notes label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Deprecated AgentSet indexing in meta_agents.extract_class function

4 participants