From bcabbf0f23e6d87b7c2255d162f35dc1a82ab573 Mon Sep 17 00:00:00 2001 From: David Fisher Date: Wed, 26 Oct 2016 15:04:23 -0700 Subject: [PATCH] Fix --warn-no-return for docstring only functions --- mypy/checker.py | 5 ++++- test-data/unit/check-flags.test | 15 +++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/mypy/checker.py b/mypy/checker.py index 513a6930a382..5a7176f5c7cc 100644 --- a/mypy/checker.py +++ b/mypy/checker.py @@ -651,7 +651,10 @@ def is_trivial_body(self, block: Block) -> bool: isinstance(body[0].expr, StrExpr)): body = block.body[1:] - if len(body) != 1: + if len(body) == 0: + # There's only a docstring. + return True + elif len(body) > 1: return False stmt = body[0] return (isinstance(stmt, PassStmt) or diff --git a/test-data/unit/check-flags.test b/test-data/unit/check-flags.test index 4e51f2940a30..86476b6e55ce 100644 --- a/test-data/unit/check-flags.test +++ b/test-data/unit/check-flags.test @@ -79,3 +79,18 @@ class BaseClass: pass [out] tmp/main.py:2: error: Class cannot subclass 'BaseClass' (has type 'Any') + +[case testWarnNoReturnIgnoresTrivialFunctions] +# flags: --warn-no-return +def f() -> int: + pass +def g() -> int: + ... +def h() -> int: + """with docstring""" + pass +def i() -> int: + """with docstring""" + ... +def j() -> int: + """docstring only"""