enthought/traits — Issue #16: dynamic traits lose type information/validation when pickled
Author: satra
Created: 2012-01-16 14:28:48+00:00
State: open

@dpinte: finally got back to this. the example below demonstrates our issue. we use dynamic traits a lot and then pickle and send our objects to remote execution nodes. so this bug affects us a fair bit. we currently work around this by not using the validation.

## code

```python
def test_dynamic_traits():
from nose.tools import assert_raises
from traits.api import HasTraits, Int
from cPickle import dumps, loads
a = HasTraits()
a.add_trait('foo', Int)
a.foo = 1
assign_a = lambda : setattr(a, 'foo', 'a')
assert_raises(Exception, assign_a)
pkld_a = dumps(a)
unpkld_a = loads(pkld_a)
assign_a_again = lambda : setattr(unpkld_a, 'foo', 'a')
assert_raises(Exception, assign_a_again)
```

## test

```python
test_dynamic_traits()
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
/ in ()
----> 1 test_dynamic_traits()

/ in test_dynamic_traits()
11 unpkld_a = loads(pkld_a)
12 assign_a_again = lambda : setattr(unpkld_a, 'foo', 'a')
---> 13 assert_raises(Exception, assign_a_again)
14

/Library/Frameworks/EPD64.framework/Versions/7.1/lib/python2.7/unittest/case.pyc in assertRaises(self, excClass, callableObj, *args, **kwargs)
469 return context
470 with context:
--> 471 callableObj(*args, **kwargs)
472
473 def _getAssertEqualityFunc(self, first, second):

/Library/Frameworks/EPD64.framework/Versions/7.1/lib/python2.7/unittest/case.pyc in __exit__(self, exc_type, exc_value, tb)
113 exc_name = str(self.expected)
114 raise self.failureException(
--> 115 "{0} not raised".format(exc_name))
116 if not issubclass(exc_type, self.expected):
117 # let unexpected exceptions pass through

AssertionError: Exception not raised
```

## traits version

```python
import traits

traits.__version__
Out[4]: '4.1.1'
```