Skip to content

Commit aefc391

Browse files
committed
Warn when requires-python patch version isn't specified
Warn when requires-python is set to an exact value (==) and doesn't include a patch (so it'll get set to 0)
1 parent 312eeb8 commit aefc391

File tree

4 files changed

+33
-1
lines changed

4 files changed

+33
-1
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/uv-resolver/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,4 @@ url = { workspace = true }
6363
[dev-dependencies]
6464
insta = { version = "1.40.0" }
6565
toml = { workspace = true }
66+
test-case = { version = "3.3.1" }

crates/uv-resolver/src/requires_python.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,18 @@ impl RequiresPython {
266266
self.range.lower().as_ref() == Bound::Unbounded
267267
}
268268

269+
/// Returns `true` if the `Requires-Python` specifier is set to a specific version
270+
/// without a patch version. (e.g. `==3.10`)
271+
pub fn is_matching_without_patch(&self) -> bool {
272+
match self.range.lower().as_ref() {
273+
Bound::Included(version) => {
274+
version.release().len() == 2
275+
&& self.range.upper().as_ref() == Bound::Included(version)
276+
}
277+
_ => false,
278+
}
279+
}
280+
269281
/// Returns the [`RequiresPythonBound`] truncated to the major and minor version.
270282
pub fn bound_major_minor(&self) -> LowerBound {
271283
match self.range.lower().as_ref() {
@@ -711,6 +723,7 @@ mod tests {
711723
use std::cmp::Ordering;
712724
use std::collections::Bound;
713725
use std::str::FromStr;
726+
use test_case::test_case;
714727

715728
use uv_distribution_filename::WheelFilename;
716729
use uv_pep440::{Version, VersionSpecifiers};
@@ -820,4 +833,20 @@ mod tests {
820833
}
821834
}
822835
}
836+
837+
#[test_case("==3.12", true)]
838+
#[test_case("==2.7", true)]
839+
#[test_case("==3.12.1", false)]
840+
#[test_case("==3.12.*", false)]
841+
#[test_case("==3.*", false)]
842+
#[test_case(">=3.10", false)]
843+
#[test_case(">3.9", false)]
844+
#[test_case("<4.0", false)]
845+
#[test_case(">=3.10, <3.11", false)]
846+
#[test_case("", false)]
847+
fn is_matching_without_patch(version: &str, expected: bool) {
848+
let version_specifiers = VersionSpecifiers::from_str(version).unwrap();
849+
let requires_python = RequiresPython::from_specifiers(&version_specifiers).unwrap();
850+
assert_eq!(requires_python.is_matching_without_patch(), expected);
851+
}
823852
}

crates/uv/src/commands/project/lock.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,8 @@ async fn do_lock(
334334
let default =
335335
RequiresPython::greater_than_equal_version(&interpreter.python_minor_version());
336336
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}`).");
337-
}
337+
} else if requires_python.is_matching_without_patch() {
338+
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}.*`?");
338339
requires_python
339340
} else {
340341
let default =

0 commit comments

Comments
 (0)