Show More
@@ -20,7 +20,7 b' from IPython.utils.traitlets import (' | |||||
20 | Int, Long, Integer, Float, Complex, Bytes, Unicode, TraitError, |
|
20 | Int, Long, Integer, Float, Complex, Bytes, Unicode, TraitError, | |
21 | Undefined, Type, This, Instance, TCPAddress, List, Tuple, |
|
21 | Undefined, Type, This, Instance, TCPAddress, List, Tuple, | |
22 | ObjectName, DottedObjectName, CRegExp, link, directional_link, |
|
22 | ObjectName, DottedObjectName, CRegExp, link, directional_link, | |
23 | EventfulList, EventfulDict |
|
23 | EventfulList, EventfulDict, ForwardDeclaredType, ForwardDeclaredInstance, | |
24 | ) |
|
24 | ) | |
25 | from IPython.utils import py3compat |
|
25 | from IPython.utils import py3compat | |
26 | from IPython.testing.decorators import skipif |
|
26 | from IPython.testing.decorators import skipif | |
@@ -969,6 +969,23 b' class TestInstanceList(TraitTestBase):' | |||||
969 | _good_values = [[Foo(), Foo(), None], None] |
|
969 | _good_values = [[Foo(), Foo(), None], None] | |
970 | _bad_values = [['1', 2,], '1', [Foo]] |
|
970 | _bad_values = [['1', 2,], '1', [Foo]] | |
971 |
|
971 | |||
|
972 | class ForwardDeclaredInstanceListTrait(HasTraits): | |||
|
973 | ||||
|
974 | value = List(ForwardDeclaredInstance('Foo')) | |||
|
975 | ||||
|
976 | class TestForwardDeclaredInstanceList(TraitTestBase): | |||
|
977 | ||||
|
978 | obj = ForwardDeclaredInstanceListTrait() | |||
|
979 | ||||
|
980 | def test_klass(self): | |||
|
981 | """Test that the instance klass is properly assigned.""" | |||
|
982 | self.assertIs(self.obj.traits()['value']._trait.klass, Foo) | |||
|
983 | ||||
|
984 | _default_value = [] | |||
|
985 | _good_values = [[Foo(), Foo(), None], None] | |||
|
986 | _bad_values = [['1', 2,], '1', [Foo]] | |||
|
987 | ||||
|
988 | ||||
972 | class LenListTrait(HasTraits): |
|
989 | class LenListTrait(HasTraits): | |
973 |
|
990 | |||
974 | value = List(Int, [0], minlen=1, maxlen=2) |
|
991 | value = List(Int, [0], minlen=1, maxlen=2) | |
@@ -1320,3 +1337,33 b' class TestEventful(TestCase):' | |||||
1320 |
|
1337 | |||
1321 | # Is the output correct? |
|
1338 | # Is the output correct? | |
1322 | self.assertEqual(a.x, {c: c for c in 'bz'}) |
|
1339 | self.assertEqual(a.x, {c: c for c in 'bz'}) | |
|
1340 | ||||
|
1341 | ||||
|
1342 | class ForwardDeclaredBar(object): | |||
|
1343 | pass | |||
|
1344 | ||||
|
1345 | class ForwardDeclaredBarSub(ForwardDeclaredBar): | |||
|
1346 | pass | |||
|
1347 | ||||
|
1348 | class ForwardDeclaredInstanceTrait(HasTraits): | |||
|
1349 | ||||
|
1350 | value = ForwardDeclaredInstance(klass='ForwardDeclaredBar') | |||
|
1351 | ||||
|
1352 | class TestForwardDeclaredInstanceTrait(TraitTestBase): | |||
|
1353 | ||||
|
1354 | obj = ForwardDeclaredInstanceTrait() | |||
|
1355 | _default_value = None | |||
|
1356 | _good_values = [None, ForwardDeclaredBar(), ForwardDeclaredBarSub()] | |||
|
1357 | _bad_values = ['foo', 3, ForwardDeclaredBar, ForwardDeclaredBarSub] | |||
|
1358 | ||||
|
1359 | ||||
|
1360 | class ForwardDeclaredTypeTrait(HasTraits): | |||
|
1361 | ||||
|
1362 | value = ForwardDeclaredType(klass='ForwardDeclaredBar') | |||
|
1363 | ||||
|
1364 | class TestForwardDeclaredInstanceTrait(TraitTestBase): | |||
|
1365 | ||||
|
1366 | obj = ForwardDeclaredTypeTrait() | |||
|
1367 | _default_value = None | |||
|
1368 | _good_values = [None, ForwardDeclaredBar, ForwardDeclaredBarSub] | |||
|
1369 | _bad_values = ['foo', 3, ForwardDeclaredBar(), ForwardDeclaredBarSub()] |
@@ -967,6 +967,35 b' class Instance(ClassBasedTraitType):' | |||||
967 | return dv |
|
967 | return dv | |
968 |
|
968 | |||
969 |
|
969 | |||
|
970 | class ForwardDeclaredMixin(object): | |||
|
971 | """ | |||
|
972 | Mixin for forward-declared versions of Instance and Type. | |||
|
973 | """ | |||
|
974 | def _resolve_classes(self): | |||
|
975 | """ | |||
|
976 | Find the specified class name by looking for it in the module in which | |||
|
977 | our this_class attribute was defined. | |||
|
978 | """ | |||
|
979 | try: | |||
|
980 | modname = self.this_class.__module__ | |||
|
981 | self.klass = import_item('.'.join([modname, self.klass])) | |||
|
982 | except AttributeError: | |||
|
983 | raise ImportError( | |||
|
984 | "Module {} has no attribute {}".format(modname, self.klass) | |||
|
985 | ) | |||
|
986 | ||||
|
987 | class ForwardDeclaredType(ForwardDeclaredMixin, Type): | |||
|
988 | """ | |||
|
989 | Forward-declared version of Type. | |||
|
990 | """ | |||
|
991 | pass | |||
|
992 | ||||
|
993 | class ForwardDeclaredInstance(ForwardDeclaredMixin, Instance): | |||
|
994 | """ | |||
|
995 | Forward-declared version of Instance. | |||
|
996 | """ | |||
|
997 | pass | |||
|
998 | ||||
970 | class This(ClassBasedTraitType): |
|
999 | class This(ClassBasedTraitType): | |
971 | """A trait for instances of the class containing this trait. |
|
1000 | """A trait for instances of the class containing this trait. | |
972 |
|
1001 |
General Comments 0
You need to be logged in to leave comments.
Login now