Show More
@@ -1050,11 +1050,27 b' class Union(TraitType):' | |||||
1050 | self.default_value = self.trait_types[0].get_default_value() |
|
1050 | self.default_value = self.trait_types[0].get_default_value() | |
1051 | super(Union, self).__init__(**metadata) |
|
1051 | super(Union, self).__init__(**metadata) | |
1052 |
|
1052 | |||
|
1053 | def instance_init(self, obj): | |||
|
1054 | for trait_type in self.trait_types: | |||
|
1055 | trait_type.name = self.name | |||
|
1056 | trait_type.this_class = self.this_class | |||
|
1057 | super(Union, self).instance_init(obj) | |||
|
1058 | ||||
1053 | def validate(self, obj, value): |
|
1059 | def validate(self, obj, value): | |
1054 | for trait_type in self.trait_types: |
|
1060 | for trait_type in self.trait_types: | |
1055 | try: |
|
1061 | try: | |
|
1062 | if value is None and trait_type.allow_none: | |||
|
1063 | return value | |||
1056 | if hasattr(trait_type, 'validate'): |
|
1064 | if hasattr(trait_type, 'validate'): | |
1057 | return trait_type.validate(obj, value) |
|
1065 | return trait_type.validate(obj, value) | |
|
1066 | elif hasattr(trait_type, 'is_valid_for'): | |||
|
1067 | valid = trait_type.is_valid_for(value) | |||
|
1068 | if valid: | |||
|
1069 | return value | |||
|
1070 | else: | |||
|
1071 | raise TraitError('invalid value for type: %r' % value) | |||
|
1072 | elif hasattr(trait_type, 'value_for'): | |||
|
1073 | return trait_type.value_for(value) | |||
1058 | else: |
|
1074 | else: | |
1059 | return value |
|
1075 | return value | |
1060 | except Exception: |
|
1076 | except Exception: |
General Comments 0
You need to be logged in to leave comments.
Login now