Skip to content

Commit c17b8bd

Browse files
authored
Fix interaction of --warn-no-return and --strict-optional (#2371)
1 parent 0c9ed7f commit c17b8bd

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

mypy/checker.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,7 @@ def is_implicit_any(t: Type) -> bool:
634634
unreachable = self.binder.is_unreachable()
635635

636636
if (self.options.warn_no_return and not unreachable
637-
and not isinstance(self.return_types[-1], (Void, AnyType))
637+
and not isinstance(self.return_types[-1], (Void, NoneTyp, AnyType))
638638
and not defn.is_generator):
639639
# Control flow fell off the end of a function that was
640640
# declared to return a non-None type.

test-data/unit/check-optional.test

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,3 +497,15 @@ if x == y:
497497
else:
498498
reveal_type(x) # E: Revealed type is 'Union[builtins.str, builtins.int, builtins.None]'
499499
[builtins fixtures/ops.pyi]
500+
501+
502+
[case testWarnNoReturnWorksWithStrictOptional]
503+
# flags: --warn-no-return
504+
def f() -> None:
505+
1 + 1 # no error
506+
507+
def g() -> int:
508+
1 + 1 #
509+
[out]
510+
main: note: In function "g":
511+
main:5: note: Missing return statement

0 commit comments

Comments
 (0)