diff --git a/tensorflow/python/framework/tensor_shape.py b/tensorflow/python/framework/tensor_shape.py index 40fccc86a34..0dc3dde4f6e 100644 --- a/tensorflow/python/framework/tensor_shape.py +++ b/tensorflow/python/framework/tensor_shape.py @@ -468,6 +468,54 @@ class Dimension(object): """ return self // other + def __rdiv__(self, other): + """Use `__floordiv__` via `x // y` instead. + + This function exists only to have a better error message. Instead of: + `TypeError: unsupported operand type(s) for /: 'int' and 'Dimension'`, + this function will explicitly call for usage of `//` instead. + + Args: + other: Another `Dimension`. + + Raises: + TypeError. + """ + raise TypeError("unsupported operand type(s) for /: '{}' and 'Dimension', " + "please use // instead".format(type(other).__name__)) + + def __truediv__(self, other): + """Use `__floordiv__` via `x // y` instead. + + This function exists only to have a better error message. Instead of: + `TypeError: unsupported operand type(s) for /: 'Dimension' and 'int'`, + this function will explicitly call for usage of `//` instead. + + Args: + other: Another `Dimension`. + + Raises: + TypeError. + """ + raise TypeError("unsupported operand type(s) for /: 'Dimension' and '{}', " + "please use // instead".format(type(other).__name__)) + + def __rtruediv__(self, other): + """Use `__floordiv__` via `x // y` instead. + + This function exists only to have a better error message. Instead of: + `TypeError: unsupported operand type(s) for /: 'int' and 'Dimension'`, + this function will explicitly call for usage of `//` instead. + + Args: + other: Another `Dimension`. + + Raises: + TypeError. + """ + raise TypeError("unsupported operand type(s) for /: '{}' and 'Dimension', " + "please use // instead".format(type(other).__name__)) + def __mod__(self, other): """Returns `self` modulo `other`. diff --git a/tensorflow/python/framework/tensor_shape_div_test.py b/tensorflow/python/framework/tensor_shape_div_test.py index 8e63d7f5470..5160c75e527 100644 --- a/tensorflow/python/framework/tensor_shape_div_test.py +++ b/tensorflow/python/framework/tensor_shape_div_test.py @@ -35,6 +35,16 @@ class DimensionDivTest(test_util.TensorFlowTestCase): for y in values: self.assertEqual((x / y).value, (x // y).value) + def testRDivFail(self): + # Note: This test is related to GitHub issue 25790. + """Without from __future__ import division, __rdiv__ is used.""" + if six.PY2: # Old division exists only in Python 2 + two = tensor_shape.Dimension(2) + message = (r"unsupported operand type\(s\) for /: " + r"'int' and 'Dimension', please use // instead") + with self.assertRaisesRegexp(TypeError, message): + _ = 6 / two + if __name__ == "__main__": googletest.main() diff --git a/tensorflow/python/framework/tensor_shape_test.py b/tensorflow/python/framework/tensor_shape_test.py index 7d85e0a99e6..b4a37c05a83 100644 --- a/tensorflow/python/framework/tensor_shape_test.py +++ b/tensorflow/python/framework/tensor_shape_test.py @@ -205,6 +205,23 @@ class DimensionTest(test_util.TensorFlowTestCase): reconstructed = ctor(*args) self.assertEquals(reconstructed, dim) + def testDiv(self): + # Note: This test is related to GitHub issue 25790. + six = tensor_shape.Dimension(6) + two = tensor_shape.Dimension(2) + message = (r"unsupported operand type\(s\) for /: " + r"'Dimension' and 'Dimension', please use // instead") + with self.assertRaisesRegexp(TypeError, message): + _ = six / two + message = (r"unsupported operand type\(s\) for /: " + r"'Dimension' and 'int', please use // instead") + with self.assertRaisesRegexp(TypeError, message): + _ = six / 2 + message = (r"unsupported operand type\(s\) for /: " + r"'int' and 'Dimension', please use // instead") + with self.assertRaisesRegexp(TypeError, message): + _ = 6 / two + class ShapeTest(test_util.TensorFlowTestCase):