From 7e5d6c25f4ce9b97166902c79b5d40c791dc119c Mon Sep 17 00:00:00 2001 From: Yong Tang Date: Mon, 18 Feb 2019 00:25:24 +0000 Subject: [PATCH] Add a better error message when unsupported ops are used for Dimension This fix is related to 25790. In 25790, `Dimension` with division will thrown an TypeError: ``` output_dim = input_shape[-1] / 2 ... TypeError: unsupported operand type(s) for /: 'Dimension' and 'int' ``` The error is expected, as Dimension only support `//` (not `/`). However, for backward compatible reasons, `__div__`(`/`) is actually implemented (but deprecated). See code. This issue causes confusion and it has been mentioned multiple times in GitHub and StackOverflow(https://stackoverflow.com/questions/51692253/typeerror-unsupported-operand-types-for-dimension-and-int) This fix is an attempt to print a better error message with: ``` TypeError: unsupported operand type(s) for /: 'Dimension' and 'int', please use // instead ``` Note that this PR does not change any current behavior, and have no impact with respect to backward-compatible `__div__`. It merely adds additional notes in the error message. Signed-off-by: Yong Tang --- tensorflow/python/framework/tensor_shape.py | 48 +++++++++++++++++++++ 1 file changed, 48 insertions(+) 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`.