Serialize Python requests for tools as canonicalized strings (#14109)

When working on support for reading global Python pins in tool
operations, I noticed that we weren't using the canonicalized Python
request in receipts — we were using the raw string provided by the user.
Since we'll need to compare these values, we should be using the
canonicalized string.

The `Tool` and `ToolReceipt` types have been updated to hold a
`PythonRequest` instead of a `String`, and `Serialize` was implemented
for `PythonRequest` so canonicalization can happen at the edge instead
of being the caller's responsibility.
This commit is contained in:
Zanie Blue 2025-06-17 17:45:11 -05:00 committed by GitHub
parent 6c096246d8
commit 47c522f9be
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 46 additions and 13 deletions

View File

@ -67,6 +67,26 @@ pub enum PythonRequest {
Key(PythonDownloadRequest), Key(PythonDownloadRequest),
} }
impl<'a> serde::Deserialize<'a> for PythonRequest {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'a>,
{
let s = String::deserialize(deserializer)?;
Ok(PythonRequest::parse(&s))
}
}
impl serde::Serialize for PythonRequest {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let s = self.to_canonical_string();
serializer.serialize_str(&s)
}
}
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, serde::Deserialize)] #[derive(Debug, Default, Clone, Copy, PartialEq, Eq, serde::Deserialize)]
#[serde(deny_unknown_fields, rename_all = "kebab-case")] #[serde(deny_unknown_fields, rename_all = "kebab-case")]
#[cfg_attr(feature = "clap", derive(clap::ValueEnum))] #[cfg_attr(feature = "clap", derive(clap::ValueEnum))]

View File

