Skip to content

Commit

Permalink
0.14: Deprecate package.metadata.maturin.name in favor of `tool.mat…
Browse files Browse the repository at this point in the history
…urin.module-name` in `pyproject.toml`
  • Loading branch information
messense committed Mar 26, 2023
1 parent 3f16f84 commit 852ef77
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 14 deletions.
8 changes: 5 additions & 3 deletions guide/src/project_layout.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,13 @@ my-rust-and-python-project
```
#### Import Rust as a submodule of your project

If the Python module created by Rust has the same name as the Python package in a mixed Rust/Python project, IDEs might get confused. You might also want to discourage end users from using the Rust functions directly by giving it a different name, say '\_my_project'. This can be done by adding `name = <package name>.<rust pymodule name>` to the `[package.metadata.maturin]` in your `Cargo.toml`. For example:
If the Python module created by Rust has the same name as the Python package in a mixed Rust/Python project, IDEs might get confused.
You might also want to discourage end users from using the Rust functions directly by giving it a different name, say '\_my_project'.
This can be done by adding `module-name = <package name>.<rust pymodule name>` to the `[tool.maturin]` in your `pyproject.toml`. For example:

```toml
[package.metadata.maturin]
name = "my_project._my_project"
[tool.maturin]
module-name = "my_project._my_project"
```

You can then import your Rust module inside your Python source as follows:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# maturin is self bootstraping, however on platforms like alpine linux that aren't
# maturin is self bootstrapping, however on platforms like alpine linux that aren't
# manylinux, pip will try installing maturin from the source distribution.
# That source distribution obviously can't depend on maturin, so we're using
# the always available setuptools.
Expand Down
2 changes: 1 addition & 1 deletion src/build_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ impl BuildOptions {
bail!(
"The module name must not contain a minus `-` \
(Make sure you have set an appropriate [lib] name or \
[package.metadata.maturin] name in your Cargo.toml)"
[tool.maturin] module-name in your pyproject.toml)"
);
}

Expand Down
13 changes: 10 additions & 3 deletions src/project_layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,15 @@ impl ProjectResolver {
.unwrap_or(crate_name)
.to_owned();

let extension_name = extra_metadata.name.as_ref().unwrap_or(&module_name);

let extension_name = pyproject
.and_then(|x| x.module_name())
.or_else(|| {
if extra_metadata.name.is_some() {
eprintln!("⚠️ Warning: specify [package.metadata.maturin] name in Cargo.toml is deprecated, use module-name in [tool.maturin] section in pyproject.toml instead");
}
extra_metadata.name.as_deref()
})
.unwrap_or(&module_name);
let project_root = if pyproject_file.is_file() {
pyproject_file.parent().unwrap_or(manifest_dir)
} else {
Expand All @@ -143,7 +150,7 @@ impl ProjectResolver {
Some(py_src) => project_root.join(py_src),
None => match extra_metadata.python_source.as_ref() {
Some(py_src) => {
println!("⚠️ Warning: specify python-source in Cargo.toml is deprecated, use python-source in [tool.maturin] section in pyproject.toml instead");
eprintln!("⚠️ Warning: specify python-source in Cargo.toml is deprecated, use python-source in [tool.maturin] section in pyproject.toml instead");
manifest_dir.join(py_src)
}
None => match pyproject.and_then(|x| x.project_name()) {
Expand Down
7 changes: 7 additions & 0 deletions src/pyproject_toml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ impl GlobPattern {
#[serde(rename_all = "kebab-case")]
pub struct ToolMaturin {
// maturin specific options
// extension module name, accepts setuptools style import name like `foo.bar`
module_name: Option<String>,
// TODO(0.15.0): remove deprecated
sdist_include: Option<Vec<String>>,
include: Option<Vec<GlobPattern>>,
Expand Down Expand Up @@ -172,6 +174,11 @@ impl PyProjectToml {
self.tool.as_ref()?.maturin.as_ref()
}

/// Returns the value of `[tool.maturin.module-name]` in pyproject.toml
pub fn module_name(&self) -> Option<&str> {
self.maturin()?.module_name.as_deref()
}

/// Returns the value of `[tool.maturin.sdist-include]` in pyproject.toml
#[deprecated(
since = "0.14.0",
Expand Down
3 changes: 0 additions & 3 deletions test-crates/pyo3-mixed-py-subdir/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,3 @@ pyo3 = { version = "0.18.0", features = ["extension-module"] }
[lib]
name = "pyo3_mixed_py_subdir"
crate-type = ["cdylib"]

[package.metadata.maturin]
name = "pyo3_mixed_py_subdir._pyo3_mixed"
1 change: 1 addition & 0 deletions test-crates/pyo3-mixed-py-subdir/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ requires-python = ">=3.6"
get_42 = "pyo3_mixed_py_subdir:get_42"

[tool.maturin]
module-name = "pyo3_mixed_py_subdir._pyo3_mixed"
python-source = "python"
3 changes: 0 additions & 3 deletions test-crates/pyo3-mixed-submodule/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ description = "Implements a dummy function combining rust and python"
readme = "README.md"
edition = "2021"

[package.metadata.maturin]
name = "pyo3_mixed_submodule.rust_module.rust"

[dependencies]
pyo3 = { version = "0.18.0", features = ["extension-module"] }

Expand Down
3 changes: 3 additions & 0 deletions test-crates/pyo3-mixed-submodule/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ classifiers = [
"Programming Language :: Python",
"Programming Language :: Rust"
]

[tool.maturin]
module-name = "pyo3_mixed_submodule.rust_module.rust"

0 comments on commit 852ef77

Please sign in to comment.