From ddff8bea25e1f08716ebd9df3e6a20453b8ba02f Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Sun, 16 Jun 2024 21:28:20 -0400 Subject: [PATCH] Stabilize declarative modules (#4257) --- Cargo.toml | 4 ---- guide/src/features.md | 6 ------ guide/src/module.md | 9 +-------- newsfragments/4257.changed.md | 1 + pyo3-macros/Cargo.toml | 1 - pyo3-macros/src/lib.rs | 9 +-------- tests/test_append_to_inittab.rs | 3 --- tests/test_compile_error.rs | 4 ---- tests/test_declarative_module.rs | 2 +- tests/ui/pymodule_missing_docs.rs | 1 - 10 files changed, 4 insertions(+), 36 deletions(-) create mode 100644 newsfragments/4257.changed.md diff --git a/Cargo.toml b/Cargo.toml index ffc87bb8..d46b742b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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"] @@ -125,7 +122,6 @@ full = [ "chrono-tz", "either", "experimental-async", - "experimental-declarative-modules", "experimental-inspect", "eyre", "hashbrown", diff --git a/guide/src/features.md b/guide/src/features.md index 6a25d40c..0536b456 100644 --- a/guide/src/features.md +++ b/guide/src/features.md @@ -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. diff --git a/guide/src/module.md b/guide/src/module.md index 51d3ef91..8c604927 100644 --- a/guide/src/module.md +++ b/guide/src/module.md @@ -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::*; @@ -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::*; @@ -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. diff --git a/newsfragments/4257.changed.md b/newsfragments/4257.changed.md new file mode 100644 index 00000000..dee4a7ae --- /dev/null +++ b/newsfragments/4257.changed.md @@ -0,0 +1 @@ +The `experimental-declarative-modules` feature is now stabilized and available by default diff --git a/pyo3-macros/Cargo.toml b/pyo3-macros/Cargo.toml index e4b550cf..789b8095 100644 --- a/pyo3-macros/Cargo.toml +++ b/pyo3-macros/Cargo.toml @@ -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] diff --git a/pyo3-macros/src/lib.rs b/pyo3-macros/src/lib.rs index 64756a1c..8dbf2782 100644 --- a/pyo3-macros/src/lib.rs +++ b/pyo3-macros/src/lib.rs @@ -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, diff --git a/tests/test_append_to_inittab.rs b/tests/test_append_to_inittab.rs index da35298b..94deb16a 100644 --- a/tests/test_append_to_inittab.rs +++ b/tests/test_append_to_inittab.rs @@ -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] @@ -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| { @@ -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#" diff --git a/tests/test_compile_error.rs b/tests/test_compile_error.rs index c978e413..9e8b3b1a 100644 --- a/tests/test_compile_error.rs +++ b/tests/test_compile_error.rs @@ -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 diff --git a/tests/test_declarative_module.rs b/tests/test_declarative_module.rs index 0858f84e..061d0337 100644 --- a/tests/test_declarative_module.rs +++ b/tests/test_declarative_module.rs @@ -1,4 +1,4 @@ -#![cfg(feature = "experimental-declarative-modules")] +#![cfg(feature = "macros")] use pyo3::create_exception; use pyo3::exceptions::PyException; diff --git a/tests/ui/pymodule_missing_docs.rs b/tests/ui/pymodule_missing_docs.rs index ed7d772f..d8bf2eb4 100644 --- a/tests/ui/pymodule_missing_docs.rs +++ b/tests/ui/pymodule_missing_docs.rs @@ -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 {}