Skip to content
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

Expose classes in package scope with __init__.pxd #3270

Open
gerion0 opened this issue Dec 12, 2019 · 2 comments
Open

Expose classes in package scope with __init__.pxd #3270

gerion0 opened this issue Dec 12, 2019 · 2 comments

Comments

@gerion0
Copy link
Contributor

@gerion0 gerion0 commented Dec 12, 2019

Cython seems to support packages with pxd files. However, they behave different than in Python.

Let's assume this structure:

|- foo
|   |- __init__.pxd
|    `- bar.pxd
`- test.pyx

With the following content:

test.pyx:

from foo cimport Bar

foo/__init__.pxd:

# expose to package scope
from .bar cimport Bar

foo/bar.pxd:

cdef class Bar:
    pass

This does not work: 'foo/Bar.pxd' not found. However, the same code in Python would work because you can export classes to the package scope. I found this kind of unintuitive.

Interestingly, the following code works again.
Delete foo/bar.pxd and change foo/__init__.pxd to:

cdef class Bar:
    pass

Please make the exposing also possible because it allows a better structuring of source files.

@scoder

This comment has been minimized.

Copy link
Contributor

@scoder scoder commented Dec 23, 2019

Worth verifying, but I think a cimport doesn't re-export the name and only makes it available internally. This is usually what people want in .pyx files. There might be an argument for differing behaviour in .pxd files, as you show, but I'm not sure if such a difference is better or worse for users to understand.

@gerion0

This comment has been minimized.

Copy link
Contributor Author

@gerion0 gerion0 commented Dec 23, 2019

Python behave exactly this way, when using import in __init__.py, so I guess, users are already familiar with it. It is also common practice to use the __init__.py to expose classes to the package scope (e.g. 1, 2).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.