From 731bc82ebc5b5051f000f2b2ad0b754fd817532e Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Tue, 12 Mar 2024 17:14:50 -0400 Subject: [PATCH] Trim injected python_version marker to (major, minor) --- crates/uv-interpreter/src/python_version.rs | 32 +++++++++++++++++---- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/crates/uv-interpreter/src/python_version.rs b/crates/uv-interpreter/src/python_version.rs index 6b75d1e4402b7..10296f63950b3 100644 --- a/crates/uv-interpreter/src/python_version.rs +++ b/crates/uv-interpreter/src/python_version.rs @@ -57,16 +57,38 @@ impl PythonVersion { pub fn markers(self, base: &MarkerEnvironment) -> MarkerEnvironment { let mut markers = base.clone(); + // Construct the `python_full_version` and `python_version` markers. + let major = self.release().first().copied().unwrap_or(0); + let minor = self.release().get(1).copied().unwrap_or(0); + let patch = self.release().get(2).copied().unwrap_or(0); + + // Ex) `python_full_version == "3.12.0"` + let python_full_version = Version::new([major, minor, patch]) + .with_pre(self.0.pre()) + .with_post(self.0.post()); + + // Ex) `python_version == "3.12"` + let python_version = Version::new([major, minor]); + // Ex) `implementation_version == "3.12.0"` if markers.implementation_name == "cpython" { - markers.implementation_version = self.0.clone(); + markers.implementation_version = StringVersion { + string: python_full_version.to_string(), + version: python_full_version.clone(), + }; } // Ex) `python_full_version == "3.12.0"` - markers.python_full_version = self.0.clone(); + markers.python_full_version = StringVersion { + string: python_full_version.to_string(), + version: python_full_version, + }; // Ex) `python_version == "3.12"` - markers.python_version = self.0; + markers.python_version = StringVersion { + string: python_version.to_string(), + version: python_version, + }; markers } @@ -78,12 +100,12 @@ impl PythonVersion { /// Return the major version of this Python version. pub fn major(&self) -> u8 { - u8::try_from(self.0.release()[0]).expect("invalid major version") + u8::try_from(self.0.release().first().copied().unwrap_or(0)).expect("invalid major version") } /// Return the minor version of this Python version. pub fn minor(&self) -> u8 { - u8::try_from(self.0.release()[1]).expect("invalid minor version") + u8::try_from(self.0.release().get(1).copied().unwrap_or(0)).expect("invalid minor version") } /// Check if this Python version is satisfied by the given interpreter.