$ 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 -- 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
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
On 27 September 2017 at 15:54, Stefan Behnel <stefan_ml@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@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
Lisandro Dalcin schrieb am 27.09.2017 um 15:53:
Annotations can be used for type hinting, but they are not required to do that.
Well, they sort of are, according to PEP 484.
For example, in my own use case, I'm just using it to get better docstrings.
Then you can set the directive "annotation_typing=False".
In this commit:
commit 806e2fe6eefea5bfa011a45ea556d93c2094a95a Author: Stefan Behnel <stefan_ml@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?
No need, I just fixed it: https://github.com/cython/cython/commit/4326a3b9912e27aaf9502e17d717b55b7504... Stefan
On 27 September 2017 at 17:18, Stefan Behnel <stefan_ml@behnel.de> wrote:
Lisandro Dalcin schrieb am 27.09.2017 um 15:53:
Annotations can be used for type hinting, but they are not required to do that.
Well, they sort of are, according to PEP 484.
I gently disagree. Third paragraph of PEP 484: """ Note that this PEP still explicitly does NOT prevent other uses of annotations, nor does it require (or forbid) any particular processing of annotations, even when they conform to this specification. """ Maybe Cython should eventually ignore (maybe with a warning) if the annotation cannot be interpreted as a type and `annotation_typing=True` is set?
For example, in my own use case, I'm just using it to get better docstrings.
Then you can set the directive "annotation_typing=False".
I did in my own code (though not in the trivial example I posted, sorry), and still got the error, so I started this thread.
No need, I just fixed it:
https://github.com/cython/cython/commit/4326a3b9912e27aaf9502e17d717b55b7504...
Thanks! Now things work just fine for me. -- 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
participants (2)
-
Lisandro Dalcin -
Stefan Behnel