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"""