Exemple #1
0
def test_return_value_is_filled_dict_by_keyword():
    # when method return dict(a='b')
    try:
        assert ReturnedExpression(
            _ast.Return(
                value=_ast.Call(
                    func=_ast.Name(id='dict', ctx=_ast.Load()),
                    args=[],
                    keywords=[_ast.keyword(arg='a', value=_ast.Str(s='b'))],
                ),
                lineno=1,
            ), ).value_not_none() is True
    except (AttributeError):
        assert ReturnedExpression(
            _ast.Return(
                value=_ast.Call(
                    func=_ast.Name(id='dict', ctx=_ast.Load()),
                    args=[],
                    keywords=[
                        _ast.keyword(arg='a',
                                     value=_ast.JoinedStr(values=['a', 'b']))
                    ],
                ),
                lineno=1,
            ), ).value_not_none() is True
Exemple #2
0
def test_return_value_is_empty_string():
    # when method return ''
    try:
        assert ReturnedExpression(_ast.Return(
            value=_ast.Str(s=''), lineno=1)).value_not_none() is False
    except (AttributeError):
        pass
Exemple #3
0
def test_return_value_is_filled_string():
    # when method return 'test'
    try:
        assert ReturnedExpression(
            _ast.Return(value=_ast.Str(s='test'),
                        lineno=1)).value_not_none() is True
    except (AttributeError):
        pass
Exemple #4
0
def test_return_value_is_class_constant():
    # when method return SomeClass.CONSTANT
    assert ReturnedExpression(
        _ast.Return(
            value=_ast.Attribute(value=_ast.Name(id='SomeClass',
                                                 ctx=_ast.Load()),
                                 attr='CONSTANT',
                                 ctx=_ast.Load()),
            lineno=1,
        ), ).value_not_none() is True
Exemple #5
0
def test_return_value_is_empty_frozenset_by_keyword():
    # when method return frozenset()
    assert ReturnedExpression(
        _ast.Return(
            value=_ast.Call(
                func=_ast.Name(id='frozenset', ctx=_ast.Load()),
                args=[],
                keywords=[],
            ),
            lineno=1,
        ), ).value_not_none() is False
Exemple #6
0
def test_return_value_is_filled_frozenset_by_keyword():
    # when method return frozenset('1')
    try:
        assert ReturnedExpression(
            _ast.Return(
                value=_ast.Call(
                    func=_ast.Name(id='frozenset', ctx=_ast.Load()),
                    args=[_ast.Str(s='1')],
                    keywords=[],
                ),
                lineno=1,
            ), ).value_not_none() is True
    except (AttributeError):
        assert ReturnedExpression(
            _ast.Return(
                value=_ast.Call(
                    func=_ast.Name(id='frozenset', ctx=_ast.Load()),
                    args=[_ast.JoinedStr(values=['1'])],
                    keywords=[],
                ),
                lineno=1,
            ), ).value_not_none() is True
Exemple #7
0
def test_return_value_is_func():
    assert ReturnedExpression(
        _ast.Return(
            value=_ast.Call(
                func=_ast.Attribute(
                    value=_ast.Name(id='Function', ctx=_ast.Load()),
                    attr='staticm',
                    ctx=_ast.Load(),
                ),
                args=[],
                keywords=[],
            ),
            lineno=1,
        ), ).value_not_none() is True
Exemple #8
0
class ResultReturnValueFixture:
    none = FunctionParseResult(
        return_not_none=bool(FunctionLint.EMPTY_RETURNED_VALUE),
        line_number=-1,
    ).__dict__
    wrong = FunctionParseResult(
        return_not_none=bool(FunctionLint.EMPTY_RETURNED_VALUE),
        line_number=1,
    ).__dict__
    success = FunctionParseResult(
        return_not_none=ReturnedExpression(_ast.Return(
            lineno=1, value=None)).value_not_none(),
        line_number=_ast.Return(lineno=1, value=None).lineno,
    ).__dict__
