diff --git a/IPython/utils/tests/test_traitlets.py b/IPython/utils/tests/test_traitlets.py index a4a5423..2cbd920 100644 --- a/IPython/utils/tests/test_traitlets.py +++ b/IPython/utils/tests/test_traitlets.py @@ -881,6 +881,25 @@ class TestList(TraitTestBase): value = list(value) return value +class Foo(object): + pass + +class InstanceListTrait(HasTraits): + + value = List(Instance(__name__+'.Foo')) + +class TestInstanceList(TraitTestBase): + + obj = InstanceListTrait() + + def test_klass(self): + """Test that the instance klass is properly assigned.""" + self.assertIs(self.obj.traits()['value']._trait.klass, Foo) + + _default_value = [] + _good_values = [[Foo(), Foo(), None], None] + _bad_values = [['1', 2,], '1', [Foo]] + class LenListTrait(HasTraits): value = List(Int, [0], minlen=1, maxlen=2) @@ -1105,4 +1124,4 @@ def test_pickle_hastraits(): c2 = pickle.loads(p) nt.assert_equal(c2.i, c.i) nt.assert_equal(c2.j, c.j) - \ No newline at end of file + diff --git a/IPython/utils/traitlets.py b/IPython/utils/traitlets.py index 8141cf6..0cc0c75 100644 --- a/IPython/utils/traitlets.py +++ b/IPython/utils/traitlets.py @@ -1297,6 +1297,11 @@ class Container(Instance): validated.append(v) return self.klass(validated) + def instance_init(self, obj): + if isinstance(self._trait, Instance): + self._trait._resolve_classes() + super(Container, self).instance_init(obj) + class List(Container): """An instance of a Python list."""