mesa icon indicating copy to clipboard operation
mesa copied to clipboard

AgentSet lacks .copy() and .__copy__(), raising AttributeError on documented copy operations

Open suhailmalik-tech opened this issue 1 month ago • 2 comments

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 an AgentSet containing 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() and model.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'

suhailmalik-tech avatar Oct 28 '26 22:10 suhailmalik-tech

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

  1. copying a copy doesn't buy you anything, and select() already returns a new set
  2. if you do want an independent set, AgentSet(agents, random=agents.random) or copy.copy(agents) both work (and afaik copy.copy is 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?

  1. AgentSet builds on MutableSet and Sequence, neither of which defines copy(), so nothing breaks contract as well

codebreaker32 avatar Oct 29 '26 15:10 codebreaker32

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:

  1. 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.

  2. 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?

priyanshux-dev avatar Oct 30 '26 03:10 priyanshux-dev