Skip to content

Commit e71b1d0

Browse files
ianpaul10zanieb
andauthored
Warn when patch isn't specified (#7959)
When patch version isn't specified and a matching version is referenced, it will default patch to 0 which could be unclear/confusing. This PR warns the user of that default. <!-- Thank you for contributing to uv! To help us out with reviewing, please consider the following: - Does this pull request include a summary of the change? (See below.) - Does this pull request include a descriptive title? - Does this pull request include references to any relevant issues? --> ## Summary <!-- What's the purpose of the change? What does it do, and why? --> The first part of this issue #7426. Will tackle the second part mentioned (`~=`) in a separate PR once I know this is the correct way to warn users. ## Test Plan <!-- How was it tested? --> Unit tests were added --------- Co-authored-by: Zanie Blue <contact@zanie.dev>
1 parent 999b3f0 commit e71b1d0

4 files changed

Lines changed: 38 additions & 0 deletions

File tree

crates/uv-resolver/src/requires_python.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,18 @@ impl RequiresPython {
248248
self.range.lower().as_ref() == Bound::Unbounded
249249
}
250250

251+
/// Returns `true` if the `Requires-Python` specifier is set to an exact version
252+
/// without specifying a patch version. (e.g. `==3.10`)
253+
pub fn is_exact_without_patch(&self) -> bool {
254+
match self.range.lower().as_ref() {
255+
Bound::Included(version) => {
256+
version.release().len() == 2
257+
&& self.range.upper().as_ref() == Bound::Included(version)
258+
}
259+
_ => false,
260+
}
261+
}
262+
251263
/// Returns the [`RequiresPythonBound`] truncated to the major and minor version.
252264
pub fn bound_major_minor(&self) -> LowerBound {
253265
match self.range.lower().as_ref() {

crates/uv-resolver/src/requires_python/tests.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,25 @@ fn upper_bound_ordering() {
134134
}
135135
}
136136
}
137+
138+
#[test]
139+
fn is_exact_without_patch() {
140+
let test_cases = [
141+
("==3.12", true),
142+
("==3.10, <3.11", true),
143+
("==3.10, <=3.11", true),
144+
("==3.12.1", false),
145+
("==3.12.*", false),
146+
("==3.*", false),
147+
(">=3.10", false),
148+
(">3.9", false),
149+
("<4.0", false),
150+
(">=3.10, <3.11", false),
151+
("", false),
152+
];
153+
for (version, expected) in test_cases {
154+
let version_specifiers = VersionSpecifiers::from_str(version).unwrap();
155+
let requires_python = RequiresPython::from_specifiers(&version_specifiers).unwrap();
156+
assert_eq!(requires_python.is_exact_without_patch(), expected);
157+
}
158+
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,8 @@ async fn do_lock(
335335
let default =
336336
RequiresPython::greater_than_equal_version(&interpreter.python_minor_version());
337337
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}`).");
338+
} else if requires_python.is_exact_without_patch() {
339+
warn_user_once!("The workspace `requires-python` value contains an exact match without a patch version: `{requires_python}`. When omitted, the patch version is implicitly `0`, e.g., `{requires_python}.0`. Did you mean `{requires_python}.*`?");
338340
}
339341
requires_python
340342
} else {

crates/uv/tests/it/lock.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3530,6 +3530,7 @@ fn lock_requires_python_exact() -> Result<()> {
35303530

35313531
----- stderr -----
35323532
Using CPython 3.12.[X]
3533+
warning: The workspace `requires-python` value contains an exact match without a patch version: `==3.12`. When omitted, the patch version is implicitly `0`, e.g., `==3.12.[X]`. Did you mean `==3.12.*`?
35333534
Resolved 2 packages in [TIME]
35343535
"###);
35353536

@@ -3586,6 +3587,7 @@ fn lock_requires_python_exact() -> Result<()> {
35863587

35873588
----- stderr -----
35883589
Using CPython 3.12.[X]
3590+
warning: The workspace `requires-python` value contains an exact match without a patch version: `==3.12`. When omitted, the patch version is implicitly `0`, e.g., `==3.12.[X]`. Did you mean `==3.12.*`?
35893591
Resolved 2 packages in [TIME]
35903592
"###);
35913593

0 commit comments

Comments
 (0)