Skip to content

Filter locked wheels by Python implementation - #23310

Draft
charliermarsh wants to merge 1 commit into
charlie/resolve-wheel-implementationfrom
charlie/filter-implementation-wheels
Draft

charliermarsh wants to merge 1 commit into
charlie/resolve-wheel-implementationfrom
charlie/filter-implementation-wheels

Conversation

@charliermarsh

@charliermarsh charliermarsh commented Nov 19, 2026 •

Copy link
Copy Markdown
Member

Summary

In #23270, a project declares three supported platforms and requires CPython on all three. uv lock drops the Intel macOS wheels as expected but keeps four PyPy wheels for NumPy, including numpy-2.4.6-pp311-pypy311_pp73-macosx_11_0_arm64.whl. The interpreter condition alone reproduces the problem.

[project]
name = "example"
version = "0.1.0"
requires-python = ">=3.11"
dependencies = ["numpy==2.4.6"]

[tool.uv]
environments = ["implementation_name == 'cpython'"]

The resolver uses a wheel’s Python tag to decide whether a release covers a supported environment (#23335). NumPy has a source distribution, so the release qualifies regardless of its wheels. We still need to decide which files to record once we’ve selected the release.

Building on #23285, we now use the same interpreter marker in the existing per-wheel check for uv.lock and compiled pylock.toml files. For example, pp311 requires implementation_name == 'pypy'. We check the wheel’s implementation and operating system together. A project that supports CPython on Linux and PyPy on macOS can then omit PyPy wheels for Linux. We check the machine separately because, for example, 32-bit Python can report x86_64 on Linux and need an i686 wheel.

This extends a limitation of the existing platform filter. Consider a project that always needs cffi==1.17.1 to build and only needs it on Linux at runtime. On macOS, uv lock omits the macOS wheel’s hash, so uv sync --frozen --no-build-package cffi rejects the wheel during the build. I reproduced this on main, and #21608 reports a similar mismatch. Changing only the runtime marker to implementation_name == 'pypy' makes the build on macOS with CPython succeed on main and fail with this PR.

If an index supplies hashes for only some files, either filter can also discard a release’s only hash. An isolated build or an install from an exported requirements file can then accept a replacement without verifying its hash. #23337 explores a fix for both filters.

Related drafts address direct wheel metadata during lock validation (#23326), a registry entry with no files in pylock.toml (#23320), and uv sync --no-install-package for such an entry (#23338).

In the reported project with NumPy 2.4.6, this removes exactly the four PyPy wheels, taking the NumPy entry from 34 wheels to 30 and the lockfile from 12,050 bytes to 10,761.

Closes #23270.

Comment thread crates/uv-lock/src/lock/mod.rs Resolved
Comment thread crates/uv-lock/src/lock/mod.rs Resolved
Comment thread crates/uv-lock/src/lock/mod.rs Resolved
@charliermarsh charliermarsh changed the title Filter locked wheels by Python implementation while preserving a distribution Filter locked wheels by interpreter and platform together Nov 19, 2026
Comment thread crates/uv-lock/src/lock/mod.rs Resolved
@charliermarsh charliermarsh changed the title Filter locked wheels by interpreter and platform together Filter locked wheels by Python implementation and operating system Nov 19, 2026
Comment thread crates/uv-lock/src/lock/mod.rs Resolved
Comment thread crates/uv-lock/src/lock/mod.rs Resolved
Comment thread crates/uv-lock/src/lock/mod.rs Resolved
Comment thread crates/uv-lock/src/lock/mod.rs Resolved
Comment thread crates/uv-lock/src/lock/mod.rs Outdated
MAC_MARKERS.combined()
} else if platform.is_android() {
// Android interpreters before Python 3.13 report `sys_platform == 'linux'`.
ANDROID_MARKERS.combined().or(LINUX_MARKERS.combined())

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P3] Include os_name == 'posix' in the Android wheel marker

A dependency guarded by implementation_name == 'cpython' and os_name == 'nt' still retains a CPython Android wheel even when an sdist is available. LINUX_MARKERS requires posix, but ANDROID_MARKERS only requires sys_platform == 'android', which uv’s marker algebra does not exclude from os_name == 'nt'. Both Android targets in TargetTriple::os_name report posix. Add that constraint to the shared Android marker so both filtering stages use it. The merge base also retained this wheel, so this pruning improvement can follow the PR.

Comment thread crates/uv-lock/src/lock/mod.rs Resolved
Comment thread crates/uv-lock/src/lock/mod.rs Resolved
Comment thread crates/uv-lock/src/lock/mod.rs Resolved
Comment thread crates/uv-lock/src/lock/mod.rs Outdated
Comment on lines +9254 to +9255
// A runtime dependency can also be a build dependency on another supported interpreter.
// Keep its hashes available to isolated builds across the project's environments.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P3] Clarify which build hashes the implementation filter preserves

With CPython/Linux and PyPy/macOS as the supported environments, a runtime dependency guarded by implementation_name == 'pypy' still loses its CPython/Linux wheel in select_registry_wheels: the dependency marker narrows that first pass to macOS. The new pass cannot restore the wheel’s hash for an isolated build on CPython/Linux, even though that is a supported environment. This also happens at the merge base. Clarify that only the additional implementation check uses the project-wide environments; any change to the earlier platform filter can follow separately.

Comment thread crates/uv-lock/src/lock/mod.rs Outdated
Comment on lines +7672 to +7680
let needs_hash = self.wheels.iter().any(|wheel| wheel.hash.is_some());
if self
.sdist
.as_ref()
.is_some_and(|sdist| !needs_hash || sdist.hash().is_some())
|| self
.wheels
.iter()
.any(|wheel| (!needs_hash || wheel.hash.is_some()) && matches(wheel))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P3] Preserve a digest accepted by required build hash checks

