pep508: un-export fields for MarkerEnvironment

We now use the getters and setters everywhere.

There were some places where we wanted to build a `MarkerEnvironment`
out of whole cloth, usually in tests. To facilitate those use cases, we
add a `MarkerEnvironmentBuilder` that provides a convenient constructor.
It's basically like a `MarkerEnvironment::new`, but with named
parameters. That's useful here because there are so many fields (and
they many have the same type).
This commit is contained in:
Andrew Gallant 2024-05-08 19:01:44 -04:00 committed by Andrew Gallant
parent be12cfb2b8
commit 7d67b7bb49
11 changed files with 198 additions and 169 deletions

View file

@ -38,13 +38,12 @@ criterion_main!(uv);
mod resolver {
use std::path::{Path, PathBuf};
use std::str::FromStr;
use anyhow::Result;
use once_cell::sync::Lazy;
use distribution_types::{IndexLocations, Requirement, Resolution, SourceDist};
use pep508_rs::{MarkerEnvironment, StringVersion};
use pep508_rs::{MarkerEnvironment, MarkerEnvironmentBuilder};
use platform_tags::{Arch, Os, Platform, Tags};
use uv_cache::Cache;
use uv_client::RegistryClient;
@ -58,19 +57,19 @@ mod resolver {
};
static MARKERS: Lazy<MarkerEnvironment> = Lazy::new(|| {
MarkerEnvironment {
implementation_name: "cpython".to_string(),
implementation_version: StringVersion::from_str("3.11.5").unwrap(),
os_name: "posix".to_string(),
platform_machine: "arm64".to_string(),
platform_python_implementation: "CPython".to_string(),
platform_release: "21.6.0".to_string(),
platform_system: "Darwin".to_string(),
platform_version: "Darwin Kernel Version 21.6.0: Mon Aug 22 20:19:52 PDT 2022; root:xnu-8020.140.49~2/RELEASE_ARM64_T6000".to_string(),
python_full_version: StringVersion::from_str("3.11.5").unwrap(),
python_version: StringVersion::from_str("3.11").unwrap(),
sys_platform: "darwin".to_string(),
}
MarkerEnvironment::try_from(MarkerEnvironmentBuilder {
implementation_name: "cpython",
implementation_version: "3.11.5",
os_name: "posix",
platform_machine: "arm64",
platform_python_implementation: "CPython",
platform_release: "21.6.0",
platform_system: "Darwin",
platform_version: "Darwin Kernel Version 21.6.0: Mon Aug 22 20:19:52 PDT 2022; root:xnu-8020.140.49~2/RELEASE_ARM64_T6000",
python_full_version: "3.11.5",
python_version: "3.11",
sys_platform: "darwin",
}).unwrap()
});
static PLATFORM: Platform = Platform::new(

View file

@ -40,8 +40,8 @@ use unicode_width::UnicodeWidthChar;
use url::Url;
pub use marker::{
MarkerEnvironment, MarkerExpression, MarkerOperator, MarkerTree, MarkerValue,
MarkerValueString, MarkerValueVersion, MarkerWarningKind, StringVersion,
MarkerEnvironment, MarkerEnvironmentBuilder, MarkerExpression, MarkerOperator, MarkerTree,
MarkerValue, MarkerValueString, MarkerValueVersion, MarkerWarningKind, StringVersion,
};
#[cfg(feature = "pyo3")]
use pep440_rs::PyVersion;

View file

@ -21,7 +21,7 @@ use pyo3::{
};
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
use pep440_rs::{Version, VersionPattern, VersionSpecifier};
use pep440_rs::{Version, VersionParseError, VersionPattern, VersionSpecifier};
use uv_normalize::ExtraName;
use crate::cursor::Cursor;
@ -311,12 +311,12 @@ pub struct StringVersion {
}
impl FromStr for StringVersion {
type Err = String;
type Err = VersionParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(Self {
string: s.to_string(),
version: Version::from_str(s).map_err(|e| e.to_string())?,
version: Version::from_str(s)?,
})
}
}
@ -363,49 +363,49 @@ impl Deref for StringVersion {
#[derive(Clone, Debug, Eq, Hash, PartialEq, serde::Deserialize, serde::Serialize)]
#[cfg_attr(feature = "pyo3", pyclass(get_all, module = "pep508"))]
pub struct MarkerEnvironment {
pub implementation_name: String,
pub implementation_version: StringVersion,
pub os_name: String,
pub platform_machine: String,
pub platform_python_implementation: String,
pub platform_release: String,
pub platform_system: String,
pub platform_version: String,
pub python_full_version: StringVersion,
pub python_version: StringVersion,
pub sys_platform: String,
implementation_name: String,
implementation_version: StringVersion,
os_name: String,
platform_machine: String,
platform_python_implementation: String,
platform_release: String,
platform_system: String,
platform_version: String,
python_full_version: StringVersion,
python_version: StringVersion,
sys_platform: String,
}
impl MarkerEnvironment {
/// Returns of the PEP 440 version typed value of the key in the current environment
pub fn get_version(&self, key: &MarkerValueVersion) -> &Version {
match key {
MarkerValueVersion::ImplementationVersion => &self.implementation_version.version,
MarkerValueVersion::PythonFullVersion => &self.python_full_version.version,
MarkerValueVersion::PythonVersion => &self.python_version.version,
MarkerValueVersion::ImplementationVersion => &self.implementation_version().version,
MarkerValueVersion::PythonFullVersion => &self.python_full_version().version,
MarkerValueVersion::PythonVersion => &self.python_version().version,
}
}
/// Returns of the stringly typed value of the key in the current environment
pub fn get_string(&self, key: &MarkerValueString) -> &str {
match key {
MarkerValueString::ImplementationName => &self.implementation_name,
MarkerValueString::OsName | MarkerValueString::OsNameDeprecated => &self.os_name,
MarkerValueString::ImplementationName => self.implementation_name(),
MarkerValueString::OsName | MarkerValueString::OsNameDeprecated => self.os_name(),
MarkerValueString::PlatformMachine | MarkerValueString::PlatformMachineDeprecated => {
&self.platform_machine
self.platform_machine()
}
MarkerValueString::PlatformPythonImplementation
| MarkerValueString::PlatformPythonImplementationDeprecated
| MarkerValueString::PythonImplementationDeprecated => {
&self.platform_python_implementation
self.platform_python_implementation()
}
MarkerValueString::PlatformRelease => &self.platform_release,
MarkerValueString::PlatformSystem => &self.platform_system,
MarkerValueString::PlatformRelease => self.platform_release(),
MarkerValueString::PlatformSystem => self.platform_system(),
MarkerValueString::PlatformVersion | MarkerValueString::PlatformVersionDeprecated => {
&self.platform_version
self.platform_version()
}
MarkerValueString::SysPlatform | MarkerValueString::SysPlatformDeprecated => {
&self.sys_platform
self.sys_platform()
}
}
}
@ -643,7 +643,7 @@ impl MarkerEnvironment {
///
/// See also [`MarkerEnvironment::python_full_version`].
#[inline]
pub fn with_python(self, value: impl Into<StringVersion>) -> MarkerEnvironment {
pub fn with_python_version(self, value: impl Into<StringVersion>) -> MarkerEnvironment {
MarkerEnvironment {
python_version: value.into(),
..self
@ -791,6 +791,50 @@ impl MarkerEnvironment {
}
}
/// A builder for constructing a marker environment.
///
/// A value of this type can be fallibly converted to a full
/// [`MarkerEnvironment`] via `MarkerEnvironment::try_from`. This can fail when
/// the version strings given aren't valid.
///
/// The main utility of this type is for constructing dummy or test environment
/// values.
#[allow(missing_docs)]
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct MarkerEnvironmentBuilder<'a> {
pub implementation_name: &'a str,
pub implementation_version: &'a str,
pub os_name: &'a str,
pub platform_machine: &'a str,
pub platform_python_implementation: &'a str,
pub platform_release: &'a str,
pub platform_system: &'a str,
pub platform_version: &'a str,
pub python_full_version: &'a str,
pub python_version: &'a str,
pub sys_platform: &'a str,
}
impl<'a> TryFrom<MarkerEnvironmentBuilder<'a>> for MarkerEnvironment {
type Error = VersionParseError;
fn try_from(builder: MarkerEnvironmentBuilder<'a>) -> Result<Self, Self::Error> {
Ok(MarkerEnvironment {
implementation_name: builder.implementation_name.to_string(),
implementation_version: builder.implementation_version.parse()?,
os_name: builder.os_name.to_string(),
platform_machine: builder.platform_machine.to_string(),
platform_python_implementation: builder.platform_python_implementation.to_string(),
platform_release: builder.platform_release.to_string(),
platform_system: builder.platform_system.to_string(),
platform_version: builder.platform_version.to_string(),
python_full_version: builder.python_full_version.parse()?,
python_version: builder.python_version.parse()?,
sys_platform: builder.sys_platform.to_string(),
})
}
}
/// Represents one clause such as `python_version > "3.8"` in the form
/// ```text
/// <a name from the PEP508 list | a string> <an operator> <a name from the PEP508 list | a string>
@ -1736,7 +1780,7 @@ mod test {
use uv_normalize::ExtraName;
use crate::marker::{MarkerEnvironment, StringVersion};
use crate::marker::{MarkerEnvironment, MarkerEnvironmentBuilder};
use crate::{
MarkerExpression, MarkerOperator, MarkerTree, MarkerValue, MarkerValueString,
MarkerValueVersion,
@ -1747,21 +1791,20 @@ mod test {
}
fn env37() -> MarkerEnvironment {
let v37 = StringVersion::from_str("3.7").unwrap();
MarkerEnvironment {
implementation_name: String::new(),
implementation_version: v37.clone(),
os_name: "linux".to_string(),
platform_machine: String::new(),
platform_python_implementation: String::new(),
platform_release: String::new(),
platform_system: String::new(),
platform_version: String::new(),
python_full_version: v37.clone(),
python_version: v37,
sys_platform: "linux".to_string(),
}
MarkerEnvironment::try_from(MarkerEnvironmentBuilder {
implementation_name: "",
implementation_version: "3.7",
os_name: "linux",
platform_machine: "",
platform_python_implementation: "",
platform_release: "",
platform_system: "",
platform_version: "",
python_full_version: "3.7",
python_version: "3.7",
sys_platform: "linux",
})
.unwrap()
}
/// Copied from <https://github.com/pypa/packaging/blob/85ff971a250dc01db188ef9775499c15553a8c95/tests/test_markers.py#L175-L221>
@ -1803,20 +1846,20 @@ mod test {
#[test]
fn test_marker_evaluation() {
let v27 = StringVersion::from_str("2.7").unwrap();
let env27 = MarkerEnvironment {
implementation_name: String::new(),
implementation_version: v27.clone(),
os_name: "linux".to_string(),
platform_machine: String::new(),
platform_python_implementation: String::new(),
platform_release: String::new(),
platform_system: String::new(),
platform_version: String::new(),
python_full_version: v27.clone(),
python_version: v27,
sys_platform: "linux".to_string(),
};
let env27 = MarkerEnvironment::try_from(MarkerEnvironmentBuilder {
implementation_name: "",
implementation_version: "2.7",
os_name: "linux",
platform_machine: "",
platform_python_implementation: "",
platform_release: "",
platform_system: "",
platform_version: "",
python_full_version: "2.7",
python_version: "2.7",
sys_platform: "linux",
})
.unwrap();
let env37 = env37();
let marker1 = MarkerTree::from_str("python_version == '2.7'").unwrap();
let marker2 = MarkerTree::from_str(

View file

@ -118,17 +118,17 @@ impl LineHaul {
name: Some("uv".to_string()),
version: Some(version().to_string()),
}),
python: Some(markers.python_full_version.version.to_string()),
python: Some(markers.python_full_version().version.to_string()),
implementation: Option::from(Implementation {
name: Some(markers.platform_python_implementation.to_string()),
version: Some(markers.python_full_version.version.to_string()),
name: Some(markers.platform_python_implementation().to_string()),
version: Some(markers.python_full_version().version.to_string()),
}),
distro,
system: Option::from(System {
name: Some(markers.platform_system.to_string()),
release: Some(markers.platform_release.to_string()),
name: Some(markers.platform_system().to_string()),
release: Some(markers.platform_release().to_string()),
}),
cpu: Some(markers.platform_machine.to_string()),
cpu: Some(markers.platform_machine().to_string()),
// Should probably always be None in uv.
openssl_version: None,
// Should probably always be None in uv.

View file

@ -8,7 +8,7 @@ use hyper::service::service_fn;
use hyper::{Request, Response};
use hyper_util::rt::TokioIo;
use insta::{assert_json_snapshot, assert_snapshot, with_settings};
use pep508_rs::{MarkerEnvironment, StringVersion};
use pep508_rs::{MarkerEnvironment, MarkerEnvironmentBuilder};
use platform_tags::{Arch, Os, Platform};
use tokio::net::TcpListener;
use uv_cache::Cache;
@ -106,28 +106,20 @@ async fn test_user_agent_has_linehaul() -> Result<()> {
});
// Add some representative markers for an Ubuntu CI runner
let markers = MarkerEnvironment {
implementation_name: "cpython".to_string(),
implementation_version: StringVersion {
string: "3.12.2".to_string(),
version: "3.12.2".parse()?,
},
os_name: "posix".to_string(),
platform_machine: "x86_64".to_string(),
platform_python_implementation: "CPython".to_string(),
platform_release: "6.5.0-1016-azure".to_string(),
platform_system: "Linux".to_string(),
platform_version: "#16~22.04.1-Ubuntu SMP Fri Feb 16 15:42:02 UTC 2024".to_string(),
python_full_version: StringVersion {
string: "3.12.2".to_string(),
version: "3.12.2".parse()?,
},
python_version: StringVersion {
string: "3.12".to_string(),
version: "3.12".parse()?,
},
sys_platform: "linux".to_string(),
};
let markers = MarkerEnvironment::try_from(MarkerEnvironmentBuilder {
implementation_name: "cpython",
implementation_version: "3.12.2",
os_name: "posix",
platform_machine: "x86_64",
platform_python_implementation: "CPython",
platform_release: "6.5.0-1016-azure",
platform_system: "Linux",
platform_version: "#16~22.04.1-Ubuntu SMP Fri Feb 16 15:42:02 UTC 2024",
python_full_version: "3.12.2",
python_version: "3.12",
sys_platform: "linux",
})
.unwrap();
// Initialize uv-client
let cache = Cache::temp()?;

View file

@ -261,21 +261,13 @@ impl TargetTriple {
/// The returned [`MarkerEnvironment`] will preserve the base environment's Python version
/// markers, but override its platform markers.
pub fn markers(self, base: &MarkerEnvironment) -> MarkerEnvironment {
MarkerEnvironment {
// Platform markers
os_name: self.os_name().to_string(),
platform_machine: self.platform_machine().to_string(),
platform_system: self.platform_system().to_string(),
sys_platform: self.sys_platform().to_string(),
platform_release: self.platform_release().to_string(),
platform_version: self.platform_version().to_string(),
// Python version markers
implementation_name: base.implementation_name.clone(),
implementation_version: base.implementation_version.clone(),
platform_python_implementation: base.platform_python_implementation.clone(),
python_full_version: base.python_full_version.clone(),
python_version: base.python_version.clone(),
}
base.clone()
.with_os_name(self.os_name())
.with_platform_machine(self.platform_machine())
.with_platform_system(self.platform_system())
.with_sys_platform(self.sys_platform())
.with_platform_release(self.platform_release())
.with_platform_version(self.platform_version())
}
}

View file

@ -202,31 +202,31 @@ impl Interpreter {
/// Returns the Python version.
#[inline]
pub const fn python_version(&self) -> &Version {
&self.markers.python_full_version.version
pub fn python_version(&self) -> &Version {
&self.markers.python_full_version().version
}
/// Returns the `python_full_version` marker corresponding to this Python version.
#[inline]
pub const fn python_full_version(&self) -> &StringVersion {
&self.markers.python_full_version
pub fn python_full_version(&self) -> &StringVersion {
self.markers.python_full_version()
}
/// Return the major version of this Python version.
pub fn python_major(&self) -> u8 {
let major = self.markers.python_full_version.version.release()[0];
let major = self.markers.python_full_version().version.release()[0];
u8::try_from(major).expect("invalid major version")
}
/// Return the minor version of this Python version.
pub fn python_minor(&self) -> u8 {
let minor = self.markers.python_full_version.version.release()[1];
let minor = self.markers.python_full_version().version.release()[1];
u8::try_from(minor).expect("invalid minor version")
}
/// Return the patch version of this Python version.
pub fn python_patch(&self) -> u8 {
let minor = self.markers.python_full_version.version.release()[2];
let minor = self.markers.python_full_version().version.release()[2];
u8::try_from(minor).expect("invalid patch version")
}
@ -237,13 +237,13 @@ impl Interpreter {
/// Return the major version of the implementation (e.g., `CPython` or `PyPy`).
pub fn implementation_major(&self) -> u8 {
let major = self.markers.implementation_version.version.release()[0];
let major = self.markers.implementation_version().version.release()[0];
u8::try_from(major).expect("invalid major version")
}
/// Return the minor version of the implementation (e.g., `CPython` or `PyPy`).
pub fn implementation_minor(&self) -> u8 {
let minor = self.markers.implementation_version.version.release()[1];
let minor = self.markers.implementation_version().version.release()[1];
u8::try_from(minor).expect("invalid minor version")
}
@ -254,7 +254,7 @@ impl Interpreter {
/// Returns the implementation name (e.g., `CPython` or `PyPy`).
pub fn implementation_name(&self) -> &str {
&self.markers.implementation_name
self.markers.implementation_name()
}
/// Return the `sys.base_exec_prefix` path for this Python interpreter.
@ -337,7 +337,7 @@ impl Interpreter {
Layout {
python_version: self.python_tuple(),
sys_executable: self.sys_executable().to_path_buf(),
os_name: self.markers.os_name.clone(),
os_name: self.markers.os_name().to_string(),
scheme: if let Some(target) = self.target.as_ref() {
target.scheme()
} else {
@ -540,7 +540,7 @@ impl InterpreterInfo {
if cached.timestamp == modified {
debug!(
"Cached interpreter info for Python {}, skipping probing: {}",
cached.data.markers.python_full_version,
cached.data.markers.python_full_version(),
executable.user_display()
);
return Ok(cached.data);
@ -567,7 +567,7 @@ impl InterpreterInfo {
let info = Self::query(executable, cache)?;
debug!(
"Found Python {} for: {}",
info.markers.python_full_version,
info.markers.python_full_version(),
executable.display()
);
@ -670,7 +670,7 @@ mod tests {
.unwrap();
let interpreter = Interpreter::query(&mocked_interpreter, &cache).unwrap();
assert_eq!(
interpreter.markers.python_version.version,
interpreter.markers.python_version().version,
Version::from_str("3.12").unwrap()
);
fs::write(
@ -683,7 +683,7 @@ mod tests {
.unwrap();
let interpreter = Interpreter::query(&mocked_interpreter, &cache).unwrap();
assert_eq!(
interpreter.markers.python_version.version,
interpreter.markers.python_version().version,
Version::from_str("3.13").unwrap()
);
}

View file

@ -88,29 +88,29 @@ impl PythonVersion {
let mut markers = base.clone();
// Ex) `implementation_version == "3.12.0"`
if markers.implementation_name == "cpython" {
if markers.implementation_name() == "cpython" {
let python_full_version = self.python_full_version();
markers.implementation_version = StringVersion {
markers = markers.with_implementation_version(StringVersion {
// Retain the verbatim representation, provided by the user.
string: self.0.to_string(),
version: python_full_version,
};
});
}
// Ex) `python_full_version == "3.12.0"`
let python_full_version = self.python_full_version();
markers.python_full_version = StringVersion {
markers = markers.with_python_full_version(StringVersion {
// Retain the verbatim representation, provided by the user.
string: self.0.to_string(),
version: python_full_version,
};
});
// Ex) `python_version == "3.12"`
let python_version = self.python_version();
markers.python_version = StringVersion {
markers = markers.with_python_version(StringVersion {
string: python_version.to_string(),
version: python_version,
};
});
markers
}

View file

@ -20,7 +20,7 @@ impl PythonRequirement {
}
pub fn from_marker_environment(interpreter: &Interpreter, env: &MarkerEnvironment) -> Self {
Self::new(interpreter, &env.python_full_version)
Self::new(interpreter, env.python_full_version())
}
/// Return the installed version of Python.

View file

@ -11,7 +11,7 @@ use chrono::{DateTime, Utc};
use once_cell::sync::Lazy;
use distribution_types::{IndexLocations, Requirement, Resolution, SourceDist};
use pep508_rs::{MarkerEnvironment, StringVersion};
use pep508_rs::{MarkerEnvironment, MarkerEnvironmentBuilder};
use platform_tags::{Arch, Os, Platform, Tags};
use uv_cache::Cache;
use uv_client::RegistryClientBuilder;
@ -724,19 +724,19 @@ async fn msgraph_sdk() -> Result<()> {
}
static MARKERS_311: Lazy<MarkerEnvironment> = Lazy::new(|| {
MarkerEnvironment {
implementation_name: "cpython".to_string(),
implementation_version: StringVersion::from_str("3.11.5").unwrap(),
os_name: "posix".to_string(),
platform_machine: "arm64".to_string(),
platform_python_implementation: "CPython".to_string(),
platform_release: "21.6.0".to_string(),
platform_system: "Darwin".to_string(),
platform_version: "Darwin Kernel Version 21.6.0: Mon Aug 22 20:19:52 PDT 2022; root:xnu-8020.140.49~2/RELEASE_ARM64_T6000".to_string(),
python_full_version: StringVersion::from_str("3.11.5").unwrap(),
python_version: StringVersion::from_str("3.11").unwrap(),
sys_platform: "darwin".to_string(),
}
MarkerEnvironment::try_from(MarkerEnvironmentBuilder {
implementation_name: "cpython",
implementation_version: "3.11.5",
os_name: "posix",
platform_machine: "arm64",
platform_python_implementation: "CPython",
platform_release: "21.6.0",
platform_system: "Darwin",
platform_version: "Darwin Kernel Version 21.6.0: Mon Aug 22 20:19:52 PDT 2022; root:xnu-8020.140.49~2/RELEASE_ARM64_T6000",
python_full_version: "3.11.5",
python_version: "3.11",
sys_platform: "darwin",
}).unwrap()
});
static TAGS_311: Lazy<Tags> = Lazy::new(|| {
@ -757,19 +757,19 @@ static TAGS_311: Lazy<Tags> = Lazy::new(|| {
});
static MARKERS_310: Lazy<MarkerEnvironment> = Lazy::new(|| {
MarkerEnvironment {
implementation_name: "cpython".to_string(),
implementation_version: StringVersion::from_str("3.10.5").unwrap(),
os_name: "posix".to_string(),
platform_machine: "arm64".to_string(),
platform_python_implementation: "CPython".to_string(),
platform_release: "21.6.0".to_string(),
platform_system: "Darwin".to_string(),
platform_version: "Darwin Kernel Version 21.6.0: Mon Aug 22 20:19:52 PDT 2022; root:xnu-8020.140.49~2/RELEASE_ARM64_T6000".to_string(),
python_full_version: StringVersion::from_str("3.10.5").unwrap(),
python_version: StringVersion::from_str("3.10").unwrap(),
sys_platform: "darwin".to_string(),
}
MarkerEnvironment::try_from(MarkerEnvironmentBuilder {
implementation_name: "cpython",
implementation_version: "3.10.5",
os_name: "posix",
platform_machine: "arm64",
platform_python_implementation: "CPython",
platform_release: "21.6.0",
platform_system: "Darwin",
platform_version: "Darwin Kernel Version 21.6.0: Mon Aug 22 20:19:52 PDT 2022; root:xnu-8020.140.49~2/RELEASE_ARM64_T6000",
python_full_version: "3.10.5",
python_version: "3.10",
sys_platform: "darwin",
}).unwrap()
});
static TAGS_310: Lazy<Tags> = Lazy::new(|| {

View file

@ -235,12 +235,15 @@ pub fn create_bare_venv(
),
(
"implementation".to_string(),
interpreter.markers().platform_python_implementation.clone(),
interpreter
.markers()
.platform_python_implementation()
.to_string(),
),
("uv".to_string(), version().to_string()),
(
"version_info".to_string(),
interpreter.markers().python_full_version.string.clone(),
interpreter.markers().python_full_version().string.clone(),
),
(
"include-system-site-packages".to_string(),