def to_representation_for_type(value: Any, tf_function_cache: MutableMapping[(str, Any)], type_spec: Optional[computation_types.Type]=None, device: Optional[tf.config.LogicalDevice]=None) -> Any:
    'Verifies or converts the `value` to an eager object matching `type_spec`.\n\n  WARNING: This function is only partially implemented. It does not support\n  data sets at this point.\n\n  The output of this function is always an eager tensor, eager dataset, a\n  representation of a TensorFlow computation, or a nested structure of those\n  that matches `type_spec`, and when `device` has been specified, everything\n  is placed on that device on a best-effort basis.\n\n  TensorFlow computations are represented here as zero- or one-argument Python\n  callables that accept their entire argument bundle as a single Python object.\n\n  Args:\n    value: The raw representation of a value to compare against `type_spec` and\n      potentially to be converted.\n    tf_function_cache: A cache obeying `dict` semantics that can be used to look\n      up previously embedded TensorFlow functions.\n    type_spec: An instance of `tff.Type`, can be `None` for values that derive\n      from `typed_object.TypedObject`.\n    device: An optional `tf.config.LogicalDevice` to place the value on (for\n      tensor-level values).\n\n  Returns:\n    Either `value` itself, or a modified version of it.\n\n  Raises:\n    TypeError: If the `value` is not compatible with `type_spec`.\n  '
    type_spec = type_utils.reconcile_value_with_type_spec(value, type_spec)
    if isinstance(value, computation_base.Computation):
        return to_representation_for_type(computation_impl.ComputationImpl.get_proto(value), tf_function_cache, type_spec, device)
    elif isinstance(value, pb.Computation):
        key = (value.SerializeToString(), str(type_spec), (device.name if device else None))
        cached_fn = tf_function_cache.get(key)
        if (cached_fn is not None):
            return cached_fn
        embedded_fn = embed_tensorflow_computation(value, type_spec, device)
        tf_function_cache[key] = embedded_fn
        return embedded_fn
    elif type_spec.is_struct():
        type_elem = structure.to_elements(type_spec)
        value_elem = structure.to_elements(structure.from_container(value))
        result_elem = []
        if (len(type_elem) != len(value_elem)):
            raise TypeError('Expected a {}-element tuple, found {} elements.'.format(len(type_elem), len(value_elem)))
        for ((t_name, el_type), (v_name, el_val)) in zip(type_elem, value_elem):
            if (t_name != v_name):
                raise TypeError('Mismatching element names in type vs. value: {} vs. {}.'.format(t_name, v_name))
            el_repr = to_representation_for_type(el_val, tf_function_cache, el_type, device)
            result_elem.append((t_name, el_repr))
        return structure.Struct(result_elem)
    elif (device is not None):
        py_typecheck.check_type(device, tf.config.LogicalDevice)
        with tf.device(device.name):
            return to_representation_for_type(value, tf_function_cache, type_spec=type_spec, device=None)
    elif isinstance(value, EagerValue):
        return value.internal_representation
    elif isinstance(value, executor_value_base.ExecutorValue):
        raise TypeError('Cannot accept a value embedded within a non-eager executor.')
    elif type_spec.is_tensor():
        if (not tf.is_tensor(value)):
            value = tf.convert_to_tensor(value, dtype=type_spec.dtype)
        elif hasattr(value, 'read_value'):
            value = value.read_value()
        value_type = computation_types.TensorType(value.dtype.base_dtype, value.shape)
        if (not type_spec.is_assignable_from(value_type)):
            raise TypeError('The apparent type {} of a tensor {} does not match the expected type {}.'.format(value_type, value, type_spec))
        return value
    elif type_spec.is_sequence():
        if isinstance(value, list):
            value = tensorflow_utils.make_data_set_from_elements(None, value, type_spec.element)
        py_typecheck.check_type(value, type_conversions.TF_DATASET_REPRESENTATION_TYPES)
        element_type = computation_types.to_type(value.element_spec)
        value_type = computation_types.SequenceType(element_type)
        type_spec.check_assignable_from(value_type)
        return value
    else:
        raise TypeError('Unexpected type {}.'.format(type_spec))
Verifies or converts the `value` to an eager object matching `type_spec`.

WARNING: This function is only partially implemented. It does not support
data sets at this point.

The output of this function is always an eager tensor, eager dataset, a
representation of a TensorFlow computation, or a nested structure of those
that matches `type_spec`, and when `device` has been specified, everything
is placed on that device on a best-effort basis.

TensorFlow computations are represented here as zero- or one-argument Python
callables that accept their entire argument bundle as a single Python object.

