def __setattr__()

in tfx/types/artifact.py [0:0]


  def __setattr__(self, name: str, value: Any):
    """Custom __setattr__ to allow access to artifact properties."""
    if not self._initialized:
      object.__setattr__(self, name, value)
      return
    if name not in self._artifact_type.properties:
      if (name in self.__dict__ or
          any(name in c.__dict__ for c in self.__class__.mro())):
        # Use any provided getter / setter if available.
        object.__setattr__(self, name, value)
        return
      # In the case where we do not handle this via an explicit getter /
      # setter, we assume that the user implied an artifact attribute store,
      # and we raise an exception since such an attribute was not explicitly
      # defined in the Artifact PROPERTIES dictionary.
      raise AttributeError('Cannot set unknown property %r on artifact %r.' %
                           (name, self))
    property_mlmd_type = self._artifact_type.properties[name]
    if property_mlmd_type == metadata_store_pb2.STRING:
      if not isinstance(value, (str, bytes)):
        raise Exception(
            'Expected string value for property %r; got %r instead.' %
            (name, value))
      self._artifact.properties[name].string_value = value
    elif property_mlmd_type == metadata_store_pb2.INT:
      if not isinstance(value, int):
        raise Exception(
            'Expected integer value for property %r; got %r instead.' %
            (name, value))
      self._artifact.properties[name].int_value = value
    elif property_mlmd_type == metadata_store_pb2.DOUBLE:
      if not isinstance(value, float):
        raise Exception(
            'Expected float value for property %r; got %r instead.' %
            (name, value))
      self._artifact.properties[name].double_value = value
    elif property_mlmd_type == metadata_store_pb2.STRUCT:
      if not isinstance(value, (dict, list, str, float, int, type(None))):
        raise Exception(
            ('Expected JSON value (dict, list, string, float, int or None) '
             'for property %r; got %r instead.') % (name, value))
      encoded_value = _encode_struct_value(value)
      if encoded_value is None:
        self._artifact.properties[name].struct_value.Clear()
      else:
        self._artifact.properties[name].struct_value.CopyFrom(encoded_value)
      self._cached_modifiable_properties[name] = value
    elif property_mlmd_type == metadata_store_pb2.PROTO:
      if not isinstance(value, (message.Message, type(None))):
        raise Exception(
            'Expected protobuf message value or None for property %r; got %r '
            'instead.' % (name, value))
      if value is None:
        self._artifact.properties[name].proto_value.Clear()
      else:
        self._artifact.properties[name].proto_value.Pack(value)
      self._cached_modifiable_properties[name] = value
    else:
      raise Exception('Unknown MLMD type %r for property %r.' %
                      (property_mlmd_type, name))