Skip to content

Commit 54a91be

Browse files
satrachrisgorgo
authored andcommitted
fix: thanks to @rkern we now have a passing test with dynamic traits retaining their type through pickling
1 parent b5b623d commit 54a91be

File tree

1 file changed

+12
-28
lines changed

1 file changed

+12
-28
lines changed

nipype/interfaces/base.py

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -499,36 +499,20 @@ def _get_sorteddict(self, object, dictwithhash=False, hash_method=None, hash_fil
499499
out = object
500500
return out
501501

502+
def __getstate__(self):
503+
state = super(BaseTraitedSpec, self).__getstate__()
504+
inst_traits = self._instance_traits()
505+
state['__instance_traits__'] = inst_traits
506+
return state
507+
508+
def __setstate__(self, state):
509+
inst_traits = state.pop('__instance_traits__', {})
510+
for attr, trait in inst_traits.iteritems():
511+
self.add_trait(attr, trait)
512+
super(BaseTraitedSpec, self).__setstate__(state)
502513

503514
class DynamicTraitedSpec(BaseTraitedSpec):
504-
""" A subclass to handle dynamic traits
505-
506-
This class is a workaround for add_traits and clone_traits not
507-
functioning well together.
508-
"""
509-
def __deepcopy__(self, memo):
510-
""" bug in deepcopy for HasTraits results in weird cloning behavior for
511-
added traits
512-
"""
513-
id_self = id(self)
514-
if id_self in memo:
515-
return memo[id_self]
516-
dup_dict = deepcopy(self.get(), memo)
517-
# access all keys
518-
for key in self.copyable_trait_names():
519-
_ = getattr(self, key)
520-
# clone once
521-
dup = self.clone_traits(memo=memo)
522-
for key in self.copyable_trait_names():
523-
try:
524-
_ = getattr(dup, key)
525-
except:
526-
pass
527-
# clone twice
528-
dup = self.clone_traits(memo=memo)
529-
dup.set(**dup_dict)
530-
return dup
531-
515+
pass
532516

533517
class TraitedSpec(BaseTraitedSpec):
534518
""" Create a subclass with strict traits.

0 commit comments

Comments
 (0)