diff --git a/mypy/checker.py b/mypy/checker.py index 8244fffa21cf..acb1c4742df0 100644 --- a/mypy/checker.py +++ b/mypy/checker.py @@ -634,7 +634,7 @@ def is_implicit_any(t: Type) -> bool: unreachable = self.binder.is_unreachable() if (self.options.warn_no_return and not unreachable - and not isinstance(self.return_types[-1], (Void, AnyType)) + and not isinstance(self.return_types[-1], (Void, NoneTyp, AnyType)) and not defn.is_generator): # Control flow fell off the end of a function that was # declared to return a non-None type. diff --git a/test-data/unit/check-optional.test b/test-data/unit/check-optional.test index a477de3b3510..d33c4c8442ee 100644 --- a/test-data/unit/check-optional.test +++ b/test-data/unit/check-optional.test @@ -497,3 +497,15 @@ if x == y: else: reveal_type(x) # E: Revealed type is 'Union[builtins.str, builtins.int, builtins.None]' [builtins fixtures/ops.pyi] + + +[case testWarnNoReturnWorksWithStrictOptional] +# flags: --warn-no-return +def f() -> None: + 1 + 1 # no error + +def g() -> int: + 1 + 1 # +[out] +main: note: In function "g": +main:5: note: Missing return statement