diff --git a/deal/linter/_rules.py b/deal/linter/_rules.py index 78331cf3..fe626ab4 100644 --- a/deal/linter/_rules.py +++ b/deal/linter/_rules.py @@ -163,7 +163,7 @@ def _check(self, func: Func, stubs: StubsManager = None) -> Iterator[Error]: yield Error( code=self.code, text=self.message, - value='return', + value='no return', row=func.line, col=func.col, ) diff --git a/tests/test_linter/test_extractors/test_returns.py b/tests/test_linter/test_extractors/test_returns.py index 4c2a1335..58adafbd 100644 --- a/tests/test_linter/test_extractors/test_returns.py +++ b/tests/test_linter/test_extractors/test_returns.py @@ -6,7 +6,7 @@ import pytest # project -from deal.linter._extractors import get_returns +from deal.linter._extractors import get_returns, has_returns @pytest.mark.parametrize('text, expected', [ @@ -57,3 +57,21 @@ def test_get_returns_inference(text, expected): print(tree.repr_tree()) returns = tuple(r.value for r in get_returns(body=tree.body)) assert returns == expected + + +@pytest.mark.parametrize('text, expected', [ + ('return', True), + ('return 1', True), + ('if b:\n return 1', True), + ('yield 1', True), + ('if b:\n yield 1', True), + ('1 + 2', False), +]) +def test_has_returns(text, expected): + tree = ast.parse(text) + print(ast.dump(tree)) + assert has_returns(body=tree.body) is expected + + tree = astroid.parse(text) + print(tree.repr_tree()) + assert has_returns(body=tree.body) is expected diff --git a/tests/test_linter/test_rules.py b/tests/test_linter/test_rules.py index 0f842c87..9d4ab7fd 100644 --- a/tests/test_linter/test_rules.py +++ b/tests/test_linter/test_rules.py @@ -158,6 +158,23 @@ def test(a): assert actual == expected +def test_check_pure_no_returns(): + checker = CheckPure() + text = """ + @deal.pure + def test(a): + a + 3 + """ + text = dedent(text).strip() + funcs1 = Func.from_ast(ast.parse(text)) + funcs2 = Func.from_astroid(astroid.parse(text)) + for func in (funcs1[0], funcs2[0]): + actual = [tuple(err) for err in checker(func)] + assert len(actual) == 1 + expected = 'DEAL014 pure contract error (no return)' + assert actual[0][2] == expected + + def test_check_asserts(): checker = CheckAsserts() text = """