diff --git a/IPython/utils/tests/test_traitlets.py b/IPython/utils/tests/test_traitlets.py index 1d888d6..c550ff0 100644 --- a/IPython/utils/tests/test_traitlets.py +++ b/IPython/utils/tests/test_traitlets.py @@ -972,6 +972,19 @@ class TestList(TraitTestBase): class Foo(object): pass +class NoneInstanceListTrait(HasTraits): + + value = List(Instance(Foo, allow_none=False)) + +class TestNoneInstanceList(TraitTestBase): + + obj = NoneInstanceListTrait() + + _default_value = [] + _good_values = [[Foo(), Foo()], []] + _bad_values = [[None], [Foo(), None]] + + class InstanceListTrait(HasTraits): value = List(Instance(__name__+'.Foo')) diff --git a/IPython/utils/traitlets.py b/IPython/utils/traitlets.py index e8b9bfa..b991386 100644 --- a/IPython/utils/traitlets.py +++ b/IPython/utils/traitlets.py @@ -1461,8 +1461,8 @@ class Container(Instance): def instance_init(self, obj): if isinstance(self._trait, TraitType): self._trait.this_class = self.this_class - if hasattr(self._trait, 'instance_init'): - self._trait.instance_init(obj) + if hasattr(self._trait, '_resolve_classes'): + self._trait._resolve_classes() super(Container, self).instance_init(obj) @@ -1631,6 +1631,14 @@ class Tuple(Container): validated.append(v) return tuple(validated) + def instance_init(self, obj): + for trait in self._traits: + if isinstance(trait, TraitType): + trait.this_class = self.this_class + if hasattr(trait, '_resolve_classes'): + trait._resolve_classes() + super(Container, self).instance_init(obj) + class Dict(Instance): """An instance of a Python dict."""