From f9a384456af5f24774b19bf3985f5513cf6908f6 Mon Sep 17 00:00:00 2001 From: Margaret_wangrui Date: Wed, 24 Nov 2021 09:05:47 +0800 Subject: [PATCH] Add the check of function return None. --- mindspore/ccsrc/pipeline/jit/parse/parse.cc | 10 ++++++++++ tests/ut/cpp/pipeline/parse/parser_test.cc | 3 --- .../gtest_input/pipeline/parse/parser_test.py | 4 ---- tests/ut/python/ops/test_ops_check.py | 7 ++++--- .../python/pipeline/parse/test_grammar_constraints.py | 9 +++++---- tests/ut/python/pipeline/parse/test_super.py | 11 ++++++----- 6 files changed, 25 insertions(+), 19 deletions(-) diff --git a/mindspore/ccsrc/pipeline/jit/parse/parse.cc b/mindspore/ccsrc/pipeline/jit/parse/parse.cc index d5822fc6dae..83a2bd0c96a 100644 --- a/mindspore/ccsrc/pipeline/jit/parse/parse.cc +++ b/mindspore/ccsrc/pipeline/jit/parse/parse.cc @@ -480,6 +480,16 @@ FunctionBlockPtr Parser::ParseReturn(const FunctionBlockPtr &block, const py::ob return_expr_node = HandleInterpret(block, return_expr_node, value_object); // Create the `return` CNode. auto func_graph = block->func_graph(); + if (IsValueNode(return_expr_node)) { + py::list ret = ast_->CallParserObjMethod(PYTHON_PARSE_GET_LOCATION, node); + const auto min_list_size = 2; + if (ret.size() < min_list_size) { + MS_LOG(EXCEPTION) << "list size:" << ret.size() << " is less than 2."; + } + py::str desc = + python_adapter::CallPyModFn(ast_->module(), PYTHON_MOD_GET_OBJECT_DESCRIPTION, ast_->function(), ret[0], ret[1]); + MS_EXCEPTION(TypeError) << "Function should not 'Return None', is located in:" << desc.cast(); + } CNodePtr return_cnode = func_graph->NewCNodeInOrder({NewValueNode(prim::kPrimReturn), return_expr_node}); func_graph->set_return(return_cnode); return block; diff --git a/tests/ut/cpp/pipeline/parse/parser_test.cc b/tests/ut/cpp/pipeline/parse/parser_test.cc index 7de484824af..7629342d8d1 100644 --- a/tests/ut/cpp/pipeline/parse/parser_test.cc +++ b/tests/ut/cpp/pipeline/parse/parser_test.cc @@ -146,9 +146,6 @@ TEST_F(TestParser, TestParseGraphNamedConst) { GetPythonFunction("testDoNamedConstFalse"); ret_val = ParsePythonCode(fn); ASSERT_TRUE(nullptr != ret_val); - GetPythonFunction("testDoNamedConstNone"); - ret_val = ParsePythonCode(fn); - ASSERT_TRUE(nullptr != ret_val); } TEST_F(TestParser, TestParseGraphForStatement) { diff --git a/tests/ut/cpp/python_input/gtest_input/pipeline/parse/parser_test.py b/tests/ut/cpp/python_input/gtest_input/pipeline/parse/parser_test.py index bb0815831bd..6d51f4b38aa 100644 --- a/tests/ut/cpp/python_input/gtest_input/pipeline/parse/parser_test.py +++ b/tests/ut/cpp/python_input/gtest_input/pipeline/parse/parser_test.py @@ -221,10 +221,6 @@ def testDoNamedConstFalse(): return False -def testDoNamedConstNone(): - return None - - # Test_Class_type @dataclass class TestFoo: diff --git a/tests/ut/python/ops/test_ops_check.py b/tests/ut/python/ops/test_ops_check.py index 5871b450652..43a0ecd3398 100644 --- a/tests/ut/python/ops/test_ops_check.py +++ b/tests/ut/python/ops/test_ops_check.py @@ -61,9 +61,10 @@ class NetMissConstruct(nn.Cell): def test_net_without_construct(): """ test_net_without_construct """ - net = NetMissConstruct() - inp = Tensor(np.ones([1, 1, 32, 32]).astype(np.float32)) - _cell_graph_executor.compile(net, inp) + with pytest.raises(TypeError, match="Function should not 'Return None'"): + net = NetMissConstruct() + inp = Tensor(np.ones([1, 1, 32, 32]).astype(np.float32)) + _cell_graph_executor.compile(net, inp) class NetWithRaise(nn.Cell): diff --git a/tests/ut/python/pipeline/parse/test_grammar_constraints.py b/tests/ut/python/pipeline/parse/test_grammar_constraints.py index 568a0d4bcc7..19aa9b0a70d 100644 --- a/tests/ut/python/pipeline/parse/test_grammar_constraints.py +++ b/tests/ut/python/pipeline/parse/test_grammar_constraints.py @@ -192,7 +192,8 @@ def test_missing_construct(): def construct1(self, inp): return 5 - np_input = np.arange(2 * 3 * 4).reshape((2, 3, 4)).astype(np.bool_) - tensor = Tensor(np_input) - net = NetMissConstruct() - assert net(tensor) is None + with pytest.raises(TypeError, match="Function should not 'Return None'"): + np_input = np.arange(2 * 3 * 4).reshape((2, 3, 4)).astype(np.bool_) + tensor = Tensor(np_input) + net = NetMissConstruct() + assert net(tensor) is None diff --git a/tests/ut/python/pipeline/parse/test_super.py b/tests/ut/python/pipeline/parse/test_super.py index d68fa5cb1c3..a8a1240cd1a 100644 --- a/tests/ut/python/pipeline/parse/test_super.py +++ b/tests/ut/python/pipeline/parse/test_super.py @@ -14,7 +14,7 @@ # ============================================================================ """ test super""" import numpy as np - +import pytest import mindspore.nn as nn from mindspore import Tensor from mindspore import context @@ -104,10 +104,11 @@ def test_mul_super(): def test_super_cell(): - net = Net(2) - x = Tensor(np.ones([1, 2, 3], np.int32)) - y = Tensor(np.ones([1, 2, 3], np.int32)) - assert net(x, y) is None + with pytest.raises(TypeError, match="Function should not 'Return None'"): + net = Net(2) + x = Tensor(np.ones([1, 2, 3], np.int32)) + y = Tensor(np.ones([1, 2, 3], np.int32)) + assert net(x, y) is None def test_single_super_in():