##// END OF EJS Templates
Overload __or__ on traitlets and add a test
Sylvain Corlay -
Show More
@@ -16,7 +16,7 b' import nose.tools as nt'
16 from nose import SkipTest
16 from nose import SkipTest
17
17
18 from IPython.utils.traitlets import (
18 from IPython.utils.traitlets import (
19 HasTraits, MetaHasTraits, TraitType, Any, CBytes, Dict,
19 HasTraits, MetaHasTraits, TraitType, Any, Bool, CBytes, Dict,
20 Int, Long, Integer, Float, Complex, Bytes, Unicode, TraitError,
20 Int, Long, Integer, Float, Complex, Bytes, Unicode, TraitError,
21 Union, Undefined, Type, This, Instance, TCPAddress, List, Tuple,
21 Union, Undefined, Type, This, Instance, TCPAddress, List, Tuple,
22 ObjectName, DottedObjectName, CRegExp, link, directional_link,
22 ObjectName, DottedObjectName, CRegExp, link, directional_link,
@@ -762,13 +762,22 b' class AnyTraitTest(TraitTestBase):'
762
762
763 class UnionTrait(HasTraits):
763 class UnionTrait(HasTraits):
764
764
765 value = Union([Type(), Unicode()])
765 value = Union([Type(), Bool()])
766
766
767 class UnionTraitTest(TraitTestBase):
767 class UnionTraitTest(TraitTestBase):
768
768
769 obj = UnionTrait()
769 obj = UnionTrait(value='IPython.utils.ipstruct.Struct')
770 _default_value = None
770 _good_values = [int, float, True]
771 _good_values = [int, float, 'ten']
771 _bad_values = [[], (0,), 1j]
772
773 class OrTrait(HasTraits):
774
775 value = Bool() | Unicode()
776
777 class OrTraitTest(TraitTestBase):
778
779 obj = OrTrait()
780 _good_values = [True, False, 'ten']
772 _bad_values = [[], (0,), 1j]
781 _bad_values = [[], (0,), 1j]
773
782
774 class IntTrait(HasTraits):
783 class IntTrait(HasTraits):
@@ -445,6 +445,12 b' class TraitType(object):'
445 else:
445 else:
446 return value
446 return value
447
447
448 def __or__(self, other):
449 if isinstance(other, Union):
450 return Union([self] + other.trait_types)
451 else:
452 return Union([self, other])
453
448 def info(self):
454 def info(self):
449 return self.info_text
455 return self.info_text
450
456
@@ -1062,10 +1068,16 b' class Union(TraitType):'
1062 for trait_type in self.trait_types:
1068 for trait_type in self.trait_types:
1063 try:
1069 try:
1064 return trait_type._validate(obj, value)
1070 return trait_type._validate(obj, value)
1065 except Exception:
1071 except TraitError:
1066 continue
1072 continue
1067 self.error(obj, value)
1073 self.error(obj, value)
1068
1074
1075 def __or__(self, other):
1076 if isinstance(other, Union):
1077 return Union(self.trait_types + other.trait_types)
1078 else:
1079 return Union(self.trait_types + [other])
1080
1069 #-----------------------------------------------------------------------------
1081 #-----------------------------------------------------------------------------
1070 # Basic TraitTypes implementations/subclasses
1082 # Basic TraitTypes implementations/subclasses
1071 #-----------------------------------------------------------------------------
1083 #-----------------------------------------------------------------------------
General Comments 0
You need to be logged in to leave comments. Login now