compas icon indicating copy to clipboard operation
compas copied to clipboard

Data.guid - mismatch in documentation and actual behaviour

Open funkchaser opened this issue 2 years ago • 1 comments

Describe the bug The documentation states that the guid attribute in Data is a "The globally unique identifier of the object." However, for example, when creating a deepcopy() of an object of a child class of Data, a rather confusing behaviour can be observed:

To Reproduce

from compas.geometry import Point
from copy import deepcopy()
p1 = Point(0,0,0)
p2 = deepcopy(p1)
print(p1.guid==p2.guid)
>>> True
print(p1.guid is p2.guid)
>>> False

Expected behavior Probably expecting a behaviour returning False in both comparisons. Otherwise suggest to mention that this is not the case in the documentation.

funkchaser avatar Jun 22 '22 10:06 funkchaser

You can use Point.copy(), which is deep-copy in the COMPAS world, to get the behavior you're expecting.

>>> p1 = Point(0, 0, 0) 
>>> p2 = p1.copy()      
>>> p1 is p2
False
>>> p1 == p2            
True                    
>>> p1.guid == p2.guid  
False                   

It might make sense to implement __deepcopy__ which would call copy() just to have copy.deepcopy behave this way, too.

The mechanism which copies over the GUID to the new instance is present in data.py and that's meant AFAIK for serialization, in which case it might make sense to keep the GUID, serialize and de-serialize it.

    def __getstate__(self):
        return {
            ...
            "guid": str(self.guid),
        }

    def __setstate__(self, state):
        ...
        if "guid" in state:
            self._guid = UUID(state['guid'])

Whereas Data.copy() which is concerned with deep copying, uses instance.from_data() which in turn creates the copy with the type's __init__().

chenkasirer avatar Jun 22 '22 11:06 chenkasirer