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

PLR1711, RET501 should exempt annotated function where return type isn’t -> None #3704

Closed
andersk opened this issue Mar 24, 2023 · 2 comments · Fixed by #3705
Closed

PLR1711, RET501 should exempt annotated function where return type isn’t -> None #3704

andersk opened this issue Mar 24, 2023 · 2 comments · Fixed by #3705
Labels
bug Something isn't working

Comments

@andersk
Copy link
Contributor

andersk commented Mar 24, 2023

Ruff flags PLR1711 useless-return and RET501 unnecessary-return-none in this code:

class BaseCache:
    def get(self, key: str) -> str | None:
        print(f"{key} not found")
        return None
test.py:4:9: RET501 [*] Do not explicitly `return None` in function if it is the only possible return value
test.py:4:9: PLR1711 [*] Useless `return` statement at end of function

But mypy complains if return None is removed:

test.py:2: error: Missing return statement  [return]

or if return None is changed to return:

test.py:4: error: Return value expected  [return-value]

mypy can be appeased by changing the return type annotation to -> None, and that’s okay in many cases (e.g. Callable[..., None] covariantly coerces to Callable[..., str | None), but it’s not without problems—for example, it prevents a derived class from overriding the method to return a str.

So I think RET501 and PLR1711 should exempt annotated functions with a return type other than -> None.

@charliermarsh
Copy link
Member

charliermarsh commented Mar 24, 2023

I agree.

@charliermarsh charliermarsh added the bug Something isn't working label Mar 24, 2023
@JonathanPlasse
Copy link
Contributor

I would like to work on it.

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

Successfully merging a pull request may close this issue.

3 participants