AgentSet lacks .copy() and .__copy__(), raising AttributeError on documented copy operations
Describing the bug
AgentSet does not implement a .copy() method or a __copy__() hook. While _HardKeyAgentSet.copy() exists, it returns a standard AgentSet (weak references). Attempting to call .copy() on that copy, or on any standard AgentSet (such as the result of .select()), immediately raises AttributeError: 'AgentSet' object has no attribute 'copy'.
Furthermore, calling Python's standard copy.copy(agentset) falls back to generic pickle reduction (__reduce__ / __getstate__ / __setstate__) instead of an efficient shallow copy.
Expected behavior
agentset.copy()should return a new shallow copy as anAgentSetcontaining the current live agents and sharing the same random number generator instance.copy.copy(agentset)should use__copy__to return the same shallow copy without invoking pickle machinery.- This aligns with the documented usage in
mesa/model.py(model.agents.copy()andmodel.agents_by_type[AgentType].copy()).
To Reproduce Minimal reproducible example:
from mesa import Model, Agent
model = Model()
Agent(model)
# 1. Calling .copy() on a subset (AgentSet) fails:
subset = model.agents.select()
subset.copy() # Raises: AttributeError: 'AgentSet' object has no attribute 'copy'
# 2. Calling .copy() on a copy fails:
agents_copy = model.agents.copy()
agents_copy.copy() # Raises: AttributeError: 'AgentSet' object has no attribute 'copy'
Traceback:
Traceback (most recent call last):
File "<string>", line 9, in <module>
subset.copy()
AttributeError: 'AgentSet' object has no attribute 'copy'
Thanks for the report, but I don't think this is a bug.
copy() on _HardKeyAgentSet is intentional. model.agents is the model's own registry and holds strong references, so copy() exists to give you a weak AgentSet snapshot you can safely work on, see #3219 for rationale
Also
- copying a copy doesn't buy you anything, and
select()already returns a new set - if you do want an independent set,
AgentSet(agents, random=agents.random)orcopy.copy(agents)both work (and afaikcopy.copyis correct, not slower)
Furthermore, calling Python's standard copy.copy(agentset) falls back to generic pickle reduction (reduce / getstate / setstate) instead of an efficient shallow copy.
Could you please elaborate incase I am missing something?
AgentSetbuilds onMutableSetandSequence, neither of which definescopy(), so nothing breaks contract as well
Thanks for the feedback @codebreaker32
But, still I wanted to share a quick perspective on why implementing a .copy() or copy() method on AgentSet might still be worth considering:
-
Since _HardKeyAgentSet has a .copy() method, users naturally expect regular AgentSet instances (like those returned by .select()) to support .copy() as well. Raising an AttributeError can be surprising from a developer UX standpoint.
-
Without a copy() hook on AgentSet, standard calls to copy.copy(agentset) fall back on pickle serialization (reduce/getstate) rather than performing a lightweight shallow copy. Adding copy would make standard Python copying much cleaner.
Even if .copy() isn't required by MutableSet, adding copy() or .copy() would improve API ergonomic consistency and performance. What do you think?