I thought I did, repeatedly, last time we discussed this; but again: they don’t cause a fatal compilation error when they’re wrong. Checked exceptions require a static compilation type-checking pass to exist in the first place; Python doesn’t have that, and the existing use of annotations simply does not count.
Can I not say the same about type annotations? If function f
declares that it returns a str
, there is nothing (first-party) that requires g
to declare a return type, nor a type for anywhere that the result of f()
is assigned, etc. Yet, if I publish f
as a library, making that declaration does have value, whether the client code wants to use type annotations for g
or not.
I don’t think that static exception analysis is any more trivial than static type analysis, certainly not in a language as flexible as Python. (For example, nothing prevents me from writing code that, for example, deserializes and raises a valid exception instance from a pickle file.) Aside from that, a third-party tool that is already built to examine function signatures statically (but not to examine bytecode statically) will have a much easier time reasoning about exception declarations in annotations than it would about raise
statements.
To document (i.e., not enforce checking, while offering an option to people who want to use third-party tools) exceptions that are salient to the normal operation of the code, i.e., which don’t indicate either a programming error or an unplanned-for condition in the environment (ctrl-C is fundamentally different from a malformed input file). Even languages that actually do have checked exceptions (like Java) have no difficulty making distinctions like that, btw, and programmers can follow simple rules to choose whether to check their own.
By documenting the potential for exceptions in this way, the information becomes more readily available to tools that can warn about potential problems.
I don’t even like type annotations, but there are people who want them, and their reasons are understandable and well considered. The reasoning about exception annotations works the same way.