[Python-ideas] Type hints for functions with side-effects and for functions raising exceptions

Juancarlo Añez apalala at gmail.com
Thu Feb 21 20:20:38 EST 2019


On Thu, Feb 21, 2019 at 6:28 PM Ivan Levkivskyi <levkivskyi at gmail.com>
wrote:

> The idea about "checked exceptions" appeared several times in various
> places. I used to think that this would be hard to support for any
> realistic use cases. But now I think there may be a chance
> to turn this into a usable feature if one frames it correctly (e.g. the
> focus could be on user defined exceptions, rather than on standard ones,
> since there is no way we can annotate every function in typeshed).
>

It's well documented how checked exceptions lead to bad code. That's why
C#, which came after Java, didn't include them.

Exceptions may be logically thought as of part of the type of a method, and
of the type of the method's class, but that requires that the type catches
every exception that may be raised from the implementation and either
handles it, or translates it to one belonging to the type. It's a lot of
work, it's cumbersome, and it is fragile, as the exception handling may
need to change over minor implementation changes. If dependencies don't
treat the exceptions that may escape from them as part of the type, then
our own types will need to be changes every time a dependency changes its
implementation.

The strategy of catching only exceptions of interest and letting others
pass produces less fragile and easier to test code.

-- 
Juancarlo *Añez*
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20190221/49e61e9d/attachment-0001.html>


More information about the Python-ideas mailing list