Skip to content
This repository was archived by the owner on Oct 17, 2024. It is now read-only.

Commit 3f740bd

Browse files
authored
Do not version strip for the ~ operator (#12)
1 parent 7fad932 commit 3f740bd

File tree

5 files changed

+22
-10
lines changed

5 files changed

+22
-10
lines changed

.github/workflows/release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ jobs:
121121
runs-on: ubuntu-latest
122122
environment:
123123
name: release
124-
url: https://pypi.org/p/pyproject-fmt
124+
url: https://pypi.org/p/pyproject-fmt/rust
125125
permissions:
126126
id-token: write
127127
if: "startsWith(github.ref, 'refs/tags/')"

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ repos:
2020
- id: tox-ini-fmt
2121
args: [ "-p", "fix" ]
2222
- repo: https://github.com/tox-dev/pyproject-fmt
23-
rev: "2.0.1"
23+
rev: "2.0.3"
2424
hooks:
2525
- id: pyproject-fmt
2626
- repo: https://github.com/astral-sh/ruff-pre-commit

Cargo.lock

Lines changed: 4 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "pyproject-fmt-rust"
3-
version = "1.0.5"
3+
version = "1.0.6"
44
description = "Format pyproject.toml files"
55
repository = "https://github.com/tox-dev/pyproject-fmt"
66
readme = "README.md"
@@ -15,7 +15,8 @@ crate-type = ["cdylib"]
1515
[dependencies]
1616
taplo = { version = "0.13.0" } # formatter
1717
pyo3 = { version = "0.21.2", features = ["abi3-py38"] } # integration with Python
18-
pep508_rs = { version = "0.5.0" }
18+
pep440_rs = { version = "0.6.0" }
19+
pep508_rs = { version = "0.6.0" }
1920
lexical-sort = { version = "0.3.1" }
2021
regex = { version = "1.10.4" }
2122

rust/src/helpers/pep508.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::fmt::Write;
22
use std::str::FromStr;
33

4+
use pep440_rs::Operator;
45
use pep508_rs::{MarkerTree, Requirement, VersionOrUrl};
56

67
pub fn format_requirement(value: &str, keep_full_version: bool) -> String {
@@ -23,7 +24,7 @@ pub fn format_requirement(value: &str, keep_full_version: bool) -> String {
2324
let extra_count = v.len() - 1;
2425
for (at, spec) in v.iter().enumerate() {
2526
let mut spec_repr = format!("{spec}");
26-
if !keep_full_version {
27+
if !keep_full_version && spec.operator() != &Operator::TildeEqual {
2728
loop {
2829
let propose = spec_repr.strip_suffix(".0");
2930
if propose.is_none() {
@@ -39,7 +40,7 @@ pub fn format_requirement(value: &str, keep_full_version: bool) -> String {
3940
}
4041
}
4142
VersionOrUrl::Url(u) => {
42-
write!(&mut result, "{u}").unwrap();
43+
write!(&mut result, " @ {u}").unwrap();
4344
}
4445
}
4546
}
@@ -109,7 +110,16 @@ mod tests {
109110
"requests[security,tests]>=2.0.0,==2.8.*; (os_name=='a' or os_name=='b') and os_name=='c' and python_version>'3.8'",
110111
true
111112
)]
113+
#[case::do_not_strip_tilda("a~=3.0.0", "a~=3.0.0", false)]
114+
#[case::url(
115+
" pip @ https://github.com/pypa/pip/archive/1.3.1.zip#sha1=da9234ee9982d4bbb3c72346a6de940a148ea686 ",
116+
"pip @ https://github.com/pypa/pip/archive/1.3.1.zip#sha1=da9234ee9982d4bbb3c72346a6de940a148ea686",
117+
true
118+
)]
112119
fn test_format_requirement(#[case] start: &str, #[case] expected: &str, #[case] keep_full_version: bool) {
113-
assert_eq!(format_requirement(start, keep_full_version), expected);
120+
let got = format_requirement(start, keep_full_version);
121+
assert_eq!(got, expected);
122+
// formatting remains stable
123+
assert_eq!(format_requirement(got.as_str(), keep_full_version), expected);
114124
}
115125
}

0 commit comments

Comments
 (0)