[Python-ideas] Python-ideas Digest, Vol 93, Issue 31

Christian Heimes christian at python.org
Thu Aug 14 11:08:43 CEST 2014


On 14.08.2014 02:59, Raymond Hettinger wrote:
> P.S.  I would really like for the annotations to grow some way
> to communicate exceptions as well as return types (i.e. that
> list.index can raise a ValueError and list.pop can raise an IndexError).
> This would be only for the exceptions directly added by a function or
> method,
> not ones raised by the data (which is something the function can't control).

+1

I made the same suggestion a couple of hours earlier. A standardized and
introspectable way to add exception annotation is a common request. It's
especially useful for C code because it's very hard to impossible to
deduce exceptions from C code.

For now there is no need for a new syntax. IMHO a decorator and standard
location for exception annotations are sufficient. Perhaps somebody is
able to come up with a syntax later.


Such a decorator should also include the reason for an exception, too.
For example:

class list:

    @raises(IndexError, "when list is empty")
    def pop(self, index=None):
        pass

    @raises(ValueError)
    def index(self, value):
        pass

Christian



More information about the Python-ideas mailing list