File tree Expand file tree Collapse file tree 1 file changed +13
-1
lines changed Expand file tree Collapse file tree 1 file changed +13
-1
lines changed Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments