Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,18 @@ def always_returns_none(self, node: Expression) -> bool:

def defn_returns_none(self, defn: SymbolNode | None) -> bool:
"""Check if `defn` can _only_ return None."""
if isinstance(defn, Decorator):
# Decorated functions (e.g. @staticmethod, @classmethod) are wrapped
# in a Decorator node; the resulting callable type is stored on the
# synthetic Var. We use that callable's return type so that
# `func-returns-value` is reported for decorated methods the same
# way it is for plain functions
# (https://github.com/python/mypy/issues/14179). The Var is marked
# is_inferred=True so we cannot reuse the Var branch below.
typ = get_proper_type(defn.var.type)
return isinstance(typ, CallableType) and isinstance(
get_proper_type(typ.ret_type), NoneType
)
if isinstance(defn, FuncDef):
return isinstance(defn.type, CallableType) and isinstance(
get_proper_type(defn.type.ret_type), NoneType
Expand Down
25 changes: 25 additions & 0 deletions test-data/unit/check-errorcodes.test
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,31 @@ def main() -> None:
y = A().g() # E: "g" of "A" does not return a value (it only ever returns None) [func-returns-value]
z = c() # E: Function does not return a value (it only ever returns None) [func-returns-value]

[case testErrorCodeFuncReturnsValueStaticAndClassMethod]
# Regression test for https://github.com/python/mypy/issues/14179
# Decorated methods (staticmethod, classmethod) should report
# func-returns-value just like instance methods do.
class Example:
def instance_op(self) -> None:
return None

@staticmethod
def static_op() -> None:
return None

@classmethod
def class_op(cls) -> None:
return None

def main() -> None:
ex = Example()
a = ex.instance_op() # E: "instance_op" of "Example" does not return a value (it only ever returns None) [func-returns-value]
b = ex.static_op() # E: "static_op" of "Example" does not return a value (it only ever returns None) [func-returns-value]
c = ex.class_op() # E: "class_op" of "Example" does not return a value (it only ever returns None) [func-returns-value]
d = Example.static_op() # E: "static_op" of "Example" does not return a value (it only ever returns None) [func-returns-value]
e = Example.class_op() # E: "class_op" of "Example" does not return a value (it only ever returns None) [func-returns-value]
[builtins fixtures/classmethod.pyi]

[case testErrorCodeInstantiateAbstract]
from abc import abstractmethod

Expand Down
Loading