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

Defining a module foo results in foo.foo being available #1399

Open
not-my-profile opened this issue Jan 8, 2023 · 6 comments
Open

Defining a module foo results in foo.foo being available #1399

not-my-profile opened this issue Jan 8, 2023 · 6 comments

Comments

@not-my-profile
Copy link

not-my-profile commented Jan 8, 2023

Run maturin new -b pyo3 foo, followed by maturin dev in a venv.

>>> import foo
>>> foo.foo
<module 'foo.foo' from '/tmp/venv/lib/python3.9/site-packages/foo/foo.cpython-39-x86_64-linux-gnu.so'>

I would expect foo.foo to be undefined. The same can be observed when using rust-cpython.

Versions:

  • maturin 0.14.8
  • Python 3.9.2
  • pip 20.3.4
@not-my-profile not-my-profile added the bug Something isn't working label Jan 8, 2023
@not-my-profile not-my-profile changed the title Defining a module foo results in foo.foo being available (pyo3) Defining a module foo results in foo.foo being available Jan 8, 2023
@messense
Copy link
Member

messense commented Jan 9, 2023

It is by design, see also #1365

@messense messense removed the bug Something isn't working label Jan 9, 2023
@not-my-profile
Copy link
Author

What is the reasoning behind this design decision? (Sidenote: If the .so file is part of the package I think prefixing its name with an _ would be somewhat better since it would clarify that it's not part of the public API.)

For my project I would prefer the .so file to reside directly in site-packages/ to avoid having any modules that you can import but actually should not import.

@messense
Copy link
Member

messense commented Jan 9, 2023

See #558, it allows package type stubs automatically.

Sidenote: If the .so file is part of the package I think prefixing its name with an _ would be somewhat better since it would clarify that it's not part of the public API.

You can use mixed layout to do this, see https://www.maturin.rs/project_layout.html#import-rust-as-a-submodule-of-your-project

@not-my-profile
Copy link
Author

not-my-profile commented Jan 9, 2023

See #558, it allows package type stubs automatically.

I don't see anything about putting the .so file directly in site-packages that would prevent type stubs from being packaged. The following works just fine (tested with pyright):

site-packages
├── my_project.cpython-36m-x86_64-linux-gnu.so
├── my_project.pyi
└── my_project-2.1.2.dist-info
    └── ...

(Putting my_project/__init__.pyi in site-packages also works).

@messense
Copy link
Member

messense commented Jan 9, 2023

That would miss the py.typed marker I think? Does it work with every other IDEs? I'm not sure.

@not-my-profile
Copy link
Author

Ah yes of course, you're correct that this wouldn't work with py.typed. Thanks!

I just opened python/typing#1333 about introducing some way to mark .so files as typed without having to introduce an intermediary __init__.py file :)

chrysn added a commit to chrysn/cbor-diag-py that referenced this issue May 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants