Skip to content

Commit

Permalink
Warn when requires-python patch version isn't specified
Browse files Browse the repository at this point in the history
Warn when requires-python is set to an exact value (==) and doesn't include a patch (so it'll get set to 0)
  • Loading branch information
ianpaul10 committed Oct 11, 2024
1 parent 312eeb8 commit 653eb84
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions crates/uv-resolver/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,4 @@ url = { workspace = true }
[dev-dependencies]
insta = { version = "1.40.0" }
toml = { workspace = true }
test-case = { version = "3.3.1" }
29 changes: 29 additions & 0 deletions crates/uv-resolver/src/requires_python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,18 @@ impl RequiresPython {
self.range.lower().as_ref() == Bound::Unbounded
}

/// Returns `true` if the `Requires-Python` specifier is set to a specific version
/// without a patch version. (e.g. `==3.10`)
pub fn is_matching_without_patch(&self) -> bool {
match self.range.lower().as_ref() {
Bound::Included(version) => {
version.release().len() == 2
&& self.range.upper().as_ref() == Bound::Included(version)
}
_ => false,
}
}

/// Returns the [`RequiresPythonBound`] truncated to the major and minor version.
pub fn bound_major_minor(&self) -> LowerBound {
match self.range.lower().as_ref() {
Expand Down Expand Up @@ -711,6 +723,7 @@ mod tests {
use std::cmp::Ordering;
use std::collections::Bound;
use std::str::FromStr;
use test_case::test_case;

use uv_distribution_filename::WheelFilename;
use uv_pep440::{Version, VersionSpecifiers};
Expand Down Expand Up @@ -820,4 +833,20 @@ mod tests {
}
}
}

#[test_case("==3.12", true)]
#[test_case("==2.7", true)]
#[test_case("==3.12.1", false)]
#[test_case("==3.12.*", false)]
#[test_case("==3.*", false)]
#[test_case(">=3.10", false)]
#[test_case(">3.9", false)]
#[test_case("<4.0", false)]
#[test_case(">=3.10, <3.11", false)]
#[test_case("", false)]
fn is_matching_without_patch(version: &str, expected: bool) {
let version_specifiers = VersionSpecifiers::from_str(version).unwrap();
let requires_python = RequiresPython::from_specifiers(&version_specifiers).unwrap();
assert_eq!(requires_python.is_matching_without_patch(), expected);
}
}
2 changes: 2 additions & 0 deletions crates/uv/src/commands/project/lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,8 @@ async fn do_lock(
let default =
RequiresPython::greater_than_equal_version(&interpreter.python_minor_version());
warn_user_once!("The workspace `requires-python` value does not contain a lower bound: `{requires_python}`. Set a lower bound to indicate the minimum compatible Python version (e.g., `{default}`).");
} else if requires_python.is_matching_without_patch() {
warn_user_once!("The workspace `requires-python` value is a matching reference without a patch version: `{requires_python}`. It will be interpreted as `{requires_python}.0`. Did you mean `{requires_python}.*`?");
}
requires_python
} else {
Expand Down

0 comments on commit 653eb84

Please sign in to comment.