Show More
@@ -89,28 +89,6 b' class TestTraitType(TestCase):' | |||||
89 | tt = MyIntTT('bad default') |
|
89 | tt = MyIntTT('bad default') | |
90 | self.assertRaises(TraitError, B) |
|
90 | self.assertRaises(TraitError, B) | |
91 |
|
91 | |||
92 | def test_is_valid_for(self): |
|
|||
93 | class MyTT(TraitType): |
|
|||
94 | def is_valid_for(self, value): |
|
|||
95 | return True |
|
|||
96 | class A(HasTraits): |
|
|||
97 | tt = MyTT |
|
|||
98 |
|
||||
99 | a = A() |
|
|||
100 | a.tt = 10 |
|
|||
101 | self.assertEqual(a.tt, 10) |
|
|||
102 |
|
||||
103 | def test_value_for(self): |
|
|||
104 | class MyTT(TraitType): |
|
|||
105 | def value_for(self, value): |
|
|||
106 | return 20 |
|
|||
107 | class A(HasTraits): |
|
|||
108 | tt = MyTT |
|
|||
109 |
|
||||
110 | a = A() |
|
|||
111 | a.tt = 10 |
|
|||
112 | self.assertEqual(a.tt, 20) |
|
|||
113 |
|
||||
114 | def test_info(self): |
|
92 | def test_info(self): | |
115 | class A(HasTraits): |
|
93 | class A(HasTraits): | |
116 | tt = TraitType |
|
94 | tt = TraitType |
@@ -447,20 +447,11 b' class TraitType(object):' | |||||
447 | def _validate(self, obj, value): |
|
447 | def _validate(self, obj, value): | |
448 | if value is None and self.allow_none: |
|
448 | if value is None and self.allow_none: | |
449 | return value |
|
449 | return value | |
|
450 | if hasattr(self, 'validate'): | |||
|
451 | value = self.validate(obj, value) | |||
450 | if 'validate' in self._metadata: |
|
452 | if 'validate' in self._metadata: | |
451 | value = self._metadata['validate'](obj, value, self) |
|
453 | value = self._metadata['validate'](obj, value, self) | |
452 | if hasattr(self, 'validate'): |
|
454 | return value | |
453 | return self.validate(obj, value) |
|
|||
454 | elif hasattr(self, 'is_valid_for'): |
|
|||
455 | valid = self.is_valid_for(value) |
|
|||
456 | if valid: |
|
|||
457 | return value |
|
|||
458 | else: |
|
|||
459 | raise TraitError('invalid value for type: %r' % value) |
|
|||
460 | elif hasattr(self, 'value_for'): |
|
|||
461 | return self.value_for(value) |
|
|||
462 | else: |
|
|||
463 | return value |
|
|||
464 |
|
455 | |||
465 | def __or__(self, other): |
|
456 | def __or__(self, other): | |
466 | if isinstance(other, Union): |
|
457 | if isinstance(other, Union): |
General Comments 0
You need to be logged in to leave comments.
Login now