def _checked_value(self, instance, value): # We allow four forms of value: # 1. An opaque (to us) string address pointing to a value that can be resolved by external # means. # 2. A `Resolvable` value that we can lazily resolve and type-check in `__get__`. # 3. A concrete instance that meets our type constraint. # 4. A dict when our type constraint has exactly one Serializable subject type - we convert the # dict into an instance of that type. if value is None: return None if isinstance(value, (six.string_types, Resolvable)): return value # Support untyped dicts that we deserialize on-demand here into the required type. # This feature allows for more brevity in the JSON form (local type inference) and an alternate # construction style in the python forms. type_constraint = self._get_type_constraint(instance) if (isinstance(value, dict) and len(type_constraint.types) == 1 and Serializable.is_serializable_type(type_constraint.types[0])): if not value: # TODO(John Sirois): Is this the right thing to do? Or should an empty serializable_type # be constructed? return None # {} -> None. else: serializable_type = type_constraint.types[0] return serializable_type(**value) if not type_constraint.satisfied_by(value): raise TypeConstraintError( 'Got {} of type {} for {} attribute of {} but expected {!r}'. format(value, type(value).__name__, self._name, instance, type_constraint)) return value
def _checked_value(self, instance, value): # We allow five forms of value: # 0. None. # 1. An opaque (to us) address pointing to a value that can be resolved by external # means. # 2. A `Resolvable` value that we can lazily resolve and type-check in `__get__`. # 3. A concrete instance that meets our type constraint. # 4. A dict when our type constraint has exactly one Serializable subject type - we convert the # dict into an instance of that type. if value is None: return None if isinstance(value, (six.string_types, Address, Resolvable)): return value # Support untyped dicts that we deserialize on-demand here into the required type. # This feature allows for more brevity in the JSON form (local type inference) and an alternate # construction style in the python forms. type_constraint = self._get_type_constraint(instance) if ( isinstance(value, dict) and len(type_constraint.types) == 1 and Serializable.is_serializable_type(type_constraint.types[0]) ): if not value: # TODO(John Sirois): Is this the right thing to do? Or should an empty serializable_type # be constructed? return None # {} -> None. else: serializable_type = type_constraint.types[0] return serializable_type(**value) if not type_constraint.satisfied_by(value): raise TypeConstraintError( "Got {} of type {} for {} attribute of {} but expected {!r}".format( value, type(value).__name__, self._name, instance, type_constraint ) ) return value
def __init__(self, type_alias, object_type): self._type_alias = type_alias self._object_type = object_type self._serializable = Serializable.is_serializable_type(self._object_type)
def __init__(self, type_alias, object_type): self._type_alias = type_alias self._object_type = object_type self._serializable = Serializable.is_serializable_type( self._object_type)