-
Notifications
You must be signed in to change notification settings - Fork 625
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow users to provide pre-defined metadata for resolution (#7442)
## Summary This PR enables users to provide pre-defined static metadata for dependencies. It's intended for situations in which the user depends on a package that does _not_ declare static metadata (e.g., a `setup.py`-only sdist), and that is expensive to build or even cannot be built on some architectures. For example, you might have a Linux-only dependency that can't be built on ARM -- but we need to build that package in order to generate the lockfile. By providing static metadata, the user can instruct uv to avoid building that package at all. For example, to override all `anyio` versions: ```toml [project] name = "project" version = "0.1.0" requires-python = ">=3.12" dependencies = ["anyio"] [[tool.uv.dependency-metadata]] name = "anyio" requires-dist = ["iniconfig"] ``` Or, to override a specific version: ```toml [project] name = "project" version = "0.1.0" requires-python = ">=3.12" dependencies = ["anyio"] [[tool.uv.dependency-metadata]] name = "anyio" version = "3.7.0" requires-dist = ["iniconfig"] ``` The current implementation uses `Metadata23` directly, so we adhere to the exact schema expected internally and defined by the standards. Any entries are treated similarly to overrides, in that we won't even look for `[email protected]` metadata in the above example. (In a way, this also enables #4422, since you could remove a dependency for a specific package, though it's probably too unwieldy to use in practice, since you'd need to redefine the _rest_ of the metadata, and do that for every package that requires the package you want to omit.) This is under-documented, since I want to get feedback on the core ideas and names involved. Closes #7393.
- Loading branch information
1 parent
e5dd67f
commit fda2276
Showing
37 changed files
with
897 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
use pep440_rs::{Version, VersionSpecifiers}; | ||
use pep508_rs::Requirement; | ||
use pypi_types::{Metadata23, VerbatimParsedUrl}; | ||
use rustc_hash::FxHashMap; | ||
use serde::{Deserialize, Serialize}; | ||
use uv_normalize::{ExtraName, PackageName}; | ||
|
||
/// Pre-defined [`StaticMetadata`] entries, indexed by [`PackageName`] and [`Version`]. | ||
#[derive(Debug, Clone, Default)] | ||
pub struct DependencyMetadata(FxHashMap<PackageName, Vec<StaticMetadata>>); | ||
|
||
impl DependencyMetadata { | ||
/// Index a set of [`StaticMetadata`] entries by [`PackageName`] and [`Version`]. | ||
pub fn from_entries(entries: impl IntoIterator<Item = StaticMetadata>) -> Self { | ||
let mut map = Self::default(); | ||
for entry in entries { | ||
map.0.entry(entry.name.clone()).or_default().push(entry); | ||
} | ||
map | ||
} | ||
|
||
/// Retrieve a [`StaticMetadata`] entry by [`PackageName`] and [`Version`]. | ||
pub fn get(&self, package: &PackageName, version: &Version) -> Option<Metadata23> { | ||
let versions = self.0.get(package)?; | ||
|
||
// Search for an exact, then a global match. | ||
let metadata = versions | ||
.iter() | ||
.find(|v| v.version.as_ref() == Some(version)) | ||
.or_else(|| versions.iter().find(|v| v.version.is_none()))?; | ||
|
||
Some(Metadata23 { | ||
name: metadata.name.clone(), | ||
version: version.clone(), | ||
requires_dist: metadata.requires_dist.clone(), | ||
requires_python: metadata.requires_python.clone(), | ||
provides_extras: metadata.provides_extras.clone(), | ||
}) | ||
} | ||
|
||
/// Retrieve all [`StaticMetadata`] entries. | ||
pub fn values(&self) -> impl Iterator<Item = &StaticMetadata> { | ||
self.0.values().flatten() | ||
} | ||
} | ||
|
||
/// A subset of the Python Package Metadata 2.3 standard as specified in | ||
/// <https://packaging.python.org/specifications/core-metadata/>. | ||
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, PartialOrd, Ord)] | ||
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] | ||
#[serde(rename_all = "kebab-case")] | ||
pub struct StaticMetadata { | ||
// Mandatory fields | ||
pub name: PackageName, | ||
#[cfg_attr( | ||
feature = "schemars", | ||
schemars( | ||
with = "String", | ||
description = "PEP 440-style package version, e.g., `1.2.3`" | ||
) | ||
)] | ||
pub version: Option<Version>, | ||
// Optional fields | ||
#[serde(default)] | ||
pub requires_dist: Vec<Requirement<VerbatimParsedUrl>>, | ||
#[cfg_attr( | ||
feature = "schemars", | ||
schemars( | ||
with = "Option<String>", | ||
description = "PEP 508-style Python requirement, e.g., `>=3.10`" | ||
) | ||
)] | ||
pub requires_python: Option<VersionSpecifiers>, | ||
#[serde(default)] | ||
pub provides_extras: Vec<ExtraName>, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.