test_docstring_checker.py 7.6 KB
Newer Older
G
gongweibao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#   Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

15
import astroid
G
gongweibao 已提交
16 17 18 19 20 21 22 23
import docstring_checker
import pylint.testutils


class TestDocstring(pylint.testutils.CheckerTestCase):
    CHECKER_CLASS = docstring_checker.DocstringChecker

    def test_one_line(self):
24 25
        func_node = astroid.extract_node(
            '''
26 27
        def test():
            """get
G
gongweibao 已提交
28 29 30 31 32
            news.
            """
            if True:
                return 5
            return 5
33 34
        '''
        )
G
gongweibao 已提交
35 36 37 38 39 40

        self.checker.visit_functiondef(func_node)
        got = self.linter.release_messages()
        assert len(got) == 1
        assert 'W9001' == got[0][0]

Z
zhangchunle 已提交
41
    def test_one_line_1(self):
42 43
        func_node = astroid.extract_node(
            '''
44
        def test():
G
gongweibao 已提交
45 46 47 48
            """get news"""
            if True:
                return 5
            return 5
49 50
        '''
        )
G
gongweibao 已提交
51 52 53 54 55 56 57

        self.checker.visit_functiondef(func_node)
        got = self.linter.release_messages()
        assert len(got) == 1
        assert 'W9002' == got[0][0]

    def test_args(self):
58 59
        func_node = astroid.extract_node(
            '''
60
        def test(scale, mean):
G
gongweibao 已提交
61 62 63 64 65 66 67 68 69 70 71
            """get news.
            Args:
                scale (int): scale is the number.
            """
            mean=scale
            mean=scale
            mean=scale
            mean=scale
            mean=scale
            mean=scale
            mean=scale
72 73
        '''
        )
G
gongweibao 已提交
74 75 76 77 78 79 80

        self.checker.visit_functiondef(func_node)
        got = self.linter.release_messages()
        assert len(got) == 1
        assert 'W9003' == got[0][0]

    def test_missing(self):
81 82
        func_node = astroid.extract_node(
            '''
83
        def test():
G
gongweibao 已提交
84 85 86 87 88 89 90 91 92 93 94
            mean=scale
            mean=scale
            mean=scale
            mean=scale
            mean=scale
            mean=scale
            mean=scale
            mean=scale
            mean=scale
            mean=scale
            mean=scale
95 96
        '''
        )
G
gongweibao 已提交
97 98 99 100 101 102 103

        self.checker.visit_functiondef(func_node)
        got = self.linter.release_messages()
        assert len(got) == 1
        assert 'W9005' == got[0][0]

    def test_indent(self):
104 105
        func_node = astroid.extract_node(
            '''
106
        def test():
G
gongweibao 已提交
107 108 109
            """ get get get get get get get get
              get get get get get get get get.
            """
110
            pass
111 112
        '''
        )
G
gongweibao 已提交
113 114 115 116 117 118 119

        self.checker.visit_functiondef(func_node)
        got = self.linter.release_messages()
        assert len(got) == 1
        assert 'W9006' == got[0][0]

    def test_with_resturns(self):
120 121
        func_node = astroid.extract_node(
            '''
122
        def test():
G
gongweibao 已提交
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
            """get news.
            Args:
                scale (int): scale is the number.
            """
            mean=scale
            mean=scale
            mean=scale
            mean=scale
            mean=scale
            mean=scale
            mean=scale
            mean=scale
            mean=scale
            mean=scale
            mean=scale
            return mean
139 140
        '''
        )
G
gongweibao 已提交
141 142 143 144 145 146 147

        self.checker.visit_functiondef(func_node)
        got = self.linter.release_messages()
        assert len(got) == 1
        assert 'W9007' == got[0][0]

    def test_with_raises(self):
148 149
        func_node = astroid.extract_node(
            '''
150
        def test():
G
gongweibao 已提交
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
            """get news.
            Args:
                scale (int): scale is the number.
            """
            mean=scale
            mean=scale
            mean=scale
            mean=scale
            mean=scale
            mean=scale
            mean=scale
            mean=scale
            mean=scale
            mean=scale
            mean=scale
            raise ValueError('A very specific bad thing happened.')
167 168
        '''
        )
G
gongweibao 已提交
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220

        self.checker.visit_functiondef(func_node)
        got = self.linter.release_messages()
        assert len(got) == 1
        assert 'W9008' == got[0][0]

    def test_no_message(self):
        p = '''
def fc(input,
       size,
       num_flatten_dims=1,
       param_attr=None,
       bias_attr=None,
       act=None,
       name=None):
    """
    **Fully Connected Layer**
    The fully connected layer can take multiple tensors as its inputs. It
    creates a variable called weights for each input tensor, which represents
    a fully connected weight matrix from each input unit to each output unit.
    The fully connected layer multiplies each input tensor with its coresponding
    weight to produce an output Tensor. If multiple input tensors are given,
    the results of multiple multiplications will be sumed up. If bias_attr is
    not None, a bias variable will be created and added to the output. Finally,
    if activation is not None, it will be applied to the output as well.
    This process can be formulated as follows:

    Args:
        input (Variable|list of Variable): The input tensor(s) of this layer, and the dimension of
            the input tensor(s) is at least 2.
        size(int): The number of output units in this layer.
        num_flatten_dims (int, default 1): The fc layer can accept an input tensor with more than
            two dimensions. If this happens, the multidimensional tensor will first be flattened
            into a 2-dimensional matrix. The parameter `num_flatten_dims` determines how the input
            tensor is flattened: the first `num_flatten_dims` (inclusive, index starts from 1)
            dimensions will be flatten to form the first dimension of the final matrix (height of
            the matrix), and the rest `rank(X) - num_flatten_dims` dimensions are flattened to
            form the second dimension of the final matrix (width of the matrix). For example, suppose
            `X` is a 6-dimensional tensor with a shape [2, 3, 4, 5, 6], and `num_flatten_dims` = 3.
            Then, the flattened matrix will have a shape [2 x 3 x 4, 5 x 6] = [24, 30].
        param_attr (ParamAttr|list of ParamAttr, default None): The parameter attribute for learnable
            parameters/weights of this layer.
        bias_attr (ParamAttr|list of ParamAttr, default None): The parameter attribute for the bias
            of this layer. If it is set to None, no bias will be added to the output units.
        act (str, default None): Activation to be applied to the output of this layer.
        name (str, default None): The name of this layer.
    Returns:
        A tensor variable storing the transformation result.
    Raises:
        ValueError: If rank of the input tensor is less than 2.
    Examples:
        .. code-block:: python
G
GGBond8488 已提交
221
            data = paddle.static.data(name="data", shape=[-1, 32, 32], dtype="float32")
C
Charles-hit 已提交
222
            fc = paddle.static.nn.fc(x=data, size=1000, activation="tanh")
G
gongweibao 已提交
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
    """
    raise ValueError('A very specific bad thing happened.')
    size = 1
    size = 1
    size = 1
    size = 1
    size = 1
    size = 1
    size = 1
    size = 1
    size = 1
    size = 1
    size = 1
    size = 1
    size = 1
    return size
    '''

        func_node = astroid.extract_node(p)
        self.checker.visit_functiondef(func_node)
        got = self.linter.release_messages()
        assert len(got) == 0