Skip to content
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

Autofix for RET503 breaks RET505 (in one case) #5765

Open
Jeremiah-England opened this issue Jul 14, 2023 · 2 comments · May be fixed by #11074
Open

Autofix for RET503 breaks RET505 (in one case) #5765

Jeremiah-England opened this issue Jul 14, 2023 · 2 comments · May be fixed by #11074
Labels
bug Something isn't working fixes Related to suggested fixes for violations

Comments

@Jeremiah-England
Copy link
Contributor

On the latest version of ruff (0.0.278) this fails on the following code:

ruff --select RET503,RET505 test.py --fix --isolated
# test.py:11:9: RET505 Unnecessary `else` after `return` statement
def main(value: str) -> int | None:
    if value == "one":
        return 1

    if value.startswith("do_stuff"):
        print("doing stuff")

        if value.endswith("haha"):
            print("haha")
        else:
            print("Instruction unclear.")
    else:
        print("Doing nothing.")

That is because the autofix for RET503 inserts return None in all three
branches of the if/else tree:

--- test.py
+++ test.py
@@ -7,7 +7,10 @@
 
         if value.endswith("haha"):
             print("haha")
+            return None
         else:
             print("Instruction unclear.")
+            return None
     else:
         print("Doing nothing.")
+        return None

But that is not compliant with RET505.

The fix in this case is to insert return None at the end of the function instead of in each branch in the if/else tree.

def main(value: str) -> int | None:
    if value == "one":
        return 1

    if value.startswith("do_stuff"):
        print("doing stuff")

        if value.endswith("haha"):
            print("haha")
        else:
            print("Instruction unclear.")
    else:
        print("Doing nothing.")
    return None

Not sure it is worth the effort to update behavior of the RET503 autofix for this case. But I thought I should report it.

@charliermarsh charliermarsh added bug Something isn't working fixes Related to suggested fixes for violations labels Jul 14, 2023
@Zac-HD
Copy link

Zac-HD commented Aug 20, 2023

Using Ruff 0.0.285, it also just looks pretty silly to have a chain of return None statements added:

ruff --isolated --fix-only t.py --select=RET503
  def f():
      if ...:
          return True
      if ...:
          pass
+         return None
+     return None

It's not a big deal for me since I don't mind skipping this cosmetic check, but with such a small repro it seemed worth reporting.

@JonathanPlasse
Copy link
Contributor

Should RET503 be simplified to only add return None at the end of the function?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixes Related to suggested fixes for violations
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants