Deprecate AgentSet sequence behavior and add to_list() method#3208
Deprecate AgentSet sequence behavior and add to_list() method#3208
to_list() method#3208Conversation
|
Performance benchmarks:
|
mesa/agent.py
Outdated
| Sequence behavior (indexing/slicing) is deprecated and will be removed in Mesa 4.0. | ||
| Use :meth:`to_list` instead: ``agentset.to_list()[index]`` or ``agentset.to_list()[start:stop]``. | ||
| """ | ||
| if not AgentSet._getitem_warning_emitted: |
There was a problem hiding this comment.
i dont;t think this is needed. warnings.warn should filter repeated warnings if I am not mistaken (but please test and check).
There was a problem hiding this comment.
You're partially correct! Python's warnings.warn with 'default' filter does suppress repeated warnings, but only per unique call site (due to stacklevel=2). Without the flag, if a user has agentset[0] in multiple locations, they'd see the warning once per location.
The class-level flag ensures the warning appears only once globally per session, which is less noisy but may hide additional call sites that need updating.
Options:
- Keep the flag - Shows warning exactly once globally (current implementation)
- Remove the flag - Shows warning once per call site (Python's default behavior, helps users find all locations to update)
Note: PendingDeprecationWarning is ignored by default in Python, so most users won't see this warning unless they enable it explicitly (e.g., running with -W default::PendingDeprecationWarning).
Which behavior do you prefer?
There was a problem hiding this comment.
I suggest removing the if and relying on Python's warning behavior. Let's keep this as simple as possible.
There was a problem hiding this comment.
Sure. I have updated the PR by relying on python's warning behavior.
|
Thanks of the PR. Can you add a small section to our migration guide? |
Sure I will. |
|
@EwoutH would you like to add this section under mesa 3.4.0 version or new mesa 3.5.0? And also could you verify the Content in it. AgentSet Sequence Behavior DeprecationThe # Old (deprecated)
first_agent = model.agents[0]
some_agents = model.agents[1:5]
last_agent = model.agents[-1]
# New
first_agent = model.agents.to_list()[0]
some_agents = model.agents.to_list()[1:5]
last_agent = model.agents.to_list()[-1]For multiple list operations, convert once and reuse: agent_list = model.agents.to_list()
first = agent_list[0]
last = agent_list[-1]
subset = agent_list[2:8]Ref: PR #3208 |
|
On first glance the content looks good. I can add it, but if you like, you can also open a PR yourself. Our migration guide is available/editable here: https://github.com/mesa/mesa/blob/main/docs/migration_guide.md |
* fix: update meta_agents extract_class to use to_list() Replace deprecated AgentSet[index] with AgentSet.to_list()[index] in extract_class function per Mesa 4.0 migration guidelines. Fixes #3240 Related to #3208 and #3218 * Use iterator protocol instead of to_list() Per maintainer feedback, use next(iter()) which is more efficient than converting to list first.
Summary
This PR implements the deprecation path discussed in #3185, preparing users for the removal of sequence behavior in Mesa 4.0.
Motivation
As discussed in #3185, sequence support (indexing/slicing via getitem) on AgentSet will be removed in Mesa 4.0. This PR provides:
Changes
mesa/agent.pytests/test_agent.py