Skip to content

Commit 4187380

Browse files
committed
feat: enhance key handling in AbstractAgentSetRegistry for string keys
1 parent 0e1044c commit 4187380

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

mesa_frames/abstract/agentsetregistry.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -540,10 +540,22 @@ def __setitem__(
540540
"""
541541
if value.model is not self.model:
542542
raise TypeError("Assigned AgentSet must belong to the same model")
543-
if isinstance(key, (int, str)):
543+
if isinstance(key, int):
544544
# Delegate to replace() so subclasses centralize invariant handling.
545545
self.replace({key: value}, inplace=True, atomic=True)
546546
return
547+
if isinstance(key, str):
548+
for existing in self:
549+
if existing.name == key:
550+
self.replace({key: value}, inplace=True, atomic=True)
551+
return
552+
try:
553+
value.rename(key, inplace=True)
554+
except Exception:
555+
if hasattr(value, "_name"):
556+
value._name = key # type: ignore[attr-defined]
557+
self.add(value, inplace=True)
558+
return
547559
raise TypeError("Key must be int index or str name")
548560

549561
@abstractmethod

0 commit comments

Comments
 (0)