Explain hash tie-breaking in distribution comparisons (#3581)

This commit is contained in:
Charlie Marsh 2024-05-14 13:34:32 -04:00 committed by GitHub
parent 8ce9051296
commit 27c8c5ad44
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 26 additions and 24 deletions

View file

@ -113,7 +113,7 @@ impl Display for IncompatibleDist {
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum WheelCompatibility {
Incompatible(IncompatibleWheel),
Compatible(Hash, TagPriority),
Compatible(HashComparison, TagPriority),
}
#[derive(Debug, PartialEq, Eq, Clone)]
@ -128,7 +128,7 @@ pub enum IncompatibleWheel {
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SourceDistCompatibility {
Incompatible(IncompatibleSource),
Compatible(Hash),
Compatible(HashComparison),
}
#[derive(Debug, PartialEq, Eq, Clone)]
@ -140,7 +140,7 @@ pub enum IncompatibleSource {
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum Hash {
pub enum HashComparison {
/// The hash is present, but does not match the expected value.
Mismatched,
/// The hash is missing.
@ -217,7 +217,9 @@ impl PrioritizedDist {
/// Return the highest-priority distribution for the package version, if any.
pub fn get(&self) -> Option<CompatibleDist> {
match (&self.0.wheel, &self.0.source) {
// If both are compatible, break ties based on the hash.
// If both are compatible, break ties based on the hash outcome. For example, prefer a
// source distribution with a matching hash over a wheel with a mismatched hash. When
// the outcomes are equivalent (e.g., both have a matching hash), prefer the wheel.
(
Some((wheel, WheelCompatibility::Compatible(wheel_hash, tag_priority))),
Some((source_dist, SourceDistCompatibility::Compatible(source_hash))),

View file

@ -6,9 +6,9 @@ use tracing::instrument;
use distribution_filename::{DistFilename, SourceDistFilename, WheelFilename};
use distribution_types::{
BuiltDist, Dist, File, Hash, HashPolicy, IncompatibleSource, IncompatibleWheel, IndexUrl,
PrioritizedDist, RegistryBuiltDist, RegistrySourceDist, SourceDist, SourceDistCompatibility,
WheelCompatibility,
BuiltDist, Dist, File, HashComparison, HashPolicy, IncompatibleSource, IncompatibleWheel,
IndexUrl, PrioritizedDist, RegistryBuiltDist, RegistrySourceDist, SourceDist,
SourceDistCompatibility, WheelCompatibility,
};
use pep440_rs::Version;
use platform_tags::{TagCompatibility, Tags};
@ -134,14 +134,14 @@ impl FlatIndex {
// Check if hashes line up
let hash = if let HashPolicy::Validate(required) = hasher.get_package(&filename.name) {
if hashes.is_empty() {
Hash::Missing
HashComparison::Missing
} else if required.iter().any(|hash| hashes.contains(hash)) {
Hash::Matched
HashComparison::Matched
} else {
Hash::Mismatched
HashComparison::Mismatched
}
} else {
Hash::Matched
HashComparison::Matched
};
SourceDistCompatibility::Compatible(hash)
@ -176,14 +176,14 @@ impl FlatIndex {
// Check if hashes line up
let hash = if let HashPolicy::Validate(required) = hasher.get_package(&filename.name) {
if hashes.is_empty() {
Hash::Missing
HashComparison::Missing
} else if required.iter().any(|hash| hashes.contains(hash)) {
Hash::Matched
HashComparison::Matched
} else {
Hash::Mismatched
HashComparison::Mismatched
}
} else {
Hash::Matched
HashComparison::Matched
};
WheelCompatibility::Compatible(hash, priority)

View file

@ -7,7 +7,7 @@ use tracing::instrument;
use distribution_filename::{DistFilename, WheelFilename};
use distribution_types::{
Dist, Hash, IncompatibleSource, IncompatibleWheel, IndexUrl, PrioritizedDist,
Dist, HashComparison, IncompatibleSource, IncompatibleWheel, IndexUrl, PrioritizedDist,
SourceDistCompatibility, WheelCompatibility,
};
use pep440_rs::{Version, VersionSpecifiers};
@ -474,17 +474,17 @@ impl VersionMapLazy {
// Check if hashes line up. If hashes aren't required, they're considered matching.
let hash = if self.required_hashes.is_empty() {
Hash::Matched
HashComparison::Matched
} else {
if hashes.is_empty() {
Hash::Missing
HashComparison::Missing
} else if hashes
.iter()
.any(|hash| self.required_hashes.contains(hash))
{
Hash::Matched
HashComparison::Matched
} else {
Hash::Mismatched
HashComparison::Mismatched
}
};
@ -538,17 +538,17 @@ impl VersionMapLazy {
// Check if hashes line up. If hashes aren't required, they're considered matching.
let hash = if self.required_hashes.is_empty() {
Hash::Matched
HashComparison::Matched
} else {
if hashes.is_empty() {
Hash::Missing
HashComparison::Missing
} else if hashes
.iter()
.any(|hash| self.required_hashes.contains(hash))
{
Hash::Matched
HashComparison::Matched
} else {
Hash::Mismatched
HashComparison::Mismatched
}
};