code: def _cast_to_type_if_compatible(name, param_type, value):
      """Cast hparam to the provided type, if compatible.
  
      Args:
        name: Name of the hparam to be cast.
        param_type: The type of the hparam.
        value: The value to be cast, if compatible.
  
      Returns:
        The result of casting `value` to `param_type`.
  
      Raises:
        ValueError: If the type of `value` is not compatible with param_type.
          * If `param_type` is a string type, but `value` is not.
          * If `param_type` is a boolean, but `value` is not, or vice versa.
          * If `param_type` is an integer type, but `value` is not.
          * If `param_type` is a float type, but `value` is not a numeric type.
      """
      fail_msg = (
          "Could not cast hparam '%s' of type '%s' from value %r"
          % (name, param_type, value)
      )
  
      # If `value` is already of type `param_type`, return it directly.
      # `isinstance` is too weak (e.g. isinstance(True, int) == True).
      if (
          type(value) == param_type
      ):  # pylint: disable=unidiomatic-typecheck
          return value
  
      # Some callers use None, for which we can't do any casting/checking. :(
      if issubclass(param_type, type(None)):
          return value
  
      # Avoid converting a non-string type to a string.
      if issubclass(
          param_type, (six.string_types, six.binary_type)
      ) and not isinstance(value, (six.string_types, six.binary_type)):
          raise ValueError(fail_msg)
  
      # Avoid converting a number or string type to a boolean or vice versa.
      if issubclass(param_type, bool) != isinstance(value, bool):
          raise ValueError(fail_msg)
  
      # Avoid converting float to an integer (the reverse is fine).
      if issubclass(param_type, numbers.Integral) and not isinstance(
          value, numbers.Integral
      ):
          raise ValueError(fail_msg)
  
      # Avoid converting a non-numeric type to a numeric type.
      if issubclass(param_type, numbers.Number) and not isinstance(
          value, numbers.Number
      ):
          raise ValueError(fail_msg)
  
      return param_type(value)
  
imports: from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  import json
  import numbers
  import re
  from model_search.proto import hparam_pb2
  import six
label_desc: Use image node_id 8 for calling a global function with example usage: _cast_to_type_if_compatible(name, param_type, value) and returns: param_type, value, value