Merge pull request #25821 from yongtang:25790-tf.keras

PiperOrigin-RevId: 234655827
This commit is contained in:
TensorFlower Gardener 2019-02-19 13:12:59 -08:00
commit 0b4cfb42a6
3 changed files with 75 additions and 0 deletions

View File

@ -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`.

View File

@ -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()

View File

@ -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):