@ -7,6 +7,7 @@ use toml_edit::{Array, Item, Table, Value, value};
use uv_distribution_types::Requirement; use uv_distribution_types::Requirement;
use uv_fs::{PortablePath, Simplified}; use uv_fs::{PortablePath, Simplified};
use uv_pypi_types::VerbatimParsedUrl; use uv_pypi_types::VerbatimParsedUrl;
use uv_python::PythonRequest;
use uv_settings::ToolOptions; use uv_settings::ToolOptions;
/// A tool entry. /// A tool entry.
@ -22,7 +23,7 @@ pub struct Tool {
/// The build constraints requested by the user during installation. /// The build constraints requested by the user during installation.
build_constraints: Vec<Requirement>, build_constraints: Vec<Requirement>,
/// The Python requested by the user during installation. /// The Python requested by the user during installation.
python: Option<String>, python: Option<PythonRequest>,
/// A mapping of entry point names to their metadata. /// A mapping of entry point names to their metadata.
entrypoints: Vec<ToolEntrypoint>, entrypoints: Vec<ToolEntrypoint>,
/// The [`ToolOptions`] used to install this tool. /// The [`ToolOptions`] used to install this tool.
@ -40,7 +41,7 @@ struct ToolWire {
overrides: Vec<Requirement>, overrides: Vec<Requirement>,
#[serde(default)] #[serde(default)]
build_constraint_dependencies: Vec<Requirement>, build_constraint_dependencies: Vec<Requirement>,
python: Option<String>, python: Option<PythonRequest>,
entrypoints: Vec<ToolEntrypoint>, entrypoints: Vec<ToolEntrypoint>,
#[serde(default)] #[serde(default)]
options: ToolOptions, options: ToolOptions,
@ -164,7 +165,7 @@ impl Tool {
constraints: Vec<Requirement>, constraints: Vec<Requirement>,
overrides: Vec<Requirement>, overrides: Vec<Requirement>,
build_constraints: Vec<Requirement>, build_constraints: Vec<Requirement>,
python: Option<String>, python: Option<PythonRequest>,
entrypoints: impl Iterator<Item = ToolEntrypoint>, entrypoints: impl Iterator<Item = ToolEntrypoint>,
options: ToolOptions, options: ToolOptions,
) -> Self { ) -> Self {
@ -280,7 +281,13 @@ impl Tool {
} }
if let Some(ref python) = self.python { if let Some(ref python) = self.python {
table.insert("python", value(python)); table.insert(
"python",
value(serde::Serialize::serialize(
&python,
toml_edit::ser::ValueSerializer::new(),
)?),
);
} }
table.insert("entrypoints", { table.insert("entrypoints", {
@ -327,7 +334,7 @@ impl Tool {
&self.build_constraints &self.build_constraints
} }
pub fn python(&self) -> &Option<String> { pub fn python(&self) -> &Option<PythonRequest> {
&self.python &self.python
} }

View File

@ -158,14 +158,18 @@ pub(crate) async fn refine_interpreter(
Ok(Some(interpreter)) Ok(Some(interpreter))
} }
/// Installs tool executables for a given package and handles any conflicts. /// Finalizes a tool installation, after creation of an environment.
pub(crate) fn install_executables( ///
/// Installs tool executables for a given package, handling any conflicts.
///
/// Adds a receipt for the tool.
pub(crate) fn finalize_tool_install(
environment: &PythonEnvironment, environment: &PythonEnvironment,
name: &PackageName, name: &PackageName,
installed_tools: &InstalledTools, installed_tools: &InstalledTools,
options: ToolOptions, options: ToolOptions,
force: bool, force: bool,
python: Option<String>, python: Option<PythonRequest>,
requirements: Vec<Requirement>, requirements: Vec<Requirement>,
constraints: Vec<Requirement>, constraints: Vec<Requirement>,
overrides: Vec<Requirement>, overrides: Vec<Requirement>,

View File

@ -33,7 +33,9 @@ use crate::commands::project::{
EnvironmentSpecification, PlatformState, ProjectError, resolve_environment, resolve_names, EnvironmentSpecification, PlatformState, ProjectError, resolve_environment, resolve_names,
sync_environment, update_environment, sync_environment, update_environment,
}; };
use crate::commands::tool::common::{install_executables, refine_interpreter, remove_entrypoints}; use crate::commands::tool::common::{
finalize_tool_install, refine_interpreter, remove_entrypoints,
};
use crate::commands::tool::{Target, ToolRequest}; use crate::commands::tool::{Target, ToolRequest};
use crate::commands::{diagnostics, reporters::PythonDownloadReporter}; use crate::commands::{diagnostics, reporters::PythonDownloadReporter};
use crate::printer::Printer; use crate::printer::Printer;
@ -592,13 +594,13 @@ pub(crate) async fn install(
} }
}; };
install_executables( finalize_tool_install(
&environment, &environment,
&from.name, &from.name,
&installed_tools, &installed_tools,
options, options,
force || invalid_tool_receipt, force || invalid_tool_receipt,
python, python_request,
requirements, requirements,
constraints, constraints,
overrides, overrides,

View File

@ -29,7 +29,7 @@ use crate::commands::project::{
}; };
use crate::commands::reporters::PythonDownloadReporter; use crate::commands::reporters::PythonDownloadReporter;
use crate::commands::tool::common::remove_entrypoints; use crate::commands::tool::common::remove_entrypoints;
use crate::commands::{ExitStatus, conjunction, tool::common::install_executables}; use crate::commands::{ExitStatus, conjunction, tool::common::finalize_tool_install};
use crate::printer::Printer; use crate::printer::Printer;
use crate::settings::{NetworkSettings, ResolverInstallerSettings}; use crate::settings::{NetworkSettings, ResolverInstallerSettings};
@ -375,7 +375,7 @@ async fn upgrade_tool(
remove_entrypoints(&existing_tool_receipt); remove_entrypoints(&existing_tool_receipt);
// If we modified the target tool, reinstall the entrypoints. // If we modified the target tool, reinstall the entrypoints.
install_executables( finalize_tool_install(
&environment, &environment,
name, name,
installed_tools, installed_tools,