def as_dtype(type_value):
    """Converts the given `type_value` to a `DType`.

    Args:
      type_value: A value that can be converted to a `tf.DType` object. This may
        currently be a `tf.DType` object, a [`DataType`
        enum](https://www.tensorflow.org/code/tensorflow/core/framework/types.proto),
        a string type name, or a `numpy.dtype`.

    Returns:
      A `DType` corresponding to `type_value`.

    Raises:
      TypeError: If `type_value` cannot be converted to a `DType`.
    """
    if isinstance(type_value, DType):
        return type_value

    try:
        return _INTERN_TABLE[type_value]
    except KeyError:
        pass

    try:
        return _STRING_TO_TF[type_value]
    except KeyError:
        pass

    try:
        return _PYTHON_TO_TF[type_value]
    except KeyError:
        pass

    if isinstance(type_value, np.dtype):
        # The numpy dtype for strings is variable length. We can not compare
        # dtype with a single constant (np.string does not exist) to decide
        # dtype is a "string" type. We need to compare the dtype.type to be
        # sure it's a string type.
        if type_value.type == np.string_ or type_value.type == np.unicode_:
            return string

    if isinstance(type_value, (type, np.dtype)):
        for key, val in _NP_TO_TF:
            try:
                if key == type_value:
                    return val
            except TypeError as e:
                raise TypeError(
                    "Cannot convert {} to a dtype. {}".format(type_value, e)
                )

    raise TypeError("Cannot convert value %r to a TensorFlow DType." % type_value)
Converts the given `type_value` to a `DType`.

    Args:
      type_value: A value that can be converted to a `tf.DType` object. This may
        currently be a `tf.DType` object, a [`DataType`
        enum](https://www.tensorflow.org/code/tensorflow/core/framework/types.proto),
        a string type name, or a `numpy.dtype`.

    Returns:
      A `DType` corresponding to `type_value`.

    Raises:
      TypeError: If `type_value` cannot be converted to a `DType`.