diff --git a/mypy/checkexpr.py b/mypy/checkexpr.py index 020fbad14292..7a13c68b6fd2 100644 --- a/mypy/checkexpr.py +++ b/mypy/checkexpr.py @@ -716,7 +716,9 @@ def always_returns_none(self, node: Expression) -> bool: def defn_returns_none(self, defn: SymbolNode | None) -> bool: """Check if `defn` can _only_ return None.""" + allow_inferred = False if isinstance(defn, Decorator): + allow_inferred = True defn = defn.var if isinstance(defn, FuncDef): return isinstance(defn.type, CallableType) and isinstance( @@ -726,8 +728,10 @@ def defn_returns_none(self, defn: SymbolNode | None) -> bool: return all(self.defn_returns_none(item) for item in defn.items) if isinstance(defn, Var): typ = get_proper_type(defn.type) - if isinstance(typ, CallableType) and isinstance( - get_proper_type(typ.ret_type), NoneType + if ( + (allow_inferred or not defn.is_inferred) + and isinstance(typ, CallableType) + and isinstance(get_proper_type(typ.ret_type), NoneType) ): return True if isinstance(typ, Instance): diff --git a/test-data/unit/check-optional.test b/test-data/unit/check-optional.test index 93f8f61f57dd..683ce0446915 100644 --- a/test-data/unit/check-optional.test +++ b/test-data/unit/check-optional.test @@ -119,8 +119,8 @@ reveal_type(z2) # N: Revealed type is "Union[Literal[0], builtins.str, None]" [case testLambdaReturningNone] f = lambda: None -x = f() # E: Function does not return a value (it only ever returns None) -reveal_type(x) # N: Revealed type is "Any" +x = f() +reveal_type(x) # N: Revealed type is "None" [case testNoneArgumentType] def f(x: None) -> None: pass