-
-
Notifications
You must be signed in to change notification settings - Fork 34.2k
Closed as not planned
Labels
pendingThe issue will be closed if no feedback is providedThe issue will be closed if no feedback is providedtopic-typingtype-featureA feature request or enhancementA feature request or enhancement
Description
Feature or enhancement
Proposal:
from typing import NoReturn
def divide(a: int, b: int) -> float | NoReturn:
return a/b
if __name__ == "__main__":
try:
print(divide(1, 0))
except ValueError:
print("err")In this piece of code, the type checker won't really see anything wrong,
but the divide function actually raises a ZeroDivisionError,
so the code will actually go wrong.
from typing import NoReturn
def divide(a: int, b: int) -> float | NoReturn[ZeroDivisionError]:
return a/b
if __name__ == "__main__":
try:
print(divide(1, 0))
except ValueError:
print("err")By making NoReturn actually declaring the error which raises,
type checkers could actually see that you're not handling ZeroDivisionError,
so you can spot the error before runtime.
Has this already been discussed elsewhere?
This is a minor feature, which does not need previous discussion elsewhere
Links to previous discussion of this feature:
No response
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
pendingThe issue will be closed if no feedback is providedThe issue will be closed if no feedback is providedtopic-typingtype-featureA feature request or enhancementA feature request or enhancement