Remove prioritized dist duplication (#14887)

`Candidate` has an optional field `prioritized`, which was mostly
redundant with `CandidateDist`. Specifically, it was only `None`, if
`CandidateDist` was `Installed`. This commit removes this duplication.
This commit is contained in:
konsti 2025-07-25 17:18:24 +02:00 committed by GitHub
parent 1146f3f62d
commit 9376cf5482
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 50 additions and 19 deletions

View File

@ -91,13 +91,21 @@ impl CompatibleDist<'_> {
} }
} }
// For installable distributions, return the prioritized distribution it was derived from.
pub fn prioritized(&self) -> Option<&PrioritizedDist> {
match self {
CompatibleDist::InstalledDist(_) => None,
CompatibleDist::SourceDist { prioritized, .. }
| CompatibleDist::CompatibleWheel { prioritized, .. }
| CompatibleDist::IncompatibleWheel { prioritized, .. } => Some(prioritized),
}
}
/// Return the set of supported platform the distribution, in terms of their markers. /// Return the set of supported platform the distribution, in terms of their markers.
pub fn implied_markers(&self) -> MarkerTree { pub fn implied_markers(&self) -> MarkerTree {
match self { match self.prioritized() {
CompatibleDist::InstalledDist(_) => MarkerTree::TRUE, Some(prioritized) => prioritized.0.markers,
CompatibleDist::SourceDist { prioritized, .. } => prioritized.0.markers, None => MarkerTree::TRUE,
CompatibleDist::CompatibleWheel { prioritized, .. } => prioritized.0.markers,
CompatibleDist::IncompatibleWheel { prioritized, .. } => prioritized.0.markers,
} }
} }
} }

View File

@ -266,7 +266,6 @@ impl CandidateSelector {
return Some(Candidate { return Some(Candidate {
name: package_name, name: package_name,
version, version,
prioritized: None,
dist: CandidateDist::Compatible(CompatibleDist::InstalledDist( dist: CandidateDist::Compatible(CompatibleDist::InstalledDist(
dist, dist,
)), )),
@ -368,7 +367,6 @@ impl CandidateSelector {
return Some(Candidate { return Some(Candidate {
name: package_name, name: package_name,
version, version,
prioritized: None,
dist: CandidateDist::Compatible(CompatibleDist::InstalledDist(dist)), dist: CandidateDist::Compatible(CompatibleDist::InstalledDist(dist)),
choice_kind: VersionChoiceKind::Installed, choice_kind: VersionChoiceKind::Installed,
}); });
@ -546,10 +544,14 @@ impl CandidateSelector {
// exclude-newer in our error messages. // exclude-newer in our error messages.
if matches!( if matches!(
candidate.dist(), candidate.dist(),
CandidateDist::Incompatible( CandidateDist::Incompatible {
IncompatibleDist::Source(IncompatibleSource::ExcludeNewer(_)) incompatible_dist: IncompatibleDist::Source(IncompatibleSource::ExcludeNewer(
| IncompatibleDist::Wheel(IncompatibleWheel::ExcludeNewer(_)) _
) )) | IncompatibleDist::Wheel(
IncompatibleWheel::ExcludeNewer(_)
),
..
}
) { ) {
continue; continue;
} }
@ -572,7 +574,7 @@ impl CandidateSelector {
// even though there are compatible wheels on PyPI. Thus, we need to ensure that we // even though there are compatible wheels on PyPI. Thus, we need to ensure that we
// return the first _compatible_ candidate across all indexes, if such a candidate // return the first _compatible_ candidate across all indexes, if such a candidate
// exists. // exists.
if matches!(candidate.dist(), CandidateDist::Incompatible(_)) { if matches!(candidate.dist(), CandidateDist::Incompatible { .. }) {
if incompatible.is_none() { if incompatible.is_none() {
incompatible = Some(candidate); incompatible = Some(candidate);
} }
@ -602,7 +604,25 @@ impl CandidateSelector {
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub(crate) enum CandidateDist<'a> { pub(crate) enum CandidateDist<'a> {
Compatible(CompatibleDist<'a>), Compatible(CompatibleDist<'a>),
Incompatible(IncompatibleDist), Incompatible {
/// The reason the prioritized distribution is incompatible.
incompatible_dist: IncompatibleDist,
/// The prioritized distribution that had no compatible wheelr or sdist.
prioritized_dist: &'a PrioritizedDist,
},
}
impl CandidateDist<'_> {
/// For an installable dist, return the prioritized distribution.
fn prioritized(&self) -> Option<&PrioritizedDist> {
match self {
CandidateDist::Compatible(dist) => dist.prioritized(),
CandidateDist::Incompatible {
incompatible_dist: _,
prioritized_dist: prioritized,
} => Some(prioritized),
}
}
} }
impl<'a> From<&'a PrioritizedDist> for CandidateDist<'a> { impl<'a> From<&'a PrioritizedDist> for CandidateDist<'a> {
@ -621,7 +641,10 @@ impl<'a> From<&'a PrioritizedDist> for CandidateDist<'a> {
} else { } else {
IncompatibleDist::Unavailable IncompatibleDist::Unavailable
}; };
CandidateDist::Incompatible(dist) CandidateDist::Incompatible {
incompatible_dist: dist,
prioritized_dist: value,
}
} }
} }
} }
@ -654,8 +677,6 @@ pub(crate) struct Candidate<'a> {
name: &'a PackageName, name: &'a PackageName,
/// The version of the package. /// The version of the package.
version: &'a Version, version: &'a Version,
/// The prioritized distribution for the package.
prioritized: Option<&'a PrioritizedDist>,
/// The distributions to use for resolving and installing the package. /// The distributions to use for resolving and installing the package.
dist: CandidateDist<'a>, dist: CandidateDist<'a>,
/// Whether this candidate was selected from a preference. /// Whether this candidate was selected from a preference.
@ -672,7 +693,6 @@ impl<'a> Candidate<'a> {
Self { Self {
name, name,
version, version,
prioritized: Some(dist),
dist: CandidateDist::from(dist), dist: CandidateDist::from(dist),
choice_kind, choice_kind,
} }
@ -709,7 +729,7 @@ impl<'a> Candidate<'a> {
/// Return the prioritized distribution for the candidate. /// Return the prioritized distribution for the candidate.
pub(crate) fn prioritized(&self) -> Option<&PrioritizedDist> { pub(crate) fn prioritized(&self) -> Option<&PrioritizedDist> {
self.prioritized self.dist.prioritized()
} }
} }

View File

@ -1271,7 +1271,10 @@ impl<InstalledPackages: InstalledPackagesProvider> ResolverState<InstalledPackag
let dist = match candidate.dist() { let dist = match candidate.dist() {
CandidateDist::Compatible(dist) => dist, CandidateDist::Compatible(dist) => dist,
CandidateDist::Incompatible(incompatibility) => { CandidateDist::Incompatible {
incompatible_dist: incompatibility,
prioritized_dist: _,
} => {
// If the version is incompatible because no distributions are compatible, exit early. // If the version is incompatible because no distributions are compatible, exit early.
return Ok(Some(ResolverVersion::Unavailable( return Ok(Some(ResolverVersion::Unavailable(
candidate.version().clone(), candidate.version().clone(),