Exemple #9
0
    def returned_value(self) -> FunctionParseResult:
        """
        return value of function "return" expression
        :return: FunctionParseResult object
        """

        node = self.definition
        # if function does not has body
        if not node:
            return FunctionParseResult(
                return_not_none=bool(FunctionLint.EMPTY_RETURNED_VALUE),
                line_number=-1,
            )

        # skip plain assignment expressions
        if isinstance(node, _ast.Assign):
            return FunctionParseResult(
                return_not_none=bool(FunctionLint.EMPTY_RETURNED_VALUE),
                line_number=node.lineno,
            )
        # skip plain pass expressions
        elif isinstance(node, _ast.Pass):
            return FunctionParseResult(
                return_not_none=bool(FunctionLint.EMPTY_RETURNED_VALUE),
                line_number=node.lineno,
            )
        # skip for any other plain expressions
        elif isinstance(node, _ast.Expr):
            return FunctionParseResult(
                return_not_none=bool(FunctionLint.EMPTY_RETURNED_VALUE),
                line_number=node.lineno,
            )
        # iterate over function body
        for item in node.body:
            # check, that expression has return type
            if isinstance(item, _ast.Return):
                return FunctionParseResult(
                    return_not_none=ReturnedExpression(item).value_not_none(),
                    line_number=item.lineno,
                )
        # in any other case - return empty
        return FunctionParseResult(
            return_not_none=bool(FunctionLint.EMPTY_RETURNED_VALUE),
            line_number=node.lineno,
        )
Exemple #10
0
def test_return_value_is_filled_joined_string():
    # when method return '{}{}{}'.format('a', 'b', 'c')
    assert ReturnedExpression(
        _ast.Return(value=_ast.JoinedStr(values=['a', 'b', 'c']),
                    lineno=1)).value_not_none() is True
Exemple #11
0
def test_return_value_is_none():
    # when method return None
    assert ReturnedExpression(
        _ast.Return(value=_ast.NameConstant(value=None),
                    lineno=1)).value_not_none() is False
Exemple #12
0
def test_return_value_is_empty_joined_string():
    # when method return ''
    assert ReturnedExpression(
        _ast.Return(value=_ast.JoinedStr(values=[]),
                    lineno=1)).value_not_none() is False
Exemple #13
0
def test_return_value_is_float_limit_to_zero():
    # when method return 0.0000000000000000000000001
    assert ReturnedExpression(
        _ast.Return(value=_ast.Num(n=0.0000000000000000000000001),
                    lineno=1), ).value_not_none() is True
Exemple #14
0
def test_return_value_is_empty_tuple():
    # when method return ()
    assert ReturnedExpression(
        _ast.Return(value=_ast.Tuple(elts=[], ctx=_ast.Load()),
                    lineno=1), ).value_not_none() is False
Exemple #15
0
def test_return_is_empty():
    # when method return nothing, not None, only 'return'
    assert ReturnedExpression(_ast.Return(
        value=None,
        lineno=1,
    ), ).value_not_none() is False
Exemple #16
0
def test_return_value_is_filled_tuple():
    # when method return ('1')
    assert ReturnedExpression(
        _ast.Return(value=_ast.Tuple(elts=['1'], ctx=_ast.Load()),
                    lineno=1), ).value_not_none() is True
Exemple #17
0
def test_return_value_is_int_zero():
    # when method return 0
    assert ReturnedExpression(_ast.Return(value=_ast.Num(n=0),
                                          lineno=1)).value_not_none() is True
Exemple #18
0
def test_return_value_is_empty_dict():
    # when method return {}
    assert ReturnedExpression(
        _ast.Return(value=_ast.Dict(keys=[], values=[]),
                    lineno=1), ).value_not_none() is False
Exemple #19
0
def test_return_value_is_filled_dict():
    # when method return {'1': '2'}
    assert ReturnedExpression(
        _ast.Return(value=_ast.Dict(keys=['1'], values=['2']),
                    lineno=1), ).value_not_none() is True