New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to import cclasses defined in other source files in pure python mode? #2763

Open
trenta3 opened this Issue Dec 17, 2018 · 1 comment

Comments

Projects
None yet
2 participants
@trenta3
Copy link

trenta3 commented Dec 17, 2018

I'm using Cython 0.29.2 in pure python mode with type hints.

The following code snipped works when the two classes are in the same file, but doesn't compile with cython when the two are written in separate files:

# This is in a.py
# cython: language_level=3
import cython
from .b import B

@cython.cclass
class A(B):
    @cython.ccall
    def add(self: A, h: cython.int, g: cython.int) -> cython.int:
        return h + g

if __name__ == "__main__":
    a = A()
    for i in range(100):
        print(a.add(i, i+1))
        print(a.sub(i, i-1))
# This is in b.py
# cython: language_level=3
# cython: language_level=3
import cython

@cython.cclass
class B:
    @cython.ccall
    def sub(self: B, h: cython.int, g: cython.int) -> cython.int:
        return h - g

I couldn't find in the documentation a proper way (decorator?) to handle such an issue.
The cython compiler gives the error First base of 'A' is not an extension type which I think is false, since it is also declared as cclass and when the two are in the same module everything works.

I'm not sure if this is:

  • a bug (should cython recognize the imports?)
  • a feature request (maybe it is not currently possible to do such thing in cython?)
  • a documentation request (I couldn't find in in any place in the documentation, which btw also don't really mentions how to use cython with type hints).

Thank you in advance.

@scoder

This comment has been minimized.

Copy link
Contributor

scoder commented Dec 29, 2018

Hmm. So, there is a feature called "cimport from pyx", which can be enabled in the Cython.Compiler.Options module, that provides sort-of what you want, but not entirely. I never used it myself, so I can't say how easy it would be to make it work for this case, apart from making it look for .py files in addition to .pyx files (and then make it interpret correctly what it finds in there).

However, is there any reason for you to not use .pxd files? If you want to export certain low-level features from a module, then adding a dedicated file that describes the exported things always seemed like a rather good and explicit approach to me. It's definitely something that works out of the box right now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment