Missing exceptions in PEP 3107

Matimus mccredie at gmail.com
Sat Aug 9 23:14:32 CEST 2008


On Aug 9, 9:08 am, Christoph Zwerschke <c... at online.de> wrote:
> I'm just reading PEP 3107 (function annotations) and wonder why
> exceptions are not mentioned there. I think it would be helpful if one
> could specify which exceptions can be raised by a function, similarly to
> how it is possible in C++ using the "throw" clause. The syntax would be
> something like this:
>
> def foo(a: expr, b: expr = 5) raises expr -> expr:
>
> The expr in that "raises" clause should be a list of Exceptions.
>
> Having the list of possible exceptions as annotation alone would be
> already helpful. Of course it could be also discussed whether Python
> should check that the function really raises only these exceptions (as
> in C++), or we could even have checked exceptions (as in Java, but this
> seems to be a controversial issue).
>
> Has this already been discussed, or is it in a different PEP?
>
> -- Christoph

Keep in mind that annotations are just a way of associating some
information with the parameters or a function. There is a special
parameter called `return` to help associate information with the
return value. Whether that information is used to describe the types
of the function parameters, how they are used, or something completely
different is up to the application that uses them.

When you say:
> The expr in that "raises" clause should be a list of Exceptions.

You are clearly confusing the annotation feature with a possible
application of the annotation feature. Annotation could be used for
many different applications besides type safety.

Annotation simply creates a dictionary. The name `return` was chosen
for the return value because it _is_ a keyword and therefore could not
conflict with the name of any of the parameters. Using "raises" would
mean that we would have to introduce the name "raises" as a new
keyword. It would be better just to use they existing keyword "raise".

With all of that being said, a package or application that uses
annotation could simply use the data-structure associated with
"return" to also contain exception information. That might not seem
intuitive, but keep in mind that the value associated with "return" in
the associations dictionary is going to be a special case anyway.

def foo(a: "a info", b: "b info") -> "return info", "exception info":
    return "hello world"

Matt



More information about the Python-list mailing list