@@ -1217,7 +1217,7 @@ def check_func_def(self, defn: FuncItem, typ: CallableType, name: Optional[str])
12171217 self .accept (item .body )
12181218 unreachable = self .binder .is_unreachable ()
12191219
1220- if self . options . warn_no_return and not unreachable :
1220+ if not unreachable and not body_is_trivial :
12211221 if defn .is_generator or is_named_instance (
12221222 self .return_types [- 1 ], "typing.AwaitableGenerator"
12231223 ):
@@ -1228,17 +1228,29 @@ def check_func_def(self, defn: FuncItem, typ: CallableType, name: Optional[str])
12281228 return_type = self .get_coroutine_return_type (self .return_types [- 1 ])
12291229 else :
12301230 return_type = self .return_types [- 1 ]
1231-
12321231 return_type = get_proper_type (return_type )
1233- if not isinstance (return_type , (NoneType , AnyType )) and not body_is_trivial :
1234- # Control flow fell off the end of a function that was
1235- # declared to return a non-None type and is not
1236- # entirely pass/Ellipsis/raise NotImplementedError.
1237- if isinstance (return_type , UninhabitedType ):
1238- # This is a NoReturn function
1239- self .fail (message_registry .INVALID_IMPLICIT_RETURN , defn )
1240- else :
1241- self .fail (message_registry .MISSING_RETURN_STATEMENT , defn )
1232+
1233+ if self .options .warn_no_return :
1234+ if not isinstance (return_type , (NoneType , AnyType )):
1235+ # Control flow fell off the end of a function that was
1236+ # declared to return a non-None type and is not
1237+ # entirely pass/Ellipsis/raise NotImplementedError.
1238+ if isinstance (return_type , UninhabitedType ):
1239+ # This is a NoReturn function
1240+ self .fail (message_registry .INVALID_IMPLICIT_RETURN , defn )
1241+ else :
1242+ self .fail (message_registry .MISSING_RETURN_STATEMENT , defn )
1243+ else :
1244+ # similar to code in check_return_stmt
1245+ self .check_subtype (
1246+ subtype_label = "implicitly returns" ,
1247+ subtype = NoneType (),
1248+ supertype_label = "expected" ,
1249+ supertype = return_type ,
1250+ context = defn ,
1251+ msg = message_registry .INCOMPATIBLE_RETURN_VALUE_TYPE ,
1252+ code = codes .RETURN_VALUE ,
1253+ )
12421254
12431255 self .return_types .pop ()
12441256
0 commit comments