Sharing extension types defined with @cython.cclass

13 views
Skip to first unread message

Mads Ynddal

unread,
Jun 28, 2024, 6:14:43 PM (2 days ago) Jun 28
to cython-users
Hi,

I'm porting an existing Python code base to Cython using only PEP-484 type hints. I want to only have a "pure python" environment, where Cython is optional.

I've added `@cython.cclass` to a few of my classes, and now I want to cimport them from another module. The only way I've found is by adding .pxd files, but this conflicts with the type hints and `@cython.ccall` decorators in the .py file (they can only be defined once). Is there a way to have .pxd files, decorators and type hints co-exist? I tried forward declaring the extension types in the .pxd but that didn't work.

I've extensively used augmenting .pxd files in other projects, but I'd rather try to keep it all contained in the .py files for simplicity this time.

Best regards,
Mads Ynddal

Stefan Behnel

unread,
4:23 PM (3 hours ago) 4:23 PM
to Cython-users, Dan
Hi,

there's a "cimport_from_pyx" feature which mostly applies here. It probably
doesn't apply to ".py" files currently (it searches only ".pyx" files), but
that seems worth changing.

Downside is that it's somewhat costly to parse a complete ".py" file just
to extract the relevant exports / exportable functions and types.

I've had a recent discussion about auto-generating .pxd files from .py
files, though. I'll CC Dan to let him comment. Seems like there's at least
some interest in this feature.

Best,
Stefan



Mads Ynddal schrieb am 28.06.24 um 23:31:
Reply all
Reply to author
Forward
0 new messages