Improve error messages for config.py and dtypes.py.
PiperOrigin-RevId: 389947507
Change-Id: Ia3f0130e4ba9a3e9da3f42aa6a181405f6cb37ba
diff --git a/tensorflow/python/framework/BUILD b/tensorflow/python/framework/BUILD
index 80cacc1..b4ae02a 100644
--- a/tensorflow/python/framework/BUILD
+++ b/tensorflow/python/framework/BUILD
@@ -1000,6 +1000,7 @@
srcs = ["config.py"],
srcs_version = "PY3",
deps = [
+ ":errors",
":ops",
"//tensorflow/python/util",
],
diff --git a/tensorflow/python/framework/config.py b/tensorflow/python/framework/config.py
index 6c8fa03..5b659b8 100644
--- a/tensorflow/python/framework/config.py
+++ b/tensorflow/python/framework/config.py
@@ -21,6 +21,7 @@
from typing import Union
from tensorflow.python.eager import context
+from tensorflow.python.framework import errors
from tensorflow.python.util import _pywrap_determinism
from tensorflow.python.util import _pywrap_tensor_float_32_execution
from tensorflow.python.util import deprecation
@@ -300,7 +301,8 @@
elif device_policy == context.DEVICE_PLACEMENT_EXPLICIT:
return 'explicit'
else:
- raise ValueError('Not a valid device policy: %r' % device_policy)
+ raise errors.InternalError(
+ f'Got an invalid device policy: {device_policy!r}.')
@tf_export('config.experimental.set_device_policy')
@@ -343,7 +345,10 @@
elif device_policy is None:
context.context().device_policy = None
else:
- raise ValueError('Not a valid device policy: %r' % device_policy)
+ raise ValueError(
+ f'Invalid argument `device_policy`: {device_policy!r}. Please refer to '
+ 'https://www.tensorflow.org/api_docs/python/tf/config/experimental/set_device_policy '
+ 'for valid `device_policy` arguments.')
@tf_export('config.experimental.get_synchronous_execution')
diff --git a/tensorflow/python/framework/dtypes.py b/tensorflow/python/framework/dtypes.py
index 35b52ce..dd99b04 100644
--- a/tensorflow/python/framework/dtypes.py
+++ b/tensorflow/python/framework/dtypes.py
@@ -98,7 +98,9 @@
"""
if (self.is_quantized or
self.base_dtype in (bool, string, complex64, complex128)):
- raise TypeError("Cannot find minimum value of %s." % self)
+ raise TypeError(f"Cannot find minimum value of {self} with "
+ f"{'quantized type' if self.is_quantized else 'type'} "
+ f"{self.base_dtype}.")
# there is no simple way to get the min value of a dtype, we have to check
# float and int types separately
@@ -110,7 +112,7 @@
except:
if self.base_dtype == bfloat16:
return _np_bfloat16(float.fromhex("-0x1.FEp127"))
- raise TypeError("Cannot find minimum value of %s." % self)
+ raise TypeError(f"Cannot find minimum value of {self}.")
@property
def max(self):
@@ -122,7 +124,9 @@
"""
if (self.is_quantized or
self.base_dtype in (bool, string, complex64, complex128)):
- raise TypeError("Cannot find maximum value of %s." % self)
+ raise TypeError(f"Cannot find maximum value of {self} with "
+ f"{'quantized type' if self.is_quantized else 'type'} "
+ f"{self.base_dtype}.")
# there is no simple way to get the max value of a dtype, we have to check
# float and int types separately
@@ -134,7 +138,7 @@
except:
if self.base_dtype == bfloat16:
return _np_bfloat16(float.fromhex("0x1.FEp127"))
- raise TypeError("Cannot find maximum value of %s." % self)
+ raise TypeError(f"Cannot find maximum value of {self}.")
@property
def limits(self, clip_negative=True):
@@ -718,5 +722,4 @@
if isinstance(type_value, _dtypes.DType):
return _INTERN_TABLE[type_value.as_datatype_enum]
- raise TypeError("Cannot convert value %r to a TensorFlow DType." %
- (type_value,))
+ raise TypeError(f"Cannot convert value {type_value!r} to a TensorFlow DType.")