From 30e2ef5b55c7bda27f97f8fbabd823780d739661 Mon Sep 17 00:00:00 2001 From: Ian Paul Date: Sat, 12 Oct 2024 10:58:33 +0700 Subject: [PATCH 1/3] 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) --- crates/uv-resolver/src/requires_python.rs | 12 ++++++++++ .../uv-resolver/src/requires_python/tests.rs | 23 +++++++++++++++++++ crates/uv/src/commands/project/lock.rs | 2 ++ 3 files changed, 37 insertions(+) diff --git a/crates/uv-resolver/src/requires_python.rs b/crates/uv-resolver/src/requires_python.rs index 318c126da2869..5a9e23bb3cf5e 100644 --- a/crates/uv-resolver/src/requires_python.rs +++ b/crates/uv-resolver/src/requires_python.rs @@ -248,6 +248,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() { diff --git a/crates/uv-resolver/src/requires_python/tests.rs b/crates/uv-resolver/src/requires_python/tests.rs index e836e7a802c7f..6382e2d7998d4 100644 --- a/crates/uv-resolver/src/requires_python/tests.rs +++ b/crates/uv-resolver/src/requires_python/tests.rs @@ -134,3 +134,26 @@ fn upper_bound_ordering() { } } } + +#[test] +fn is_matching_without_patch() { + let test_cases = [ + ("==3.12", true), + ("==2.7", true), + ("==3.10, <3.11", true), + ("==3.10, <=3.11", true), + ("==3.12.1", false), + ("==3.12.*", false), + ("==3.*", false), + (">=3.10", false), + (">3.9", false), + ("<4.0", false), + (">=3.10, <3.11", false), + ("", false), + ]; + for (version, expected) in test_cases { + 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); + } +} diff --git a/crates/uv/src/commands/project/lock.rs b/crates/uv/src/commands/project/lock.rs index acee9ac406c5f..31c911b47a628 100644 --- a/crates/uv/src/commands/project/lock.rs +++ b/crates/uv/src/commands/project/lock.rs @@ -335,6 +335,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 does not have a patch version: `{requires_python}`. It will be interpreted as `{requires_python}.0`. Did you mean `{requires_python}.*`?"); } requires_python } else { From 708cd3d6ddbc29f2bb5f22b861dad1ef1acd54f5 Mon Sep 17 00:00:00 2001 From: Ian Paul Date: Sat, 12 Oct 2024 16:00:49 +0700 Subject: [PATCH 2/3] patch lock_requires_python_exact test --- crates/uv-resolver/src/requires_python/tests.rs | 1 - crates/uv/tests/it/lock.rs | 2 ++ 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/crates/uv-resolver/src/requires_python/tests.rs b/crates/uv-resolver/src/requires_python/tests.rs index 6382e2d7998d4..4e87fd8b6d7b9 100644 --- a/crates/uv-resolver/src/requires_python/tests.rs +++ b/crates/uv-resolver/src/requires_python/tests.rs @@ -139,7 +139,6 @@ fn upper_bound_ordering() { fn is_matching_without_patch() { let test_cases = [ ("==3.12", true), - ("==2.7", true), ("==3.10, <3.11", true), ("==3.10, <=3.11", true), ("==3.12.1", false), diff --git a/crates/uv/tests/it/lock.rs b/crates/uv/tests/it/lock.rs index c47de0151bfe8..ee1937830e7ad 100644 --- a/crates/uv/tests/it/lock.rs +++ b/crates/uv/tests/it/lock.rs @@ -3530,6 +3530,7 @@ fn lock_requires_python_exact() -> Result<()> { ----- stderr ----- Using CPython 3.12.[X] + warning: The workspace `requires-python` value does not have a patch version: `==3.12`. It will be interpreted as `==3.12.[X]`. Did you mean `==3.12.*`? Resolved 2 packages in [TIME] "###); @@ -3586,6 +3587,7 @@ fn lock_requires_python_exact() -> Result<()> { ----- stderr ----- Using CPython 3.12.[X] + warning: The workspace `requires-python` value does not have a patch version: `==3.12`. It will be interpreted as `==3.12.[X]`. Did you mean `==3.12.*`? Resolved 2 packages in [TIME] "###); From 9e1c325175f48e3ccbbcd78ac5c99056b0ce3a27 Mon Sep 17 00:00:00 2001 From: Zanie Blue Date: Tue, 15 Oct 2024 23:06:27 -0500 Subject: [PATCH 3/3] Review --- crates/uv-resolver/src/requires_python.rs | 6 +++--- crates/uv-resolver/src/requires_python/tests.rs | 4 ++-- crates/uv/src/commands/project/lock.rs | 4 ++-- crates/uv/tests/it/lock.rs | 4 ++-- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/crates/uv-resolver/src/requires_python.rs b/crates/uv-resolver/src/requires_python.rs index 5a9e23bb3cf5e..f6d2869f17207 100644 --- a/crates/uv-resolver/src/requires_python.rs +++ b/crates/uv-resolver/src/requires_python.rs @@ -248,9 +248,9 @@ 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 { + /// Returns `true` if the `Requires-Python` specifier is set to an exact version + /// without specifying a patch version. (e.g. `==3.10`) + pub fn is_exact_without_patch(&self) -> bool { match self.range.lower().as_ref() { Bound::Included(version) => { version.release().len() == 2 diff --git a/crates/uv-resolver/src/requires_python/tests.rs b/crates/uv-resolver/src/requires_python/tests.rs index 4e87fd8b6d7b9..660abd71bacda 100644 --- a/crates/uv-resolver/src/requires_python/tests.rs +++ b/crates/uv-resolver/src/requires_python/tests.rs @@ -136,7 +136,7 @@ fn upper_bound_ordering() { } #[test] -fn is_matching_without_patch() { +fn is_exact_without_patch() { let test_cases = [ ("==3.12", true), ("==3.10, <3.11", true), @@ -153,6 +153,6 @@ fn is_matching_without_patch() { for (version, expected) in test_cases { 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); + assert_eq!(requires_python.is_exact_without_patch(), expected); } } diff --git a/crates/uv/src/commands/project/lock.rs b/crates/uv/src/commands/project/lock.rs index 31c911b47a628..e893f043412b2 100644 --- a/crates/uv/src/commands/project/lock.rs +++ b/crates/uv/src/commands/project/lock.rs @@ -335,8 +335,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 does not have a patch version: `{requires_python}`. It will be interpreted as `{requires_python}.0`. Did you mean `{requires_python}.*`?"); + } else if requires_python.is_exact_without_patch() { + 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}.*`?"); } requires_python } else { diff --git a/crates/uv/tests/it/lock.rs b/crates/uv/tests/it/lock.rs index ee1937830e7ad..1ce937e1aad75 100644 --- a/crates/uv/tests/it/lock.rs +++ b/crates/uv/tests/it/lock.rs @@ -3530,7 +3530,7 @@ fn lock_requires_python_exact() -> Result<()> { ----- stderr ----- Using CPython 3.12.[X] - warning: The workspace `requires-python` value does not have a patch version: `==3.12`. It will be interpreted as `==3.12.[X]`. Did you mean `==3.12.*`? + 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.*`? Resolved 2 packages in [TIME] "###); @@ -3587,7 +3587,7 @@ fn lock_requires_python_exact() -> Result<()> { ----- stderr ----- Using CPython 3.12.[X] - warning: The workspace `requires-python` value does not have a patch version: `==3.12`. It will be interpreted as `==3.12.[X]`. Did you mean `==3.12.*`? + 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.*`? Resolved 2 packages in [TIME] "###);