[Cython] Safer default exception handling with return type annotations?

Stefan Behnel stefan_ml at behnel.de
Wed Sep 6 01:44:56 EDT 2017

Robert Bradshaw schrieb am 06.09.2017 um 07:21:
> I'm not a huge fan of behaving differently depending on what syntax
> was used to annotate the return type--I'd rather they be 100% aliases
> of each other.

Regarding this bit - I already chose to implement some differences for
annotation typing. Mainly, if you say

    def f(x: int) -> float:
        return x

then the (plain "def") function will actually be typed as "double
f(object)"., assuming that you probably meant the Python types and not the
C types. If you want the C types "int" and "float", you have to use either
of these:

    def f1(x: cython.int) -> cython.float:
        return x

    cpdef float f2(int x):
        return x

That is because the main use case of signature annotations is Python code
compatibility, so I tried to change the semantics as little as possible
from what the code would be expected to do in Python.

I also considered if distinguishing between .py and .pyx files would make
sense here, but adapting the type interpretation to that felt much worse
than the above, which is at least consistent regarding the typing scheme
that you use.

I think this type interpretation is a reasonable, use case driven
difference to make. Thus my question if we should extend this to the
exception declaration.


More information about the cython-devel mailing list