10000 Fix `[func-returns-value]` not being trigged when calling decorated functions by brianschubert · Pull Request #18015 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

Fix [func-returns-value] not being trigged when calling decorated functions #18015

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Fix handling of decorators that modify the function signature
  • Loading branch information
brianschubert committed Oct 22, 2024
commit 21d8078c7fb1baf6f7fa6667c19c783ec577605e
8 changes: 3 additions & 5 deletions mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -717,7 +717,7 @@ 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):
defn = defn.func
defn = defn.var
if isinstance(defn, FuncDef):
return isinstance(defn.type, CallableType) and isinstance(
get_proper_type(defn.type.ret_type), NoneType
Expand All @@ -726,10 +726,8 @@ 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 (
not defn.is_inferred
and isinstance(typ, CallableType)
and isinstance(get_proper_type(typ.ret_type), NoneType)
if isinstance(typ, CallableType) and isinstance(
get_proper_type(typ.ret_type), NoneType
):
return True
if isinstance(typ, Instance):
Expand Down
24 changes: 22 additions & 2 deletions test-data/unit/check-expressions.test
A82A
Original file line numberDiff line number Diff line change
Expand Up @@ -1152,9 +1152,25 @@ F = TypeVar('F', bound=Callable[..., Any])
def deco(f: F) -> F:
pass

def deco_return_none(f: object) -> Callable[..., None]:
pass

def deco_return_not_none(f: object) -> Callable[..., int]:
pass

@deco
@deco
def f() -> None:
def f1() -> None:
pass

@deco
@deco_return_none
def f2() -> int:
pass

@deco
@deco_return_not_none
def f3() -> None:
pass

class A:
Expand All @@ -1163,7 +1179,11 @@ class A:
pass

if int():
x = f() # E: "f" does not return a value (it only ever returns None)
x = f1() # E: "f1" does not return a value (it only ever returns None)
if int():
x = f2() # E: "f2" does not return a value (it only ever returns None)
if int():
x = f3()
if int():
x = A.s() # E: "s" of "A" does not return a value (it only ever returns None)
if int():
Expand Down
0