fix: update meta_agents extract_class to use to_list()#3241
fix: update meta_agents extract_class to use to_list()#3241
Conversation
There was a problem hiding this comment.
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_classfunction to useAgentSet.to_list()[index]instead of deprecatedAgentSet[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]) |
There was a problem hiding this comment.
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]]))
There was a problem hiding this comment.
@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
|
Performance benchmarks:
|
Per maintainer feedback, use next(iter()) which is more efficient than converting to list first.
Summary
Updates the
extract_classfunction inmesa/experimental/meta_agents/meta_agent.pyto useAgentSet.to_list()[index]instead of deprecatedAgentSet[index]syntax.Bug / Issue
Fixes #3240
The
extract_classfunction (line 133) uses deprecatedAgentSet.__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_typereturns_HardKeyAgentSetinstances), it should follow the recommendedto_list()pattern for consistency and future compatibility.Implementation
Changed line 133 in
mesa/experimental/meta_agents/meta_agent.py:Testing
Ran all meta_agents tests to verify the fix:
pytest tests/experimental/test_meta_agents.py -v # Result: 16 passed in 0.71sAll tests pass successfully with the updated code.
Additional Notes
evaluate_combinationandfind_combinationsfunctions within the same file, but missed theextract_classfunction