Args:
  value: The raw representation of a value to compare against `type_spec` and
    potentially to be converted.
  tf_function_cache: A cache obeying `dict` semantics that can be used to look
    up previously embedded TensorFlow functions.
  type_spec: An instance of `tff.Type`, can be `None` for values that derive
    from `typed_object.TypedObject`.
  device: An optional `tf.config.LogicalDevice` to place the value on (for
    tensor-level values).

Returns:
  Either `value` itself, or a modified version of it.

Raises:
  TypeError: If the `value` is not compatible with `type_spec`.
def to_representation_for_type(value: Any, tf_function_cache: MutableMapping[(str, Any)], type_spec: Optional[computation_types.Type]=None, device: Optional[tf.config.LogicalDevice]=None) -> Any:
    'Verifies or converts the `value` to an eager object matching `type_spec`.\n\n  WARNING: This function is only partially implemented. It does not support\n  data sets at this point.\n\n  The output of this function is always an eager tensor, eager dataset, a\n  representation of a TensorFlow computation, or a nested structure of those\n  that matches `type_spec`, and when `device` has been specified, everything\n  is placed on that device on a best-effort basis.\n\n  TensorFlow computations are represented here as zero- or one-argument Python\n  callables that accept their entire argument bundle as a single Python object.\n\n  Args:\n    value: The raw representation of a value to compare against `type_spec` and\n      potentially to be converted.\n    tf_function_cache: A cache obeying `dict` semantics that can be used to look\n      up previously embedded TensorFlow functions.\n    type_spec: An instance of `tff.Type`, can be `None` for values that derive\n      from `typed_object.TypedObject`.\n    device: An optional `tf.config.LogicalDevice` to place the value on (for\n      tensor-level values).\n\n  Returns:\n    Either `value` itself, or a modified version of it.\n\n  Raises:\n    TypeError: If the `value` is not compatible with `type_spec`.\n  '
    type_spec = type_utils.reconcile_value_with_type_spec(value, type_spec)
    if isinstance(value, computation_base.Computation):
        return to_representation_for_type(computation_impl.ComputationImpl.get_proto(value), tf_function_cache, type_spec, device)
    elif isinstance(value, pb.Computation):
        key = (value.SerializeToString(), str(type_spec), (device.name if device else None))
        cached_fn = tf_function_cache.get(key)
        if (cached_fn is not None):
            return cached_fn
        embedded_fn = embed_tensorflow_computation(value, type_spec, device)
        tf_function_cache[key] = embedded_fn
        return embedded_fn
    elif type_spec.is_struct():
        type_elem = structure.to_elements(type_spec)
        value_elem = structure.to_elements(structure.from_container(value))
        result_elem = []
        if (len(type_elem) != len(value_elem)):
            raise TypeError('Expected a {}-element tuple, found {} elements.'.format(len(type_elem), len(value_elem)))
        for ((t_name, el_type), (v_name, el_val)) in zip(type_elem, value_elem):
            if (t_name != v_name):
                raise TypeError('Mismatching element names in type vs. value: {} vs. {}.'.format(t_name, v_name))
            el_repr = to_representation_for_type(el_val, tf_function_cache, el_type, device)
            result_elem.append((t_name, el_repr))
        return structure.Struct(result_elem)
    elif (device is not None):
        py_typecheck.check_type(device, tf.config.LogicalDevice)
        with tf.device(device.name):
            return to_representation_for_type(value, tf_function_cache, type_spec=type_spec, device=None)
    elif isinstance(value, EagerValue):
        return value.internal_representation
    elif isinstance(value, executor_value_base.ExecutorValue):
        raise TypeError('Cannot accept a value embedded within a non-eager executor.')
    elif type_spec.is_tensor():
        if (not tf.is_tensor(value)):
            value = tf.convert_to_tensor(value, dtype=type_spec.dtype)
        elif hasattr(value, 'read_value'):
            value = value.read_value()
        value_type = computation_types.TensorType(value.dtype.base_dtype, value.shape)
        if (not type_spec.is_assignable_from(value_type)):
            raise TypeError('The apparent type {} of a tensor {} does not match the expected type {}.'.format(value_type, value, type_spec))
        return value
    elif type_spec.is_sequence():
        if isinstance(value, list):
            value = tensorflow_utils.make_data_set_from_elements(None, value, type_spec.element)
        py_typecheck.check_type(value, type_conversions.TF_DATASET_REPRESENTATION_TYPES)
        element_type = computation_types.to_type(value.element_spec)
        value_type = computation_types.SequenceType(element_type)
        type_spec.check_assignable_from(value_type)
        return value
    else:
        raise TypeError('Unexpected type {}.'.format(type_spec))