@@ -4895,6 +4895,10 @@ def infer_context_dependent(
48954895 return typ
48964896
48974897 def check_return_stmt (self , s : ReturnStmt ) -> None :
4898+
4899+ def is_notimplemented (t : object ) -> bool :
4900+ return isinstance (t , Instance ) and t .type .fullname == "builtins._NotImplementedType"
4901+
48984902 defn = self .scope .current_function ()
48994903 if defn is not None :
49004904 if defn .is_generator :
@@ -4946,6 +4950,7 @@ def check_return_stmt(self, s: ReturnStmt) -> None:
49464950 if defn .is_async_generator :
49474951 self .fail (message_registry .RETURN_IN_ASYNC_GENERATOR , s )
49484952 return
4953+
49494954 # Returning a value of type Any is always fine.
49504955 if isinstance (typ , AnyType ):
49514956 # (Unless you asked to be warned in that case, and the
@@ -4971,18 +4976,18 @@ def check_return_stmt(self, s: ReturnStmt) -> None:
49714976 if is_lambda or isinstance (typ , NoneType ):
49724977 return
49734978 self .fail (message_registry .NO_RETURN_VALUE_EXPECTED , s )
4974- elif (
4975- isinstance (typ , Instance )
4976- and typ .type .fullname == "builtins._NotImplementedType"
4977- and (
4978- (defn .name in BINARY_MAGIC_METHODS or defn .name == "__subclasshook__" )
4979- )
4980- ):
4981- return
49824979 else :
4980+ typ_ : Type = typ
4981+ if defn .name in BINARY_MAGIC_METHODS or defn .name == "__subclasshook__" :
4982+ if is_notimplemented (typ ):
4983+ return
4984+ if isinstance (typ , UnionType ):
4985+ typ_ = UnionType .make_union (
4986+ [i for i in typ .items if not is_notimplemented (i )]
4987+ )
49834988 self .check_subtype (
49844989 subtype_label = "got" ,
4985- subtype = typ ,
4990+ subtype = typ_ ,
49864991 supertype_label = "expected" ,
49874992 supertype = return_type ,
49884993 context = s .expr ,
0 commit comments