Skip to content

Commit 85c01b2

Browse files
authored
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 PyO3/maturin@342239a ## 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.
1 parent 424e588 commit 85c01b2

File tree

2 files changed

+30
-33
lines changed

2 files changed

+30
-33
lines changed

crates/uv/src/commands/project/init.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1075,7 +1075,7 @@ fn pyproject_build_backend_prerequisites(
10751075
[package]
10761076
name = "{module_name}"
10771077
version = "0.1.0"
1078-
edition = "2021"
1078+
edition = "2024"
10791079
10801080
[lib]
10811081
name = "_core"
@@ -1085,7 +1085,7 @@ fn pyproject_build_backend_prerequisites(
10851085
[dependencies]
10861086
# "extension-module" tells pyo3 we want to build an extension module (skips linking against libpython.so)
10871087
# "abi3-py39" tells pyo3 (and maturin) to build using the stable ABI with minimum Python version 3.9
1088-
pyo3 = {{ version = "0.22.4", features = ["extension-module", "abi3-py39"] }}
1088+
pyo3 = {{ version = "0.27.1", features = ["extension-module", "abi3-py39"] }}
10891089
"#},
10901090
)?;
10911091
}
@@ -1174,18 +1174,17 @@ fn generate_package_scripts(
11741174
indoc::formatdoc! {r#"
11751175
use pyo3::prelude::*;
11761176
1177-
#[pyfunction]
1178-
fn hello_from_bin() -> String {{
1179-
"Hello from {package}!".to_string()
1180-
}}
1181-
1182-
/// A Python module implemented in Rust. The name of this function must match
1177+
/// A Python module implemented in Rust. The name of this module must match
11831178
/// the `lib.name` setting in the `Cargo.toml`, else Python will not be able to
11841179
/// import the module.
11851180
#[pymodule]
1186-
fn _core(m: &Bound<'_, PyModule>) -> PyResult<()> {{
1187-
m.add_function(wrap_pyfunction!(hello_from_bin, m)?)?;
1188-
Ok(())
1181+
mod _core {{
1182+
use pyo3::prelude::*;
1183+
1184+
#[pyfunction]
1185+
fn hello_from_bin() -> String {{
1186+
"Hello from {package}!".to_string()
1187+
}}
11891188
}}
11901189
"#},
11911190
)?;

crates/uv/tests/it/init.rs

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3366,18 +3366,17 @@ fn init_app_build_backend_maturin() -> Result<()> {
33663366
lib_core_contents, @r###"
33673367
use pyo3::prelude::*;
33683368
3369-
#[pyfunction]
3370-
fn hello_from_bin() -> String {
3371-
"Hello from foo!".to_string()
3372-
}
3373-
3374-
/// A Python module implemented in Rust. The name of this function must match
3369+
/// A Python module implemented in Rust. The name of this module must match
33753370
/// the `lib.name` setting in the `Cargo.toml`, else Python will not be able to
33763371
/// import the module.
33773372
#[pymodule]
3378-
fn _core(m: &Bound<'_, PyModule>) -> PyResult<()> {
3379-
m.add_function(wrap_pyfunction!(hello_from_bin, m)?)?;
3380-
Ok(())
3373+
mod _core {
3374+
use pyo3::prelude::*;
3375+
3376+
#[pyfunction]
3377+
fn hello_from_bin() -> String {
3378+
"Hello from foo!".to_string()
3379+
}
33813380
}
33823381
"###
33833382
);
@@ -3392,7 +3391,7 @@ fn init_app_build_backend_maturin() -> Result<()> {
33923391
[package]
33933392
name = "foo"
33943393
version = "0.1.0"
3395-
edition = "2021"
3394+
edition = "2024"
33963395
33973396
[lib]
33983397
name = "_core"
@@ -3402,7 +3401,7 @@ fn init_app_build_backend_maturin() -> Result<()> {
34023401
[dependencies]
34033402
# "extension-module" tells pyo3 we want to build an extension module (skips linking against libpython.so)
34043403
# "abi3-py39" tells pyo3 (and maturin) to build using the stable ABI with minimum Python version 3.9
3405-
pyo3 = { version = "0.22.4", features = ["extension-module", "abi3-py39"] }
3404+
pyo3 = { version = "0.27.1", features = ["extension-module", "abi3-py39"] }
34063405
"###
34073406
);
34083407
});
@@ -3623,18 +3622,17 @@ fn init_lib_build_backend_maturin() -> Result<()> {
36233622
lib_core_contents, @r###"
36243623
use pyo3::prelude::*;
36253624
3626-
#[pyfunction]
3627-
fn hello_from_bin() -> String {
3628-
"Hello from foo!".to_string()
3629-
}
3630-
3631-
/// A Python module implemented in Rust. The name of this function must match
3625+
/// A Python module implemented in Rust. The name of this module must match
36323626
/// the `lib.name` setting in the `Cargo.toml`, else Python will not be able to
36333627
/// import the module.
36343628
#[pymodule]
3635-
fn _core(m: &Bound<'_, PyModule>) -> PyResult<()> {
3636-
m.add_function(wrap_pyfunction!(hello_from_bin, m)?)?;
3637-
Ok(())
3629+
mod _core {
3630+
use pyo3::prelude::*;
3631+
3632+
#[pyfunction]
3633+
fn hello_from_bin() -> String {
3634+
"Hello from foo!".to_string()
3635+
}
36383636
}
36393637
"###
36403638
);
@@ -3649,7 +3647,7 @@ fn init_lib_build_backend_maturin() -> Result<()> {
36493647
[package]
36503648
name = "foo"
36513649
version = "0.1.0"
3652-
edition = "2021"
3650+
edition = "2024"
36533651
36543652
[lib]
36553653
name = "_core"
@@ -3659,7 +3657,7 @@ fn init_lib_build_backend_maturin() -> Result<()> {
36593657
[dependencies]
36603658
# "extension-module" tells pyo3 we want to build an extension module (skips linking against libpython.so)
36613659
# "abi3-py39" tells pyo3 (and maturin) to build using the stable ABI with minimum Python version 3.9
3662-
pyo3 = { version = "0.22.4", features = ["extension-module", "abi3-py39"] }
3660+
pyo3 = { version = "0.27.1", features = ["extension-module", "abi3-py39"] }
36633661
"###
36643662
);
36653663
});

0 commit comments

Comments
 (0)