Working with PEP484 type annotations and numpy

215 views
Skip to first unread message

Guilherme Caminha

unread,
Feb 8, 2018, 1:19:56 AM2/8/18
to cython-users
Is there a way to work with PEP 484 type annotations and numpy, while still benefiting from Cython performance improval?
This github issue (https://github.com/cython/cython/issues/1672) suggests to use the cython built-in types (ex: myvar: cython.int). However, I can't figure out how to use numpy arrays with this, since types such as cython.p_double can't fully represent a numpy array.

Attached here is an example of what I'm trying to accomplish: a script that works on both Python and Cython, using type annotations, which I expect to gain performance after compiling with Cython. It doesn't compile with Cython, however.
cypyteste.py

Stefan Behnel

unread,
Feb 9, 2018, 3:52:06 AM2/9/18
to cython...@googlegroups.com
Consider using memory views instead. AFAIR, they should also work in pure
Python code, as long as the underlying array/buffer supports the operations.

Stefan

Guilherme Caminha

unread,
Feb 10, 2018, 4:08:04 PM2/10/18
to cython...@googlegroups.com
So there is no way currently to use type annotations to declare arrays?
I believe typed memoryviews can't be used as type annotations. So the only way would be to use pure Python mode, which wouldn't leverage python static type checking tools such as mypy.


--

---
You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Chris Barker

unread,
Feb 12, 2018, 3:13:21 PM2/12/18
to cython-users
On Sat, Feb 10, 2018 at 1:02 PM, Guilherme Caminha <gp...@cin.ufpe.br> wrote:
So there is no way currently to use type annotations to declare arrays?

no -- but there was discussion about it on the numpy list -- I think a small group of folks is working on it.

-CHB


--

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris....@noaa.gov

Ethan Smith

unread,
Feb 14, 2018, 8:22:07 AM2/14/18
to cython-users
The goal is to develop a set of PEP 561 compliant stubs here: https://github.com/numpy/numpy_stubs. Typing numpy will require some new type system features such as literals and dependent types. If added to PEP 484, these would have to be added to Cython to understand them. There is not insignificant work to be done on this.
Reply all
Reply to author
Forward
0 new messages