Merge uv-pubgrub into uv-pep440 (#8669)

This commit is contained in:
konsti 2024-10-29 20:15:18 +01:00 committed by GitHub
parent f903cd2cce
commit e5b8cdba70
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
18 changed files with 96 additions and 106 deletions

View file

@ -5,14 +5,16 @@ use std::collections::Bound;
use std::ops::Deref;
use uv_distribution_filename::WheelFilename;
use uv_pep440::{Version, VersionSpecifier, VersionSpecifiers};
use uv_pep440::{
Version, VersionRangesSpecifier, VersionRangesSpecifierError, VersionSpecifier,
VersionSpecifiers,
};
use uv_pep508::{MarkerExpression, MarkerTree, MarkerValueVersion};
use uv_pubgrub::PubGrubSpecifier;
#[derive(thiserror::Error, Debug)]
pub enum RequiresPythonError {
#[error(transparent)]
PubGrub(#[from] crate::pubgrub::PubGrubSpecifierError),
VersionRangesSpecifier(#[from] VersionRangesSpecifierError),
}
/// The `Requires-Python` requirement specifier.
@ -53,10 +55,11 @@ impl RequiresPython {
/// Returns a [`RequiresPython`] from a version specifier.
pub fn from_specifiers(specifiers: &VersionSpecifiers) -> Result<Self, RequiresPythonError> {
let (lower_bound, upper_bound) = PubGrubSpecifier::from_release_specifiers(specifiers)?
.bounding_range()
.map(|(lower_bound, upper_bound)| (lower_bound.cloned(), upper_bound.cloned()))
.unwrap_or((Bound::Unbounded, Bound::Unbounded));
let (lower_bound, upper_bound) =
VersionRangesSpecifier::from_release_specifiers(specifiers)?
.bounding_range()
.map(|(lower_bound, upper_bound)| (lower_bound.cloned(), upper_bound.cloned()))
.unwrap_or((Bound::Unbounded, Bound::Unbounded));
Ok(Self {
specifiers: specifiers.clone(),
range: RequiresPythonRange(LowerBound(lower_bound), UpperBound(upper_bound)),
@ -72,7 +75,7 @@ impl RequiresPython {
// Convert to PubGrub range and perform an intersection.
let range = specifiers
.into_iter()
.map(PubGrubSpecifier::from_release_specifiers)
.map(VersionRangesSpecifier::from_release_specifiers)
.fold_ok(None, |range: Option<Range<Version>>, requires_python| {
if let Some(range) = range {
Some(range.intersection(&requires_python.into()))
@ -237,7 +240,7 @@ impl RequiresPython {
/// provided range. However, `>=3.9` would not be considered compatible, as the
/// `Requires-Python` includes Python 3.8, but `>=3.9` does not.
pub fn is_contained_by(&self, target: &VersionSpecifiers) -> bool {
let Ok(target) = PubGrubSpecifier::from_release_specifiers(target) else {
let Ok(target) = VersionRangesSpecifier::from_release_specifiers(target) else {
return false;
};
let target = target
@ -485,11 +488,12 @@ impl serde::Serialize for RequiresPython {
impl<'de> serde::Deserialize<'de> for RequiresPython {
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
let specifiers = VersionSpecifiers::deserialize(deserializer)?;
let (lower_bound, upper_bound) = PubGrubSpecifier::from_release_specifiers(&specifiers)
.map_err(serde::de::Error::custom)?
.bounding_range()
.map(|(lower_bound, upper_bound)| (lower_bound.cloned(), upper_bound.cloned()))
.unwrap_or((Bound::Unbounded, Bound::Unbounded));
let (lower_bound, upper_bound) =
VersionRangesSpecifier::from_release_specifiers(&specifiers)
.map_err(serde::de::Error::custom)?
.bounding_range()
.map(|(lower_bound, upper_bound)| (lower_bound.cloned(), upper_bound.cloned()))
.unwrap_or((Bound::Unbounded, Bound::Unbounded));
Ok(Self {
specifiers,
range: RequiresPythonRange(LowerBound(lower_bound), UpperBound(upper_bound)),