In a PyPy-only lock, this guard can discard a CPython wheel carrying the package’s only SHA-256 digest because the retained PyPy wheel has an MD5 digest. If the same package version is an isolated build dependency, uv sync --frozen --require-build-hashes discards MD5 in HashStrategy::with_constraint_hashes. With no eligible lock digest left, combine_constraint_hashes accepts the entire build-constraint hash list. A registry can then supply a same-version artifact whose digest appears only in that broader constraint. Preserve at least one digest accepted by satisfies_require_hashes whenever the original wheel list had one.

Suggested change
let needs_hash = self.wheels.iter().any(|wheel| wheel.hash.is_some());
if self
.sdist
.as_ref()
.is_some_and(|sdist| !needs_hash || sdist.hash().is_some())
|| self
.wheels
.iter()
.any(|wheel| (!needs_hash || wheel.hash.is_some()) && matches(wheel))
let is_strong = uv_distribution_types::satisfies_require_hashes;
let mut hashes = self
.wheels
.iter()
.filter_map(|wheel| wheel.hash.as_ref())
.peekable();
let needs_hash = hashes.peek().is_some();
let needs_strong_hash = hashes.any(is_strong);
let preserves_hash = |hash: Option<&HashDigest>| match hash {
Some(digest) => !needs_strong_hash || is_strong(digest),
None => !needs_hash,
};
if self
.sdist
.as_ref()
.is_some_and(|sdist| preserves_hash(sdist.hash()))
|| self
.wheels
.iter()
.any(|wheel| preserves_hash(wheel.hash.as_ref()) && matches(wheel))

