Show More
@@ -885,7 +885,7 b' class Instance(ClassBasedTraitType):' | |||
|
885 | 885 | """Construct an Instance trait. |
|
886 | 886 | |
|
887 | 887 | This trait allows values that are instances of a particular |
|
888 |
class or its sub |
|
|
888 | class or its subclasses. Our implementation is quite different | |
|
889 | 889 | from that of enthough.traits as we don't allow instances to be used |
|
890 | 890 | for klass and we handle the ``args`` and ``kw`` arguments differently. |
|
891 | 891 | |
@@ -1026,6 +1026,41 b' class This(ClassBasedTraitType):' | |||
|
1026 | 1026 | self.error(obj, value) |
|
1027 | 1027 | |
|
1028 | 1028 | |
|
1029 | class Union(TraitType): | |
|
1030 | """A trait type representing a Union type.""" | |
|
1031 | ||
|
1032 | def __init__(self, trait_types, **metadata): | |
|
1033 | """Construct a Union trait. | |
|
1034 | ||
|
1035 | This trait allows values that are allowed by at least one of the | |
|
1036 | specified trait types. | |
|
1037 | ||
|
1038 | Parameters | |
|
1039 | ---------- | |
|
1040 | trait_types: sequence | |
|
1041 | The list of trait types of length at least 1. | |
|
1042 | ||
|
1043 | Notes | |
|
1044 | ----- | |
|
1045 | Union([Float(), Bool(), Int()]) attempts to validate the provided values | |
|
1046 | with the validation function of Float, then Bool, and finally Int. | |
|
1047 | """ | |
|
1048 | self.trait_types = trait_types | |
|
1049 | self.info_text = " or ".join([tt.info_text for tt in self.trait_types]) | |
|
1050 | self.default_value = self.trait_types[0].get_default_value() | |
|
1051 | super(Union, self).__init__(**metadata) | |
|
1052 | ||
|
1053 | def validate(self, obj, value): | |
|
1054 | for trait_type in self.trait_types: | |
|
1055 | try: | |
|
1056 | if hasattr(trait_type, 'validate'): | |
|
1057 | return trait_type.validate(obj, value) | |
|
1058 | else: | |
|
1059 | return value | |
|
1060 | except Exception: | |
|
1061 | continue | |
|
1062 | self.error(obj, value) | |
|
1063 | ||
|
1029 | 1064 | #----------------------------------------------------------------------------- |
|
1030 | 1065 | # Basic TraitTypes implementations/subclasses |
|
1031 | 1066 | #----------------------------------------------------------------------------- |
General Comments 0
You need to be logged in to leave comments.
Login now