python - How to make ruff complain about useless return statements in functions annotated to return not None? - Stack Overflow

Today, I went to Ruff playground, enabled PLR1711 (useless-return) ruleand pasted this code:def f()

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745659472a4638745.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信