diff --git a/tensorflow/python/util/nest.py b/tensorflow/python/util/nest.py index 66f43a3d682..e072bebe6f2 100644 --- a/tensorflow/python/util/nest.py +++ b/tensorflow/python/util/nest.py @@ -392,6 +392,10 @@ def assert_same_structure(nest1, nest2, check_types=True, TypeError: If the two structures differ in the type of sequence in any of their substructures. Only possible if `check_types` is `True`. """ + # Convert to bool explicitly as otherwise pybind will not be able# to handle + # type mismatch message correctly. See GitHub issue 42329 for details. + check_types = bool(check_types) + expand_composites = bool(expand_composites) try: _pywrap_utils.AssertSameStructure(nest1, nest2, check_types, expand_composites) diff --git a/tensorflow/python/util/nest_test.py b/tensorflow/python/util/nest_test.py index fb3f2102ba7..7f8bb247792 100644 --- a/tensorflow/python/util/nest_test.py +++ b/tensorflow/python/util/nest_test.py @@ -1218,6 +1218,18 @@ class NestTest(parameterized.TestCase, test.TestCase): expected, ) + def testInvalidCheckTypes(self): + with self.assertRaises((ValueError, TypeError)): + nest.assert_same_structure( + nest1=array_ops.zeros((1)), + nest2=array_ops.ones((1, 1, 1)), + check_types=array_ops.ones((2))) + with self.assertRaises((ValueError, TypeError)): + nest.assert_same_structure( + nest1=array_ops.zeros((1)), + nest2=array_ops.ones((1, 1, 1)), + expand_composites=array_ops.ones((2))) + class NestBenchmark(test.Benchmark):