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

Make writing modules simpler #3268

Closed
orowith2os opened this issue Jun 24, 2023 · 4 comments
Closed

Make writing modules simpler #3268

orowith2os opened this issue Jun 24, 2023 · 4 comments

Comments

@orowith2os
Copy link

Currently, with PyO3, it's quite simple to write functions that Python can use. However, it gets a bit weird when it comes to creating a module.

This is the current example:

use pyo3::prelude::*;

#[pyfunction]
fn double(x: usize) -> usize {
    x * 2
}

#[pymodule]
fn my_extension(py: Python<'_>, m: &PyModule) -> PyResult<()> {
    m.add_function(wrap_pyfunction!(double, m)?)?;
    Ok(())
}

You can see that the double function looks just like normal Rust, only with an added attribute macro. However, defining the my_extension module is a bit weirder. The syntax is a bit confusing and hard to write for each function you want to expose to Python.

This issue is about how to make it simpler to utilize; ideally, it would be as simple as something like this:

#[pymodule]
fn my_extension() {
    double();
}

Or something along those lines. This would benefit users of PyO3 greatly, especially when it comes to consistently using it in Python projects.

@davidhewitt
Copy link
Member

davidhewitt commented Jun 25, 2023

Thanks for the proposal! I agree the #[pymodule] macro is overdue some ergonomic improvements.

What do you think about allowing #[pymodule] on mod items? For example, it might look like this:

#[pymodule]
mod my_extension() {
    #[pyfunction]
    fn double(x: usize) -> usize {
        x * 2
    }
}

Every #[pyfunction] and #[pyclass] inside the module could be automatically registered. We could also allow a #[module_init] function or similar to enable full customization like the function-modules we have today.

I started working on a draft of this in #2367, however I haven't found the time to continue with this. Help in design or implementation is always welcome.

@orowith2os
Copy link
Author

What do you think about allowing #[pymodule] on mod items?

It looks great! I'd love to have this, especially in a large project; only having to add a #[pymodule] and #[pyfunction] would be nice, especially because chances are it's already all split up into modules.

@orowith2os orowith2os mentioned this issue Jul 3, 2023
6 tasks
@birkenfeld
Copy link
Member

I started working on a draft of this in #2367, however I haven't found the time to continue with this. Help in design or implementation is always welcome.

As a first step, I rebased your work and added support for cfgs on the use items: #3294

@davidhewitt
Copy link
Member

Closing in favour of #3900, which tracks the experimental-declarative-modules feature.

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

No branches or pull requests

3 participants