enthought / enthought/traits

Pickling error with DelegatesTo on multiple levels

Open
#549 8 comments 0 reactions 0 assignees View on GitHub
type: bug
Dominant language
Python
Stars
462
Forks
90
PR merge metrics
No merged PRs in 30d

Description

I have an issue restoring pickled traits instances when I use DelegatesTo in multiple levels.
In this example C1 delegates Value to C2 which delegates it to C3.
After de-serialization, I'm getting an error:
`
traits.trait_errors.DelegationError: The 'Value' attribute of a 'C2' object has a delegate which does not have traits.`

Other than printing the error, it then appears to works fine.
Am I doing anything wrong or is there a way to suppress this error?

traits 5.1.2, Python 3.7.4 win10 64 bit

Thanks

```

import pickle

from traits.api import HasStrictTraits, Str, Any, DelegatesTo, Instance

class C1(HasStrictTraits):
Parent = Any
Value = DelegatesTo('Parent')
def __init__(self, parent):
HasStrictTraits.__init__(self)
self.Parent = parent

class C2(HasStrictTraits):
Parent = Any
Value = DelegatesTo('Parent')
Child = Instance(C1)

def _Child_default(self):
return C1(self)

def __init__(self, parent):
HasStrictTraits.__init__(self)
self.Parent = parent

class C3(HasStrictTraits):
Value = Str
Child = Instance(C2)

def _Child_default(self):
return C2(self)

def save(self):
with open('test.dump', 'wb') as fid:
pickle.dump(self.__getstate__(), fid, protocol=2)

def load(self):
with open('test.dump', 'rb') as fid:
self.__setstate__(pickle.load(fid))

i1 = C3()
i1.Value = 'test'
i1.save()

i2 = C3()
i2.load()

print(i2.Value)
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.