typing
typing copied to clipboard
Spec: Allow `NoReturn` to appear outside of function return type annotations
The typing spec currently states:
The
NoReturntype is only valid as a return annotation of functions, and considered an error if it appears in other positions.
This is old language from the time when NoReturn was first added in PEP 484. Since then, we've added Never to the typing spec and indicated that Never and NoReturn are equivalent types. Even before Never was added, the assert_never function made use of NoReturn outside of a return annotation.
No type checker currently enforces that NoReturn appears only in return type annotations of functions. I'd like to propose that we simply delete that section of the spec. That would bring the spec into alignment with the current behavior of all four major type checkers.
Should we also clearly state that NoReturn is deprecated in favor of Never to mirror the wording in the docs?
Should we also clearly state that NoReturn is deprecated in favor of Never to mirror the wording in the docs?
I don't have a strong opinion one way or another about this, but I think that's a separate issue.
It is related in my mind, since NoReturn is named so that it really only makes sense in function return types, and this issue is about lifting that limitation.
Should we also clearly state that
NoReturnis deprecated in favor ofNeverto mirror the wording in the docs?
The docs don't actually state that NoReturn is deprecated. They state that using NoReturn to express the concept of the bottom type is discouraged (because Never is a clearer name for the type when it's used as such). We don't have any plans to remove NoReturn from the typing module, and I wouldn't even call it soft-deprecated.
If you just want to use it as a return type to say that a function never returns (because it enters into an infinite loop or whatever), then the name NoReturn is pretty unproblematic, so there's no real reason to deprecate it
I would propose that the spec should state that Never and NoReturn should be treated as exact equivalents by type checkers. There are reasons to use one name over the other, but that's more of a style guide issue, not a spec issue.
Thanks @AlexWaygood and @JelleZijlstra!
I agree that it makes sense to treat Never and NoReturn as equivalent. I would personally still prefer for Python to have just one spelling of the bottom type, but I'm not convinced the migration cost for users is justified.
I think this has been completed. See here. @JelleZijlstra, if you agree, please close this issue.
Yeah, fixed in https://github.com/python/typing/pull/1570/files