Skip to content

Commit 21d8078

Browse files
committed
Fix handling of decorators that modify the function signature
1 parent db07091 commit 21d8078

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

mypy/checkexpr.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -717,7 +717,7 @@ def always_returns_none(self, node: Expression) -> bool:
717717
def defn_returns_none(self, defn: SymbolNode | None) -> bool:
718718
"""Check if `defn` can _only_ return None."""
719719
if isinstance(defn, Decorator):
720-
defn = defn.func
720+
defn = defn.var
721721
if isinstance(defn, FuncDef):
722722
return isinstance(defn.type, CallableType) and isinstance(
723723
get_proper_type(defn.type.ret_type), NoneType
@@ -726,10 +726,8 @@ def defn_returns_none(self, defn: SymbolNode | None) -> bool:
726726
return all(self.defn_returns_none(item) for item in defn.items)
727727
if isinstance(defn, Var):
728728
typ = get_proper_type(defn.type)
729-
if (
730-
not defn.is_inferred
731-
and isinstance(typ, CallableType)
732-
and isinstance(get_proper_type(typ.ret_type), NoneType)
729+
if isinstance(typ, CallableType) and isinstance(
730+
get_proper_type(typ.ret_type), NoneType
733731
):
734732
return True
735733
if isinstance(typ, Instance):

test-data/unit/check-expressions.test

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1152,9 +1152,25 @@ F = TypeVar('F', bound=Callable[..., Any])
11521152
def deco(f: F) -> F:
11531153
pass
11541154

1155+
def deco_return_none(f: object) -> Callable[..., None]:
1156+
pass
1157+
1158+
def deco_return_not_none(f: object) -> Callable[..., int]:
1159+
pass
1160+
11551161
@deco
11561162
@deco
1157-
def f() -> None:
1163+
def f1() -> None:
1164+
pass
1165+
1166+
@deco
1167+
@deco_return_none
1168+
def f2() -> int:
1169+
pass
1170+
1171+
@deco
1172+
@deco_return_not_none
1173+
def f3() -> None:
11581174
pass
11591175

11601176
class A:
@@ -1163,7 +1179,11 @@ class A:
11631179
pass
11641180

11651181
if int():
1166-
x = f() # E: "f" does not return a value (it only ever returns None)
1182+
x = f1() # E: "f1" does not return a value (it only ever returns None)
1183+
if int():
1184+
x = f2() # E: "f2" does not return a value (it only ever returns None)
1185+
if int():
1186+
x = f3()
11671187
if int():
11681188
x = A.s() # E: "s" of "A" does not return a value (it only ever returns None)
11691189
if int():

0 commit comments

Comments
 (0)