[Cython] Issue with function annotations

Lisandro Dalcin dalcinl at gmail.com
Wed Sep 27 09:53:26 EDT 2017


On 27 September 2017 at 15:54, Stefan Behnel <stefan_ml at behnel.de> wrote:
> Hi Lisandro,
>
> Lisandro Dalcin schrieb am 27.09.2017 um 14:42:
>> $ cython --version
>> Cython version 0.27
>>
>> $ cat tmp.pyx
>> def f() -> (list, list):
>>     return [], []
>>
>> $ cython tmp.pyx
>>
>> Error compiling Cython file:
>> ------------------------------------------------------------
>> ...
>> def f() -> (list, list):
>>            ^
>> ------------------------------------------------------------
>>
>> tmp.pyx:1:12: C struct/union member cannot be a Python object
>>
>> Error compiling Cython file:
>> ------------------------------------------------------------
>> ...
>> def f() -> (list, list):
>>            ^
>> ------------------------------------------------------------
>>
>> tmp.pyx:1:12: C struct/union member cannot be a Python object
>
> Yes, I noticed that, too. It's a bit annoying. PEP 484 would spell this
> using the "Tuple" type, i.e. "Tuple[list, list]", or even something like
> "Tuple[List[Any], List[Any]]".
>
> The problem in Cython is that the syntax is reserved for C tuples, which
> are syntactically nicer structs. "(list, list)" is a struct with two member
> lists, but since structs do not support reference counting, this is illegal
> in Cython.
>
> The best way to resolve this for now is probably to ignore C tuples in
> function annotations entirely. At some point, Cython will have to learn to
> understand complex PEP-484 types like Tuple[].
>

Stefan, I think you are mixing things up. Annotations can be used for
type hinting, but they are not required to do that. For example, in my
own use case, I'm just using it to get better docstrings.

In this commit:

commit 806e2fe6eefea5bfa011a45ea556d93c2094a95a
Author: Stefan Behnel <stefan_ml at behnel.de>
Date:   Sat Sep 2 21:01:28 2017 +0200

    Disable an "embedsignatures" test that is not actually valid with
PEP 484 but fails to compile as the return type now interpreted as a
C-tuple of Python objects (which is not supported).

You disabled one of my test case. This test case is about docstrings,
not type hints! I wrote that test on purpose, to prevent regressions.
At the time I wrote the example, IIRC, annotation_typing=False was the
default.  At most, we should add `#cython: annotation_typing=False` in
the top, and Cython should happily accept the code and the test should
succeed.

I can try to contribute a fix. I would like to ignore C-tuples in
annotations at least if annotation_typing=False. Any pointers about
where to start looking?


-- 
Lisandro Dalcin
============
Research Scientist
Computer, Electrical and Mathematical Sciences & Engineering (CEMSE)
Extreme Computing Research Center (ECRC)
King Abdullah University of Science and Technology (KAUST)
http://ecrc.kaust.edu.sa/

4700 King Abdullah University of Science and Technology
al-Khawarizmi Bldg (Bldg 1), Office # 0109
Thuwal 23955-6900, Kingdom of Saudi Arabia
http://www.kaust.edu.sa

Office Phone: +966 12 808-0459


More information about the cython-devel mailing list