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

Allow inline struct, enum, fn and mod inside of declarative modules #3902

Merged
merged 2 commits into from Mar 6, 2024

Conversation

Tpt
Copy link
Contributor

@Tpt Tpt commented Feb 26, 2024

This PR lifts restrictions on the content of declarative modules.

It also allows to add #[pymodule_export] to:

  • struct with #[pyclass]
  • enum with #[pyclass]
  • fn with #[pyfunction]
  • mod with #[pymodule]

Open question: do we want to require adding #[pymodule_export] to the elements to be exported or do we export automatically all elements with #[pyclass], #[pyfunction] or #[pymodule]?

Example:

#[pymodule]
mod declarative_module {
    #[pymodule_export]
    #[pymodule]
    mod inner {
        #[pymodule_export]
        #[pyfunction]
        fn triple(x: usize) -> usize {
            x * 3
        }

        #[pymodule_export]
        #[pyclass]
        struct Struct;

        #[pymethods]
        impl Struct {
            #[new]
            fn new() -> Self {
                Self
            }
        }
    }
}

@Tpt Tpt mentioned this pull request Feb 26, 2024
12 tasks
Copy link
Member

@davidhewitt davidhewitt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this follow-up, I am very excited to see this feature coming together after so long!

Open question: do we want to require adding #[pymodule_export] to the elements to be exported or do we export automatically all elements with #[pyclass], #[pyfunction] or #[pymodule]?

Great question! I think automatic is likely to lead to nicer-reading code (a lot less repetition), so I'd prefer that.

If there are reasons users don't want to export they could just move the code outside the module. Or we could add a #[pyo3(no_export)] option if users really ask for it, but I'm not convinced that's necessary to add right now...

pyo3-macros-backend/src/module.rs Outdated Show resolved Hide resolved
pyo3-macros-backend/src/module.rs Outdated Show resolved Hide resolved
pyo3-macros-backend/src/module.rs Outdated Show resolved Hide resolved
@Tpt
Copy link
Contributor Author

Tpt commented Mar 1, 2024

@davidhewitt Thank you for the reviews!

Great question! I think automatic is likely to lead to nicer-reading code (a lot less repetition), so I'd prefer that.

If there are reasons users don't want to export they could just move the code outside the module. Or we could add a #[pyo3(no_export)] option if users really ask for it, but I'm not convinced that's necessary to add right now...

Perfect! Done!

Copy link
Member

@davidhewitt davidhewitt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fantastic, imo this leads to a beautiful result I've wanted for years! Thanks 🙏

!has_attribute(&item_enum.attrs, "pymodule_export"),
item.span() => "`#[pymodule_export]` may only be used on `use` statements"
);
if has_attribute(&item_enum.attrs, "pyclass") {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice way to reuse the existing machinery!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same thing about the changelog entry not being needed here, I'll remove.

@davidhewitt davidhewitt added the CI-skip-changelog Skip checking changelog entry label Mar 6, 2024
@davidhewitt davidhewitt added this pull request to the merge queue Mar 6, 2024
Merged via the queue into PyO3:main with commit fe84fed Mar 6, 2024
41 of 43 checks passed
@Tpt Tpt deleted the class-in-module branch March 6, 2024 08:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CI-skip-changelog Skip checking changelog entry
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants