最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin5PV0评论

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

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far