diff --git a/IPython/utils/tests/test_traitlets.py b/IPython/utils/tests/test_traitlets.py index 9251ae8..b08adf4 100644 --- a/IPython/utils/tests/test_traitlets.py +++ b/IPython/utils/tests/test_traitlets.py @@ -684,6 +684,20 @@ class TestThis(TestCase): self.assertEqual(f.t, b) self.assertRaises(TraitError, setattr, b, 't', f) + def test_this_in_container(self): + + class Tree(HasTraits): + value = Unicode() + leaves = List(This()) + + tree = Tree( + value='foo', + leaves=[Tree('bar'), Tree('buzz')] + ) + + with self.assertRaises(TraitError): + tree.leaves = [1, 2] + class TraitTestBase(TestCase): """A best testing class for basic trait types.""" diff --git a/IPython/utils/traitlets.py b/IPython/utils/traitlets.py index 1b0838a..f37e472 100644 --- a/IPython/utils/traitlets.py +++ b/IPython/utils/traitlets.py @@ -1354,6 +1354,8 @@ class Container(Instance): return self.klass(validated) def instance_init(self, obj): + if isinstance(self._trait, TraitType): + self._trait.this_class = self.this_class if isinstance(self._trait, Instance): self._trait._resolve_classes() super(Container, self).instance_init(obj)