From 7d0992cf1f7e96fddf9a25546b21f47b72a4ddba Mon Sep 17 00:00:00 2001 From: Rushabh Vasani Date: Sun, 9 Feb 2020 22:58:22 +0530 Subject: [PATCH 1/3] add error msg and test --- tensorflow/python/kernel_tests/cwise_ops_test.py | 9 +++++++++ tensorflow/python/ops/math_ops.py | 3 +++ 2 files changed, 12 insertions(+) diff --git a/tensorflow/python/kernel_tests/cwise_ops_test.py b/tensorflow/python/kernel_tests/cwise_ops_test.py index f762cec86682ed..b004cfb927ca77 100644 --- a/tensorflow/python/kernel_tests/cwise_ops_test.py +++ b/tensorflow/python/kernel_tests/cwise_ops_test.py @@ -20,6 +20,8 @@ import numpy as np +import pytest + from tensorflow.python.compat import compat from tensorflow.python.framework import constant_op from tensorflow.python.framework import dtypes as dtypes_lib @@ -1228,6 +1230,13 @@ def testEmpty(self): tf_val = math_ops.polyval(coeffs, x) self.assertAllClose(np_val, self.evaluate(tf_val)) + def test_coeffs_raise(self): + x = np.random.rand(2, 2).astype(np.float32) + coeffs = {} + msg = "Argument coeffs must be list type." + with pytest.raises(ValueError, match=msg): + math_ops.polyval(coeffs, x) + class SingularGradientOpTest(test.TestCase): diff --git a/tensorflow/python/ops/math_ops.py b/tensorflow/python/ops/math_ops.py index bcc009b1e68434..f0554850ff55a4 100644 --- a/tensorflow/python/ops/math_ops.py +++ b/tensorflow/python/ops/math_ops.py @@ -4290,6 +4290,9 @@ def polyval(coeffs, x, name=None): Equivalent to numpy.polyval. @end_compatibility """ + if not isinstance(coeffs, list): + raise ValueError("Argument coeffs must be list type " + "found {}.".format(type(coeffs))) with ops.name_scope(name, "polyval", nest.flatten(coeffs) + [x]) as name: x = ops.convert_to_tensor(x, name="x") From 4dd1401a2054f4fd63b29659ac55517d17d3c242 Mon Sep 17 00:00:00 2001 From: Rushabh Vasani Date: Tue, 11 Feb 2020 19:38:21 +0530 Subject: [PATCH 2/3] use self.assertRaises --- tensorflow/python/kernel_tests/cwise_ops_test.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/tensorflow/python/kernel_tests/cwise_ops_test.py b/tensorflow/python/kernel_tests/cwise_ops_test.py index b004cfb927ca77..21f5cf22f18f30 100644 --- a/tensorflow/python/kernel_tests/cwise_ops_test.py +++ b/tensorflow/python/kernel_tests/cwise_ops_test.py @@ -20,8 +20,6 @@ import numpy as np -import pytest - from tensorflow.python.compat import compat from tensorflow.python.framework import constant_op from tensorflow.python.framework import dtypes as dtypes_lib @@ -1233,8 +1231,7 @@ def testEmpty(self): def test_coeffs_raise(self): x = np.random.rand(2, 2).astype(np.float32) coeffs = {} - msg = "Argument coeffs must be list type." - with pytest.raises(ValueError, match=msg): + with self.assertRaises(ValueError): math_ops.polyval(coeffs, x) From b664a40dbdddd11b0dcd05b30598358193e2bfe7 Mon Sep 17 00:00:00 2001 From: Rushabh Vasani Date: Wed, 12 Feb 2020 00:13:00 +0530 Subject: [PATCH 3/3] add msg check --- tensorflow/python/kernel_tests/cwise_ops_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensorflow/python/kernel_tests/cwise_ops_test.py b/tensorflow/python/kernel_tests/cwise_ops_test.py index 21f5cf22f18f30..6420ed435bd240 100644 --- a/tensorflow/python/kernel_tests/cwise_ops_test.py +++ b/tensorflow/python/kernel_tests/cwise_ops_test.py @@ -1231,7 +1231,7 @@ def testEmpty(self): def test_coeffs_raise(self): x = np.random.rand(2, 2).astype(np.float32) coeffs = {} - with self.assertRaises(ValueError): + with self.assertRaisesRegexp(ValueError, "Argument coeffs must be list"): math_ops.polyval(coeffs, x)