import six


def _metric_value(value_str, metric_type):
    """
    Return a Python-typed metric value from a metric value string.
    """
    if metric_type in (int, float):
        try:
            return metric_type(value_str)
        except ValueError:
            raise ValueError("Invalid {} metric value: {!r}".
                             format(metric_type.__class__.__name__, value_str))
    elif metric_type is six.text_type:
        # In Python 3, decode('unicode_escape) requires bytes, so we need
        # to encode to bytes. This also works in Python 2.
        return value_str.strip('"').encode('utf-8').decode('unicode_escape')
    else:
        assert metric_type is bool
        lower_str = value_str.lower()
        if lower_str == 'true':
            return True
        if lower_str == 'false':
            return False
        raise ValueError("Invalid boolean metric value: {!r}".format(value_str))