Skip to content

Commit

Permalink
Stabilize declarative modules (#4257)
Browse files Browse the repository at this point in the history
  • Loading branch information
alex committed Jun 17, 2024
1 parent baae929 commit ddff8be
Show file tree
Hide file tree
Showing 10 changed files with 4 additions and 36 deletions.
4 changes: 0 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,6 @@ experimental-async = ["macros", "pyo3-macros/experimental-async"]
# and IntoPy traits
experimental-inspect = []

# Enables annotating Rust inline modules with #[pymodule] to build Python modules declaratively
experimental-declarative-modules = ["pyo3-macros/experimental-declarative-modules", "macros"]

# Enables macros: #[pyclass], #[pymodule], #[pyfunction] etc.
macros = ["pyo3-macros", "indoc", "unindent"]

Expand Down Expand Up @@ -125,7 +122,6 @@ full = [
"chrono-tz",
"either",
"experimental-async",
"experimental-declarative-modules",
"experimental-inspect",
"eyre",
"hashbrown",
Expand Down
6 changes: 0 additions & 6 deletions guide/src/features.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,6 @@ This feature adds support for `async fn` in `#[pyfunction]` and `#[pymethods]`.

The feature has some unfinished refinements and performance improvements. To help finish this off, see [issue #1632](https://github.com/PyO3/pyo3/issues/1632) and its associated draft PRs.

### `experimental-declarative-modules`

This feature allows to declare Python modules using `#[pymodule] mod my_module { ... }` syntax.

The feature has some unfinished refinements and edge cases. To help finish this off, see [issue #3900](https://github.com/PyO3/pyo3/issues/3900).

### `experimental-inspect`

This feature adds the `pyo3::inspect` module, as well as `IntoPy::type_output` and `FromPyObject::type_input` APIs to produce Python type "annotations" for Rust types.
Expand Down
9 changes: 1 addition & 8 deletions guide/src/module.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,12 @@ submodules by using `from parent_module import child_module`. For more informati

It is not necessary to add `#[pymodule]` on nested modules, which is only required on the top-level module.

## Declarative modules (experimental)
## Declarative modules

Another syntax based on Rust inline modules is also available to declare modules.
The `experimental-declarative-modules` feature must be enabled to use it.

For example:
```rust
# #[cfg(feature = "experimental-declarative-modules")]
# mod declarative_module_test {
use pyo3::prelude::*;

Expand Down Expand Up @@ -157,7 +155,6 @@ For nested modules, the name of the parent module is automatically added.
In the following example, the `Unit` class will have for `module` `my_extension.submodule` because it is properly nested
but the `Ext` class will have for `module` the default `builtins` because it not nested.
```rust
# #[cfg(feature = "experimental-declarative-modules")]
# mod declarative_module_module_attr_test {
use pyo3::prelude::*;

Expand All @@ -184,7 +181,3 @@ mod my_extension {
```
It is possible to customize the `module` value for a `#[pymodule]` with the `#[pyo3(module = "MY_MODULE")]` option.

Some changes are planned to this feature before stabilization, like automatically
filling submodules into `sys.modules` to allow easier imports (see [issue #759](https://github.com/PyO3/pyo3/issues/759)).
Macro names might also change.
See [issue #3900](https://github.com/PyO3/pyo3/issues/3900) to track this feature progress.
1 change: 1 addition & 0 deletions newsfragments/4257.changed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The `experimental-declarative-modules` feature is now stabilized and available by default
1 change: 0 additions & 1 deletion pyo3-macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ proc-macro = true
[features]
multiple-pymethods = []
experimental-async = ["pyo3-macros-backend/experimental-async"]
experimental-declarative-modules = []
gil-refs = ["pyo3-macros-backend/gil-refs"]

[dependencies]
Expand Down
9 changes: 1 addition & 8 deletions pyo3-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,7 @@ use syn::{parse::Nothing, parse_macro_input, Item};
pub fn pymodule(args: TokenStream, input: TokenStream) -> TokenStream {
parse_macro_input!(args as Nothing);
match parse_macro_input!(input as Item) {
Item::Mod(module) => if cfg!(feature = "experimental-declarative-modules") {
pymodule_module_impl(module)
} else {
Err(syn::Error::new_spanned(
module,
"#[pymodule] requires the 'experimental-declarative-modules' feature to be used on Rust modules.",
))
},
Item::Mod(module) => pymodule_module_impl(module),
Item::Fn(function) => pymodule_function_impl(function),
unsupported => Err(syn::Error::new_spanned(
unsupported,
Expand Down
3 changes: 0 additions & 3 deletions tests/test_append_to_inittab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ fn module_fn_with_functions(m: &Bound<'_, PyModule>) -> PyResult<()> {
Ok(())
}

#[cfg(feature = "experimental-declarative-modules")]
#[pymodule]
mod module_mod_with_functions {
#[pymodule_export]
Expand All @@ -27,7 +26,6 @@ fn test_module_append_to_inittab() {

append_to_inittab!(module_fn_with_functions);

#[cfg(feature = "experimental-declarative-modules")]
append_to_inittab!(module_mod_with_functions);

Python::with_gil(|py| {
Expand All @@ -43,7 +41,6 @@ assert module_fn_with_functions.foo() == 123
.unwrap();
});

#[cfg(feature = "experimental-declarative-modules")]
Python::with_gil(|py| {
py.run_bound(
r#"
Expand Down
4 changes: 0 additions & 4 deletions tests/test_compile_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,9 @@ fn test_compile_errors() {
t.compile_fail("tests/ui/not_send2.rs");
t.compile_fail("tests/ui/get_set_all.rs");
t.compile_fail("tests/ui/traverse.rs");
#[cfg(feature = "experimental-declarative-modules")]
t.compile_fail("tests/ui/invalid_pymodule_in_root.rs");
#[cfg(feature = "experimental-declarative-modules")]
t.compile_fail("tests/ui/invalid_pymodule_glob.rs");
#[cfg(feature = "experimental-declarative-modules")]
t.compile_fail("tests/ui/invalid_pymodule_trait.rs");
#[cfg(feature = "experimental-declarative-modules")]
t.compile_fail("tests/ui/invalid_pymodule_two_pymodule_init.rs");
#[cfg(feature = "experimental-async")]
#[cfg(any(not(Py_LIMITED_API), Py_3_10))] // to avoid PyFunctionArgument for &str
Expand Down
2 changes: 1 addition & 1 deletion tests/test_declarative_module.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![cfg(feature = "experimental-declarative-modules")]
#![cfg(feature = "macros")]

use pyo3::create_exception;
use pyo3::exceptions::PyException;
Expand Down
1 change: 0 additions & 1 deletion tests/ui/pymodule_missing_docs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ pub fn python_module(_m: &Bound<'_, PyModule>) -> PyResult<()> {
Ok(())
}

#[cfg(feature = "experimental-declarative-modules")]
/// Some module documentation
#[pymodule]
pub mod declarative_python_module {}
Expand Down

0 comments on commit ddff8be

Please sign in to comment.