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 `anyio@3.7.0` 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 https://github.com/astral-sh/uv/issues/7393.
This commit is contained in:
Charlie Marsh 2024-09-17 23:18:05 -04:00 committed by GitHub
parent e5dd67f58e
commit fda227616c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
37 changed files with 897 additions and 50 deletions

View file

@ -451,6 +451,47 @@ specified as `KEY=VALUE` pairs.
---
### [`dependency-metadata`](#dependency-metadata) {: #dependency-metadata }
Pre-defined static metadata for dependencies of the project (direct or transitive). When
provided, enables the resolver to use the specified metadata instead of querying the
registry or building the relevant package from source.
Metadata should be provided in adherence with the [Metadata 2.3](https://packaging.python.org/en/latest/specifications/core-metadata/)
standard, though only the following fields are respected:
- `name`: The name of the package.
- (Optional) `version`: The version of the package. If omitted, the metadata will be applied
to all versions of the package.
- (Optional) `requires-dist`: The dependencies of the package (e.g., `werkzeug>=0.14`).
- (Optional) `requires-python`: The Python version required by the package (e.g., `>=3.10`).
- (Optional) `provides-extras`: The extras provided by the package.
**Default value**: `[]`
**Type**: `list[dict]`
**Example usage**:
=== "pyproject.toml"
```toml
[tool.uv]
dependency-metadata = [
{ name = "flask", version = "1.0.0", requires-dist = ["werkzeug"], requires-python = ">=3.6" },
]
```
=== "uv.toml"
```toml
dependency-metadata = [
{ name = "flask", version = "1.0.0", requires-dist = ["werkzeug"], requires-python = ">=3.6" },
]
```
---
### [`exclude-newer`](#exclude-newer) {: #exclude-newer }
Limit candidate packages to those that were uploaded prior to the given date.
@ -1474,6 +1515,48 @@ Used to reflect custom build scripts and commands that wrap `uv pip compile`.
---
#### [`dependency-metadata`](#pip_dependency-metadata) {: #pip_dependency-metadata }
<span id="dependency-metadata"></span>
Pre-defined static metadata for dependencies of the project (direct or transitive). When
provided, enables the resolver to use the specified metadata instead of querying the
registry or building the relevant package from source.
Metadata should be provided in adherence with the [Metadata 2.3](https://packaging.python.org/en/latest/specifications/core-metadata/)
standard, though only the following fields are respected:
- `name`: The name of the package.
- (Optional) `version`: The version of the package. If omitted, the metadata will be applied
to all versions of the package.
- (Optional) `requires-dist`: The dependencies of the package (e.g., `werkzeug>=0.14`).
- (Optional) `requires-python`: The Python version required by the package (e.g., `>=3.10`).
- (Optional) `provides-extras`: The extras provided by the package.
**Default value**: `[]`
**Type**: `list[dict]`
**Example usage**:
=== "pyproject.toml"
```toml
[tool.uv.pip]
dependency-metadata = [
{ name = "flask", version = "1.0.0", requires-dist = ["werkzeug"], requires-python = ">=3.6" },
]
```
=== "uv.toml"
```toml
[pip]
dependency-metadata = [
{ name = "flask", version = "1.0.0", requires-dist = ["werkzeug"], requires-python = ">=3.6" },
]
```
---
#### [`emit-build-options`](#pip_emit-build-options) {: #pip_emit-build-options }
<span id="emit-build-options"></span>