Hi @gjedlicska,
I’m working on detaching attributes of some Python Speckle objects. This mostly works properly. However, I believe that dictionary values containing Speckle objects within a dict attribute are not being detached, e.g. an example attribute as:
line_load.connecting_shapes = { 'shape': element_1d, 'geometry': node }
In which:
-
line_load
is a Speckle LoadBeam object -
connecting_shapes
is a custom, detachable attribute we add -
element_1d
is a Speckle Element1D object -
node
is a Speckle Point object
Detaching the connecting_shapes
attribute does not detach the element_1d
and node
objects. Instead, it stores the fully serialized objects within the dictionary values. To me, this seems incorrect. However, maybe there’s a good reason to do so (?).
Anyway, I’ve looked at implementing the detachment within the serialization procedure. To do so, I added a few lines within the dictionary processing functionality in the traverse_value
method of BaseObjectSerializer
, that are very similar to the processing of objects within lists, just above it :
elif isinstance(obj, dict):
for k, v in obj.items():
if isinstance(v, PRIMITIVES) or v is None:
continue
elif isinstance(v, Base): # THIS ELIF CHECK HAS BEEN ADDED
self.detach_lineage.append(detach)
ref_id, _ = self._traverse_base(v)
obj[k] = self.detach_helper(ref_id=ref_id)
else:
obj[k] = self.traverse_value(v, detach)
return obj
For me this seems to work properly and it actually fixes sending a full model with quite some of this nested data. However, I’m still wondering what your opinion is on this; is the missing detachment of dictionary values simply a bug, or is it a design choice? Hope you can elaborate a bit!
If this should be implemented, I’m happy to make a pull request