Comment thread crates/uv-lock/src/lock/mod.rs Outdated
Comment on lines +7680 to +7682
.any(|wheel| (!needs_hash || wheel.hash.is_some()) && matches(wheel))
{
self.wheels.retain(matches);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Keep wheels whose hashes are pinned by build constraints

A CPython-only project can lock a release with hashed py3 and pp311 wheels and a build constraint that pins the pp311 digest. The py3 hash satisfies this guard, so the writer drops pp311. The next uv lock --check on CPython fails before any build: with_constraint_hashes requires an intersection between the locked hashes and the constraint. The merge base retains both wheels. I confirmed the failure by removing only the pp311 entry from an otherwise passing lock. Before merging, retain wheels whose digests are explicitly pinned by a build constraint for that package and version.

Comment thread crates/uv/tests/lock/lock.rs Resolved
Comment thread crates/uv/tests/lock/lock.rs Resolved
Comment thread crates/uv-lock/src/lock/mod.rs Resolved
Comment thread crates/uv-lock/src/lock/mod.rs Outdated
Comment on lines +10747 to +10748
if marker.is_false() {
return true;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Keep explicitly referenced wheels available for lock validation

A local cp311-cp311-any wheel referenced by demo; implementation_name == 'pypy' can supply metadata during universal resolution, but this return removes its only entry from uv.lock. Freshness validation later uses TagPolicy::Preferred to read that metadata even on an interpreter where the dependency is inactive. The merge base retained the wheel. On uv 0.13.7, I confirmed that removing only this entry makes uv lock --check --no-build fail on CPython with “has no binary distribution”; restoring it makes the check pass. Apply implementation pruning to registry candidates rather than removing the sole wheel for an explicit path dependency.

Comment thread crates/uv-lock/src/lock/mod.rs Resolved
Comment thread crates/uv/tests/lock/lock.rs Resolved
Comment on lines +10745 to +10748
let mut implementation_marker = *marker;
implementation_marker.and(UniversalMarker::from_combined(implementations));
if implementation_marker.is_false() {
return true;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Keep wheel hashes needed by isolated builds

A project can require builddep==1 only on PyPy at runtime but unconditionally in build-system.requires. If that release has hashed CPython and PyPy Linux wheels and no sdist, the merge base records both; this intersection drops the CPython wheel. Lock::hash_strategy then applies the remaining PyPy hash to the isolated build by name and version. On CPython with uv 0.13.7, uv sync --frozen --no-editable succeeded with both entries and failed with Hash mismatch after removing only the CPython entry. For uv.lock, use the project-wide supported environments for implementation pruning rather than a runtime dependency marker, unless build hashes are recorded separately.

Comment on lines +10747 to +10748
if implementation_marker.is_false() {
return true;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Do not emit a source-less package in a universal pylock

Without explicit artifact environments, uv pip compile --universal can resolve demo; implementation_name == 'pypy' from a release containing only a cp311 wheel. The merge base retained that wheel. This return removes it, and registry_artifacts then emits neither wheels nor sdist. I confirmed that packaging.pylock.Pylock.from_dict rejects the resulting file even on CPython, where demo is inactive. Platform filtering could already empty an entry, but implementation-only markers now trigger it too. When no sdist will be written, retain an otherwise eligible wheel rather than emitting an invalid lock.

Comment thread crates/uv/tests/lock/lock.rs Resolved
Comment on lines +10747 to +10748
if implementation_marker.is_false() {
return true;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Preserve the last hash in requirements.txt exports

For a PyPy-only runtime dependency, a CPython wheel can carry the release’s only advertised hash, even when an unhashed sdist remains. Dropping that wheel makes uv export --frozen --format requirements.txt emit the version pin without --hash. If the publisher later adds a PyPy wheel to the same release, a default uv pip install -r requirements.txt on PyPy can install it. The merge-base export included the CPython wheel’s digest and would reject the new bytes. Preserve the digest for export or fail the hash-enabled export when pruning removes the last one.

Comment thread crates/uv-lock/src/lock/mod.rs Resolved
Comment thread crates/uv-lock/src/lock/mod.rs Resolved
Comment on lines +10854 to +10856
// Before Python 3.13, Android reports `sys_platform == 'linux'`.
if implementation_marker.is_disjoint(*ANDROID_MARKERS)
&& implementation_marker.is_disjoint(*LINUX_MARKERS)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P3] Limit the Android Linux fallback to older CPython versions

With requires-python = '>=3.13' and a dependency needed on CPython/Linux or on any PyPy, this keeps cp313-cp313-android_24_arm64_v8a: the CPython branch passes the Linux check, and the PyPy branch passes the later Android architecture check. Neither branch can use the wheel. CPython on Android reports sys.platform == 'android' starting in 3.13. (docs.python.org) The merge base also retained it, so this is a pruning gap, not an installation regression. A follow-up can limit the Linux alternative to environments where CPython before 3.13 is reachable.

Comment on lines +38288 to +38289
let lock = context.read("uv.lock").parse::<toml_edit::DocumentMut>()?;
assert_snapshot!(lock["package"][0], @r#"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P3] Snapshot the supported environments alongside the retained wheels

Snapshotting lock["package"][0] assumes demo sorts first and omits the supported-markers that justify dropping its other wheels. The test would still pass if the writer pruned the right files but serialized a broader set of supported environments. With only two packages, snapshotting context.read("uv.lock") directly, as the neighboring lock_x86 test does, checks both sides of that contract without a TOML parse or another command.

Comment on lines +10760 to +10761
let windows_only = platform_tags.iter().all(PlatformTag::is_windows);
if windows_only && implementation_marker.is_disjoint(*WINDOWS_MARKERS) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P3] Use the implementation-filtered marker for Windows emulation

For a release with an sdist needed on CPython/x86 Windows or PyPy/ARM64 Windows, the CPython branch passes this Windows check, then the PyPy branch triggers the win_amd64 emulation exception. That retains a cp311-cp311-win_amd64 wheel neither branch can use. The merge base retained it too, so this is a pruning gap, not an installation regression. Check WINDOWS_ARM_MARKERS against implementation_marker in the emulation guard; the Windows OS check can then move back into the Windows block, while ordinary machine checks still use marker. This can follow separately.

"cp311-cp311-manylinux_2_17_x86_64",
// Both 32-bit Linux and Android on Python 3.11 can report Linux x86_64.
"cp311-cp311-manylinux_2_17_i686",
"cp311-cp311-android_24_x86_64",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P3] Exercise the rejection side of the Android fallback

The only Android wheel in this fixture is expected to survive. Removing the new Android implementation check entirely would leave the snapshot unchanged: the unrestricted GraalPy branch passes the old Android platform check. Add pp311-pypy311_pp73-android_24_x86_64 to the fixture and leave it absent from the snapshot. The merge base retains it through the GraalPy branch, but the new check should reject it because PyPy is supported only on macOS. This covers the Android-specific rejection without another command.

This branch was previously deployed

1 inactive (outdated) deployment
automations — e8dd431a Deployed Nov 20, 2026 by charliermarsh via check | crates / cargo-vet in . #54240
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

implementation_name filter doesn't work in environment

1 participant