Today, I went to Ruff playground, enabled PLR1711 (useless-return) rule and pasted this code:
def f() -> int:
print()
return None
In Diagnostics tab I saw only Everything is looking good!
.
I then removed -> int
for the code to be:
def f():
print()
return None
and saw Useless `return` statement at end of function (PLR1711) [Ln 3, Col 5]
.
Ruff complained in the same way when I annotated function to return None:
def f() -> None:
print()
return None
All the same goes for cases with return
instead of return None
.
Is this an intended behavior? If it is, I think the documentation on this rule should inform about this.
I have checked how pylint handles such cases by running cell with the following in fresh google colab runtime:
!pip install pylint>/dev/null
!printf 'def f() -> int:\n print()\n return None' > a.py
!pylint --version && pylint --disable C a.py
I got
pylint 3.3.1
astroid 3.3.5
Python 3.10.12 (main, Sep 11 2024, 15:47:36) [GCC 11.4.0]
************* Module a
a.py:1:0: R1711: Useless return at end of function or method (useless-return)
-----------------------------------
Your code has been rated at 6.67/10
So how can I get ruff to complain about useless return statements in cases when function is annotated to return not None? Do we have to wait for an update for this?
You can also help on github