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

Commit

Permalink
Do not version strip for the ~ operator (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
gaborbernat authored May 13, 2024
1 parent 7fad932 commit 3f740bd
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ jobs:
runs-on: ubuntu-latest
environment:
name: release
url: https://pypi.org/p/pyproject-fmt
url: https://pypi.org/p/pyproject-fmt/rust
permissions:
id-token: write
if: "startsWith(github.ref, 'refs/tags/')"
Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ repos:
- id: tox-ini-fmt
args: [ "-p", "fix" ]
- repo: https://github.com/tox-dev/pyproject-fmt
rev: "2.0.1"
rev: "2.0.3"
hooks:
- id: pyproject-fmt
- repo: https://github.com/astral-sh/ruff-pre-commit
Expand Down
7 changes: 4 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "pyproject-fmt-rust"
version = "1.0.5"
version = "1.0.6"
description = "Format pyproject.toml files"
repository = "https://github.com/tox-dev/pyproject-fmt"
readme = "README.md"
Expand All @@ -15,7 +15,8 @@ crate-type = ["cdylib"]
[dependencies]
taplo = { version = "0.13.0" } # formatter
pyo3 = { version = "0.21.2", features = ["abi3-py38"] } # integration with Python
pep508_rs = { version = "0.5.0" }
pep440_rs = { version = "0.6.0" }
pep508_rs = { version = "0.6.0" }
lexical-sort = { version = "0.3.1" }
regex = { version = "1.10.4" }

Expand Down
16 changes: 13 additions & 3 deletions rust/src/helpers/pep508.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::fmt::Write;
use std::str::FromStr;

use pep440_rs::Operator;
use pep508_rs::{MarkerTree, Requirement, VersionOrUrl};

pub fn format_requirement(value: &str, keep_full_version: bool) -> String {
Expand All @@ -23,7 +24,7 @@ pub fn format_requirement(value: &str, keep_full_version: bool) -> String {
let extra_count = v.len() - 1;
for (at, spec) in v.iter().enumerate() {
let mut spec_repr = format!("{spec}");
if !keep_full_version {
if !keep_full_version && spec.operator() != &Operator::TildeEqual {
loop {
let propose = spec_repr.strip_suffix(".0");
if propose.is_none() {
Expand All @@ -39,7 +40,7 @@ pub fn format_requirement(value: &str, keep_full_version: bool) -> String {
}
}
VersionOrUrl::Url(u) => {
write!(&mut result, "{u}").unwrap();
write!(&mut result, " @ {u}").unwrap();
}
}
}
Expand Down Expand Up @@ -109,7 +110,16 @@ mod tests {
"requests[security,tests]>=2.0.0,==2.8.*; (os_name=='a' or os_name=='b') and os_name=='c' and python_version>'3.8'",
true
)]
#[case::do_not_strip_tilda("a~=3.0.0", "a~=3.0.0", false)]
#[case::url(
" pip @ https://github.com/pypa/pip/archive/1.3.1.zip#sha1=da9234ee9982d4bbb3c72346a6de940a148ea686 ",
"pip @ https://github.com/pypa/pip/archive/1.3.1.zip#sha1=da9234ee9982d4bbb3c72346a6de940a148ea686",
true
)]
fn test_format_requirement(#[case] start: &str, #[case] expected: &str, #[case] keep_full_version: bool) {
assert_eq!(format_requirement(start, keep_full_version), expected);
let got = format_requirement(start, keep_full_version);
assert_eq!(got, expected);
// formatting remains stable
assert_eq!(format_requirement(got.as_str(), keep_full_version), expected);
}
}

0 comments on commit 3f740bd

Please sign in to comment.