Skip to content

Commit

Permalink
Properly handle dynamic version in pyproject.toml
Browse files Browse the repository at this point in the history
  • Loading branch information
messense committed Dec 18, 2024
1 parent 4005ecc commit 8ce320b
Show file tree
Hide file tree
Showing 14 changed files with 28 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use pep508_rs::{MarkerExpression, MarkerOperator, MarkerTree, MarkerValue, Requi
use pyproject_toml::License;
use regex::Regex;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::collections::{HashMap, HashSet};
use std::fmt::Write as _;
use std::path::{Path, PathBuf};
use std::str;
Expand Down Expand Up @@ -134,10 +134,23 @@ impl Metadata24 {
) -> Result<()> {
let pyproject_dir = pyproject_dir.as_ref();
if let Some(project) = &pyproject_toml.project {
self.name.clone_from(&project.name);
let dynamic: HashSet<&str> = project
.dynamic
.as_ref()
.map(|x| x.iter().map(AsRef::as_ref).collect())
.unwrap_or_default();
if dynamic.contains("name") {
bail!("`project.dynamic` must not specify `name` in pyproject.toml");
}

self.name.clone_from(&project.name);
if let Some(version) = &project.version {
if dynamic.contains("version") {
bail!("`project.dynamic` must not specify `version` when `project.version` is present in pyproject.toml");
}
self.version = version.clone();
} else if !dynamic.contains("version") {
bail!("`project.version` field is required in pyproject.toml unless it is present in the `project.dynamic` list");
}

if let Some(description) = &project.description {
Expand Down
1 change: 1 addition & 0 deletions test-crates/cffi-mixed/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ build-backend = "maturin"
[project]
name = "cffi-mixed"
dependencies = ["cffi"]
dynamic = ["version"]

[tool.maturin]
bindings = "cffi"
1 change: 1 addition & 0 deletions test-crates/cffi-pure/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ build-backend = "maturin"
[project]
name = "cffi-pure"
dependencies = ["cffi"]
dynamic = ["version"]

[tool.maturin]
bindings = "cffi"
1 change: 1 addition & 0 deletions test-crates/license-test/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ build-backend = "maturin"
[project]
name = "license-test"
license = { file = "LICENCE.txt" }
dynamic = ["version"]

[tool.maturin]
bindings = "bin"
1 change: 1 addition & 0 deletions test-crates/pyo3-ffi-pure/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ maintainers = [
{name = "messense", email = "messense@icloud.com"}
]
license = { file = "LICENSE" }
dynamic = ["version"]
1 change: 1 addition & 0 deletions test-crates/pyo3-mixed-implicit/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ classifiers = [
"Programming Language :: Python",
"Programming Language :: Rust"
]
dynamic = ["version"]

[tool.maturin]
features = ["pyo3/extension-module"]
Expand Down
1 change: 1 addition & 0 deletions test-crates/pyo3-mixed-include-exclude/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ build-backend = "maturin"
name = "pyo3-mixed-include-exclude"
classifiers = ["Programming Language :: Python", "Programming Language :: Rust"]
requires-python = ">=3.7"
dynamic = ["version"]

[project.scripts]
get_42 = "pyo3_mixed_include_exclude:get_42"
Expand Down
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 @@ -9,6 +9,7 @@ classifiers = [
"Programming Language :: Rust"
]
requires-python = ">=3.6"
dynamic = ["version"]

[project.scripts]
get_42 = "pyo3_mixed_py_subdir:get_42"
Expand Down
1 change: 1 addition & 0 deletions test-crates/pyo3-mixed-src/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ classifiers = [
"Programming Language :: Rust"
]
requires-python = ">=3.7"
dynamic = ["version"]

[project.scripts]
get_42 = "pyo3_mixed_src:get_42"
Expand Down
1 change: 1 addition & 0 deletions test-crates/pyo3-mixed-submodule/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ classifiers = [
"Programming Language :: Python",
"Programming Language :: Rust"
]
dynamic = ["version"]

[tool.maturin]
module-name = "pyo3_mixed_submodule.rust_module.rust"
1 change: 1 addition & 0 deletions test-crates/pyo3-mixed-with-path-dep/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ classifiers = [
"Programming Language :: Rust"
]
requires-python = ">=3.7"
dynamic = ["version"]
1 change: 1 addition & 0 deletions test-crates/pyo3-mixed-workspace/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ classifiers = [
"Programming Language :: Rust"
]
requires-python = ">=3.7"
dynamic = ["version"]

[project.scripts]
get_42 = "pyo3_mixed_workspace:get_42"
Expand Down
1 change: 1 addition & 0 deletions test-crates/pyo3-mixed/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ classifiers = [
]
requires-python = ">=3.7"
dependencies = ["boltons"]
dynamic = ["version"]

[project.scripts]
get_42 = "pyo3_mixed:get_42"
Expand Down
1 change: 1 addition & 0 deletions test-crates/with-data/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ build-backend = "maturin"
[project]
name = "with-data"
dependencies = ["cffi"]
dynamic = ["version"]

0 comments on commit 8ce320b

Please sign in to comment.