-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Ability to specify and check possible unhandled exceptions #11282
Comments
Also, @VanyaDNDZ suggests that probably would be good to understand, that if the user-function propagates the same exception (or wider), Mypy could assume that it's ok to not def divide(a: int, b: int) -> int | NoReturn[ZeroDivisionError]:
return a // b
def business_logic(a: int, b: int, c: int) -> int | NoReturn[ZeroDivisionError]]:
return divide(a, b) + c |
@sobolevn Thank you for this link, but that's exactly what I'm trying to avoid. That's a beautiful style of programming, but with this feature request I'm trying to bring exhaust error checking to imperative programming style (which is prevalent for most Python projects) |
This is a duplicate of #1773. This idea would require a new PEP, so the discussion more properly belongs in the python/typing project rather than mypy. |
I agree with @erictraut |
Feature
So, the main idea is to be able to specify and then let Mypy check that some function probably could raise an exception and client code properly handle it during the call.
Initially, I posted some ideas of this feature request into #7326.
Pitch
Basically, the idea is simple - to make a fault-tolerant code we should know where exceptions could occur and handle it properly. Before the Mypy and typing this could be done only by empirical analysis of the code in Python, with these tools - there is a possibility to get some help from static analysis.
There were a few discussions here and here, but proposed solutions are too radical, IMHO. Rust-style error handling dictates a monadic style of code execution with unwrapping and
Result[Ok, Err]
return type which stands against the pythonic way of dealing with exceptions.So, I propose a simple way (from the first point of view, of course, it'll be great to hear from dear maintainers regarding technical limitations of this) to mark that function could have a side effect - re-use the special
NoReturn
type alias, like this:This style is quite close to what @JukkaL proposed in original conversation, but in this way, we could definitely distinguish between values that are returning from function (including exceptions) and actual unhandled exceptions, so this signature is perfectly normal and unambiguous:
General Type Checking Rules
For Mypy this kind of definition with
Union[some_particular_type, NoResult]
could be checked as a regular type, but instead ofif <condition>
guards,NoResult
type should be inferred/excluded withtry .. except
guardAlso, in case our
except
block catching the exception which is not an ancestor for NoResult[SomeException], we could again assume that we do not properly handle the possible exception hereDisable the error checking
And of course, sometimes we need to ignore the possible errors, e. g.
IndexError
when we are 100% sure there can't be an empty list. For this, we could use some# type: ...
hint. I proposing something like this (sinceunwrap
is something that specifies we don't want to handle this error and wants to fail):Or we could simply use
# type: ignore
here, as usual.Of course, this feature should be optional, at least for some time, but I believe this optional "exhaustive error handling" checking could make our lives better :)
As an additional bonus, that could help IDE to generate
try .. except
blocks automatically and shows better documentation, but documentation is not a major point here, really.The text was updated successfully, but these errors were encountered: