-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
sys.exit() is not considered as noreturn #13592
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
Comments
Can you clarify what you think the bug is? Mypy is correct when it doesn't produce any errors on your first sample, because a function that never returns is a valid |
Looks like So, it looks fine to me! |
Using return value of noreturn function is an error, indeed. |
Feel free to reopen if you have other questions / problems :) |
@sobolevn please reopen. It's not fixed actually. |
Can you please specify what example is not fixed? |
The first one. Mypy does not see any errors in using value of noreturn function. |
The first code sample is not a bug. Mypy is correct in not emitting an error here. |
I think, Noreturn must not be compatible with any other type. What the case when it should be compatible ? |
Should be |
|
Thanks for verbose explanation. I checked TypeScript: const sys_exit: (code: number) => never = (code: number) => {
throw new Error('message');
};
const x: number = sys_exit(42); It also sees nothing wrong here. I still don't understand why |
Because a value of https://en.wikipedia.org/wiki/Bottom_type def n() -> Never: raise Exception
print(n() + 1) At runtime, this will not produce a |
Why not make it incompatible with any type to detect obvious errors such as trying to use return value of noreturn functions ? |
I think what you are asking for is more in the realm of a linting rule*: I like the idea for that reason, idk if it belongs in mypy / pyright (maybe as an optional setting?). But few python linters are type-aware. I would personally turn on that rule if I had the option. * It wouldn't the be the first time a type-checker has a rule that's not specific to typing (pyright has a few I like). |
@Avasam Mypy complains on usages of functions that return def foo() -> None:
return
a: int | None = foo() # error: "foo" does not return a value [func-returns-value] https://mypy-play.net/?mypy=latest&python=3.11&gist=e15787211c8a4115c82687bc15fee244 Perhaps the title of this issue could be updated to be something like "Warn on usage of |
Mypy is working correctly here. This isn't a bug. I recommend closing. |
https://mypy-play.net/?mypy=latest&python=3.10&flags=strict%2Cstrict-equality&gist=e86aa52c05acd36b6c04067cb6f44ce1
MyPy finds nothing wrong here.
Anyway, this should be valid:
i.e. this function either raises specific exception or noreturn.
The text was updated successfully, but these errors were encountered: