ruff
ruff copied to clipboard
[Rule request] Missing return statement
I just stumbled over a bug in our codebase:
def f() -> int:
return 42
def g() -> int | None:
f()
The last line should have been return f()
.
While Mypy reports a Missing return statement
, Pyright does not. This is a deliberate decision by the Pyright maintainer (point 2) as there is no type issue here. In this case Mypy actually does some static analysis outside of its main responsibility.
I'm wondering whether there could be a Ruff rule for this. Often, Ruff lacks the type information. But in this case type information isn't needed. If a function:
- has a return annotation,
- and it's different than
-> None
or-> NoReturn
, - and the body
- isn't a stub (
...
orpass
) - doesn't have a
return
anywhere (in that case RET503 takes over) - and control flow can reach the end of the body
- isn't a stub (
- report an error
def ok_1() -> int | None:
return 2
def ok_2() -> int | None:
return # < maybe a second rule later could reject `return` without a value in this scenario
def ok_3() -> int | None:
raise NotImplementedError
def ok_4() -> int | None: ...
def ok_5() -> int | None:
pass
def ok_6() -> int | None:
if ...:
return 42 # RET503 will cause an error here
def ok_7():
f()
def bad_1() -> int | None:
f()
def bad_2() -> int | None:
if ...:
raise ValueError()
I think it would be a good idea to extend RET503 to take this into account.
I think this could be a good first issue.