Skip to content

Commit ddcb81d

Browse files
authored
refine error message of input TypeError in framework.py (#19970) (#20648)
test=release/1.6
1 parent c33312f commit ddcb81d

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

python/paddle/fluid/framework.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1756,9 +1756,12 @@ def find_name(var_list, name):
17561756
elif isinstance(arg, Variable):
17571757
in_arg_names.append(cpt.to_text(arg.name))
17581758
else:
1759-
raise ValueError(
1760-
"not suprt args type , should be[ string_type, binary_type, Varibale]"
1761-
)
1759+
raise TypeError(
1760+
"The type of '%s' in operator %s should be "
1761+
"one of [basestring(), str, Varibale] in python2, "
1762+
"or one of [str, bytes, Variable] in python3."
1763+
"but received : " % (in_proto.name, type),
1764+
arg)
17621765
self.desc.set_input(in_proto.name, in_arg_names)
17631766
else:
17641767
self.desc.set_input(in_proto.name, [])

python/paddle/fluid/tests/unittests/test_compare_op.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
import op_test
1818
import unittest
1919
import numpy
20+
import paddle.fluid as fluid
21+
from paddle.fluid import Program, program_guard
2022

2123

2224
def create_test_class(op_type, typename, callback):
@@ -45,5 +47,16 @@ def test_output(self):
4547
create_test_class('equal', _type_name, lambda _a, _b: _a == _b)
4648
create_test_class('not_equal', _type_name, lambda _a, _b: _a != _b)
4749

50+
51+
class TestCompareOpError(op_test.OpTest):
52+
def test_errors(self):
53+
with program_guard(Program(), Program()):
54+
# The input x and y of compare_op must be Variable.
55+
x = fluid.layers.data(name='x', shape=[1], dtype="float32")
56+
y = fluid.create_lod_tensor(
57+
numpy.array([[-1]]), [[1]], fluid.CPUPlace())
58+
self.assertRaises(TypeError, fluid.layers.greater_equal, x, y)
59+
60+
4861
if __name__ == '__main__':
4962
unittest.main()

0 commit comments

Comments
 (0)