Skip to content

Commit

Permalink
When determining the python module name, use pyproject.toml `project.…
Browse files Browse the repository at this point in the history
…name` over Cargo.toml `package.name`.

I propose to change the precedence for determining the module name to consider before pyproject.toml `project.name` over Cargo.toml `package.name`. This came up in ruff (astral-sh/ruff#4397, astral-sh/ruff#4399), where the crate name is `ruff_cli` and the project name is `ruff`.

I'm not sure if there are any cases a user would like the crate name over the package name.
  • Loading branch information
konstin committed May 12, 2023
1 parent e378ddd commit 02f5764
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
2 changes: 2 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

* When determining the python module name, use pyproject.toml `project.name` over Cargo.toml `package.name`.

## [0.15.1] - 2023-05-07

* Fix finding interpreters from bundled sysconfigs in [#1598](https://github.com/PyO3/maturin/pull/1598)
Expand Down
21 changes: 12 additions & 9 deletions src/project_layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,17 +119,20 @@ impl ProjectResolver {

// If the package name contains minuses, you must declare a module with
// underscores as lib name
let module_name = cargo_toml
.lib
.as_ref()
.and_then(|lib| lib.name.as_ref())
// Precedence:
// * Explicitly declared pyproject.toml `tool.maturin.module-name`
// * Cargo.toml `lib.name`
// * pyproject.toml `project.name`
// * Cargo.toml `package.name`
let module_name = pyproject
.and_then(|x| x.module_name())
.or(cargo_toml.lib.as_ref().and_then(|lib| lib.name.as_deref()))
.or(pyproject
.and_then(|pyproject| pyproject.project.as_ref())
.map(|project| project.name.as_str()))
.unwrap_or(crate_name)
.to_owned();

let extension_name = pyproject
.and_then(|x| x.module_name())
.unwrap_or(&module_name);

let project_root = if pyproject_file.is_file() {
pyproject_file.parent().unwrap_or(manifest_dir)
} else {
Expand Down Expand Up @@ -176,7 +179,7 @@ impl ProjectResolver {
}
});
let project_layout =
ProjectLayout::determine(project_root, extension_name, py_root, python_packages, data)?;
ProjectLayout::determine(project_root, &module_name, py_root, python_packages, data)?;
Ok(Self {
project_layout,
cargo_toml_path: manifest_file,
Expand Down

0 comments on commit 02f5764

Please sign in to comment.