-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Add rule for improper use of NoReturn
#9113
Comments
This could fit https://github.com/PyCQA/flake8-pyi (which Ruff re-implements). I have myself introduced this incorrect union once by accident in typeshed (see python/typeshed#10819 ) |
All Ruff needs to check is that Mypy already checks that |
Will add. |
@charliermarsh Much appreciated! |
@charliermarsh Just FYI, I've just noticed that mypy as well as pylance honor usage of unions containing Given the following function: from typing import Literal, NoReturn, overload
@overload
def raise_conditionally(raise_exc: Literal[False]) -> None:
...
@overload
def raise_conditionally(raise_exc: Literal[True]) -> NoReturn:
...
def raise_conditionally(raise_exc: bool) -> None | NoReturn:
if raise_exc:
raise RuntimeError("raise_exc was true")
return None This function is fine: def is_reachable() -> None:
raise_conditionally(False)
print("This line will be executed") But in this function the last line will be marked as unreachable: def is_unreachable() -> None:
raise_conditionally(True)
print("This line will not be executed") As per my understanding, this usage is incorrect, but still supported by both type checkers. Knowing this, do you want to keep the |
@SRv6d |
@andersk Thanks for clarifying, I falsely assumed that the return type of the function implementation had to be a union containing all the overloaded return types. |
The
NoReturn
return type is commonly misunderstood and not very intuitive.It should be used for functions that never return, either through unconditionally raising an exception,
or by exiting.
Yet, I commonly see it misused (and have misused it myself) to annotate functions that may raise or return using a union return value, eg:
Although ideally ruff would ensure the full invariant(no use of
NoReturn
for functions that might do anything other than raise or exit), I haven't looked into how much work it would be to implement that and simply ensuring no use of union types withNoReturn
would go a long way.The text was updated successfully, but these errors were encountered: