Union Property

class properties.Union(doc, props, **kwargs)[source]

Property with multiple valid Property types

Union Properties contain a list of Property instances. Validation, serialization, etc. cycle through the corresponding method on the each Property instance sequentially until one succeeds. If all Property types raise an error, the Union Property will also raise an error.

Note

When specifying Property types, the order matters; if multiple types are valid, the earlier type will be favored. For example,

import properties
union_0 = properties.Union(
    doc='String and Color',
    props=(properties.String(''), properties.Color('')),
)
union_1 = properties.Union(
    doc='String and Color',
    props=(properties.Color(''), properties.String('')),
)

union_0.validate(None, 'red') == 'red'  # Validates to string
union_1.validate(None, 'red') == (255, 0, 0)  # Validates to color

Available keywords (in addition to those inherited from Property):

  • props - A list of Property instances that each specify a valid type for the Union Property. HasProperties classes may also be specified; these are coerced to Instance Properties of the respective class.