Update maturin build backend init template (#16449)

## Summary

Upgrades to the latest Rust edition and pyo3 version. 
Change initialized module to "Inline Declaration" format.
https://pyo3.rs/v0.27.1/module.html#inline-declaration

The output of `maturin new` is also updating to the new declarative
format

342239a95a

## Test Plan

Updated the relevant snapshot tests and to confirm ran
`cargo nextest run --all-features --no-fail-fast maturin`

Also used the updated template to generate a library in a different
project with
```
cargo run -- init --lib --build-backend maturin --name try-template ../_OTHER_PROJECT_/backend/try-template
```
and the generated workspace member functioned as expected.
This commit is contained in:
Matthijs Kok 2025-10-27 10:58:17 +01:00 committed by GitHub
parent 424e588213
commit 85c01b29d5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 30 additions and 33 deletions

View File

@ -1075,7 +1075,7 @@ fn pyproject_build_backend_prerequisites(
[package] [package]
name = "{module_name}" name = "{module_name}"
version = "0.1.0" version = "0.1.0"
edition = "2021" edition = "2024"
[lib] [lib]
name = "_core" name = "_core"
@ -1085,7 +1085,7 @@ fn pyproject_build_backend_prerequisites(
[dependencies] [dependencies]
# "extension-module" tells pyo3 we want to build an extension module (skips linking against libpython.so) # "extension-module" tells pyo3 we want to build an extension module (skips linking against libpython.so)
# "abi3-py39" tells pyo3 (and maturin) to build using the stable ABI with minimum Python version 3.9 # "abi3-py39" tells pyo3 (and maturin) to build using the stable ABI with minimum Python version 3.9
pyo3 = {{ version = "0.22.4", features = ["extension-module", "abi3-py39"] }} pyo3 = {{ version = "0.27.1", features = ["extension-module", "abi3-py39"] }}
"#}, "#},
)?; )?;
} }
@ -1174,18 +1174,17 @@ fn generate_package_scripts(
indoc::formatdoc! {r#" indoc::formatdoc! {r#"
use pyo3::prelude::*; use pyo3::prelude::*;
/// A Python module implemented in Rust. The name of this module must match
/// the `lib.name` setting in the `Cargo.toml`, else Python will not be able to
/// import the module.
#[pymodule]
mod _core {{
use pyo3::prelude::*;
#[pyfunction] #[pyfunction]
fn hello_from_bin() -> String {{ fn hello_from_bin() -> String {{
"Hello from {package}!".to_string() "Hello from {package}!".to_string()
}} }}
/// A Python module implemented in Rust. The name of this function must match
/// the `lib.name` setting in the `Cargo.toml`, else Python will not be able to
/// import the module.
#[pymodule]
fn _core(m: &Bound<'_, PyModule>) -> PyResult<()> {{
m.add_function(wrap_pyfunction!(hello_from_bin, m)?)?;
Ok(())
}} }}
"#}, "#},
)?; )?;

View File

@ -3366,18 +3366,17 @@ fn init_app_build_backend_maturin() -> Result<()> {
lib_core_contents, @r###" lib_core_contents, @r###"
use pyo3::prelude::*; use pyo3::prelude::*;
/// A Python module implemented in Rust. The name of this module must match
/// the `lib.name` setting in the `Cargo.toml`, else Python will not be able to
/// import the module.
#[pymodule]
mod _core {
use pyo3::prelude::*;
#[pyfunction] #[pyfunction]
fn hello_from_bin() -> String { fn hello_from_bin() -> String {
"Hello from foo!".to_string() "Hello from foo!".to_string()
} }
/// A Python module implemented in Rust. The name of this function must match
/// the `lib.name` setting in the `Cargo.toml`, else Python will not be able to
/// import the module.
#[pymodule]
fn _core(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_function(wrap_pyfunction!(hello_from_bin, m)?)?;
Ok(())
} }
"### "###
); );
@ -3392,7 +3391,7 @@ fn init_app_build_backend_maturin() -> Result<()> {
[package] [package]
name = "foo" name = "foo"
version = "0.1.0" version = "0.1.0"
edition = "2021" edition = "2024"
[lib] [lib]
name = "_core" name = "_core"
@ -3402,7 +3401,7 @@ fn init_app_build_backend_maturin() -> Result<()> {
[dependencies] [dependencies]
# "extension-module" tells pyo3 we want to build an extension module (skips linking against libpython.so) # "extension-module" tells pyo3 we want to build an extension module (skips linking against libpython.so)
# "abi3-py39" tells pyo3 (and maturin) to build using the stable ABI with minimum Python version 3.9 # "abi3-py39" tells pyo3 (and maturin) to build using the stable ABI with minimum Python version 3.9
pyo3 = { version = "0.22.4", features = ["extension-module", "abi3-py39"] } pyo3 = { version = "0.27.1", features = ["extension-module", "abi3-py39"] }
"### "###
); );
}); });
@ -3623,18 +3622,17 @@ fn init_lib_build_backend_maturin() -> Result<()> {
lib_core_contents, @r###" lib_core_contents, @r###"
use pyo3::prelude::*; use pyo3::prelude::*;
/// A Python module implemented in Rust. The name of this module must match
/// the `lib.name` setting in the `Cargo.toml`, else Python will not be able to
/// import the module.
#[pymodule]
mod _core {
use pyo3::prelude::*;
#[pyfunction] #[pyfunction]
fn hello_from_bin() -> String { fn hello_from_bin() -> String {
"Hello from foo!".to_string() "Hello from foo!".to_string()
} }
/// A Python module implemented in Rust. The name of this function must match
/// the `lib.name` setting in the `Cargo.toml`, else Python will not be able to
/// import the module.
#[pymodule]
fn _core(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_function(wrap_pyfunction!(hello_from_bin, m)?)?;
Ok(())
} }
"### "###
); );
@ -3649,7 +3647,7 @@ fn init_lib_build_backend_maturin() -> Result<()> {
[package] [package]
name = "foo" name = "foo"
version = "0.1.0" version = "0.1.0"
edition = "2021" edition = "2024"
[lib] [lib]
name = "_core" name = "_core"
@ -3659,7 +3657,7 @@ fn init_lib_build_backend_maturin() -> Result<()> {
[dependencies] [dependencies]
# "extension-module" tells pyo3 we want to build an extension module (skips linking against libpython.so) # "extension-module" tells pyo3 we want to build an extension module (skips linking against libpython.so)
# "abi3-py39" tells pyo3 (and maturin) to build using the stable ABI with minimum Python version 3.9 # "abi3-py39" tells pyo3 (and maturin) to build using the stable ABI with minimum Python version 3.9
pyo3 = { version = "0.22.4", features = ["extension-module", "abi3-py39"] } pyo3 = { version = "0.27.1", features = ["extension-module", "abi3-py39"] }
"### "###
); );
}); });