From ddcb81d14ad70cc264abfca9be1b6711a49e5af8 Mon Sep 17 00:00:00 2001 From: Tao Luo Date: Wed, 16 Oct 2019 10:59:03 +0800 Subject: [PATCH] refine error message of input TypeError in framework.py (#19970) (#20648) test=release/1.6 --- python/paddle/fluid/framework.py | 9 ++++++--- .../paddle/fluid/tests/unittests/test_compare_op.py | 13 +++++++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/python/paddle/fluid/framework.py b/python/paddle/fluid/framework.py index 740e86856ad..610dfca8737 100644 --- a/python/paddle/fluid/framework.py +++ b/python/paddle/fluid/framework.py @@ -1756,9 +1756,12 @@ class Operator(object): elif isinstance(arg, Variable): in_arg_names.append(cpt.to_text(arg.name)) else: - raise ValueError( - "not suprt args type , should be[ string_type, binary_type, Varibale]" - ) + raise TypeError( + "The type of '%s' in operator %s should be " + "one of [basestring(), str, Varibale] in python2, " + "or one of [str, bytes, Variable] in python3." + "but received : " % (in_proto.name, type), + arg) self.desc.set_input(in_proto.name, in_arg_names) else: self.desc.set_input(in_proto.name, []) diff --git a/python/paddle/fluid/tests/unittests/test_compare_op.py b/python/paddle/fluid/tests/unittests/test_compare_op.py index 437ad35538a..d990b093ccb 100644 --- a/python/paddle/fluid/tests/unittests/test_compare_op.py +++ b/python/paddle/fluid/tests/unittests/test_compare_op.py @@ -17,6 +17,8 @@ from __future__ import print_function import op_test import unittest import numpy +import paddle.fluid as fluid +from paddle.fluid import Program, program_guard def create_test_class(op_type, typename, callback): @@ -45,5 +47,16 @@ for _type_name in {'float32', 'float64', 'int32', 'int64'}: create_test_class('equal', _type_name, lambda _a, _b: _a == _b) create_test_class('not_equal', _type_name, lambda _a, _b: _a != _b) + +class TestCompareOpError(op_test.OpTest): + def test_errors(self): + with program_guard(Program(), Program()): + # The input x and y of compare_op must be Variable. + x = fluid.layers.data(name='x', shape=[1], dtype="float32") + y = fluid.create_lod_tensor( + numpy.array([[-1]]), [[1]], fluid.CPUPlace()) + self.assertRaises(TypeError, fluid.layers.greater_equal, x, y) + + if __name__ == '__main__': unittest.main() -- GitLab