Python type hinting with exceptions Python type hinting with exceptions python-3.x python-3.x

Python type hinting with exceptions


Type hinting can't say anything about exceptions. They are entirely out of scope for the feature. You can still document the exception in the docstring however.

From PEP 484 -- Type Hints:

Exceptions

No syntax for listing explicitly raised exceptions is proposed. Currently the only known use case for this feature is documentational, in which case the recommendation is to put this information in a docstring.

Guido van Rossum has strongly opposed adding exceptions to the type hinting spec, as he doesn't want to end up in a situation where exceptions need to be checked (handled in calling code) or declared explicitly at each level.


It is usually a good idea to document the error. This means that another developer using your function will be able to handle your errors without having to read through your code.