uv-resolver 0.0.62

This is an internal component crate of uv
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
use std::fmt::{Debug, Display, Formatter};
use std::hash::{Hash, Hasher};
use std::ops::{Bound, Deref, RangeBounds};

use pubgrub::{Ranges, SetRelation, VersionSet};

use uv_pep440::{
    LocalVersionSlice, Version, VersionSpecifiers, canonicalize_version_ranges,
    strip_local_version_sentinels,
};

/// A PEP 440 version range with encoded and canonical representations.
///
/// `encoded_versions` retains the internal sentinel bounds required for candidate membership and
/// as the source for a lossy diagnostic projection. `canonical_versions` folds those sentinels
/// onto their least valid successors. PubGrub uses the canonical representation for equality,
/// hashing, and set relations.
#[derive(Clone, Debug)]
pub struct Range<T> {
    encoded_versions: Ranges<T>,
    canonical_versions: Option<Box<Ranges<T>>>,
}

impl<T> Range<T> {
    /// Return the canonical set used for identity and set relationships.
    ///
    /// Ranges that do not need canonicalization reuse their encoded representation.
    fn logical_versions(&self) -> &Ranges<T> {
        self.canonical_versions
            .as_deref()
            .unwrap_or(&self.encoded_versions)
    }
}

impl<T: PartialEq> PartialEq for Range<T> {
    fn eq(&self, other: &Self) -> bool {
        self.logical_versions() == other.logical_versions()
    }
}

impl<T: Eq> Eq for Range<T> {}

impl<T: Hash> Hash for Range<T> {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.logical_versions().hash(state);
    }
}

impl<T> Deref for Range<T> {
    type Target = Ranges<T>;

    fn deref(&self) -> &Self::Target {
        &self.encoded_versions
    }
}

impl Range<Version> {
    /// Construct a [`Range`], omitting a redundant canonical representation.
    fn from_parts(
        encoded_versions: Ranges<Version>,
        canonical_versions: Option<Ranges<Version>>,
    ) -> Self {
        let canonical_versions = canonical_versions
            .filter(|canonical| canonical != &encoded_versions)
            .map(Box::new);
        Self {
            encoded_versions,
            canonical_versions,
        }
    }

    /// Construct a [`Range`] whose encoded representation is already canonical.
    fn from_canonical_versions(versions: Ranges<Version>) -> Self {
        Self {
            encoded_versions: versions,
            canonical_versions: None,
        }
    }

    /// Create a range from its internal PEP 440 encoding.
    pub(crate) fn from_versions(versions: Ranges<Version>) -> Self {
        let canonical_versions = canonicalize_version_ranges(&versions);
        Self::from_parts(versions, canonical_versions)
    }

    pub(crate) fn empty() -> Self {
        Self::from_canonical_versions(Ranges::empty())
    }

    pub(crate) fn full() -> Self {
        Self::from_canonical_versions(Ranges::full())
    }

    pub(crate) fn singleton(version: Version) -> Self {
        Self::from_versions(Ranges::singleton(version))
    }

    pub(crate) fn from_range_bounds(range: impl RangeBounds<Version>) -> Self {
        Self::from_versions(Ranges::from_range_bounds(range))
    }

    pub(crate) fn strictly_lower_than(version: Version) -> Self {
        Self::from_versions(Ranges::strictly_lower_than(version))
    }

    pub(crate) fn strictly_higher_than(version: Version) -> Self {
        Self::from_versions(Ranges::strictly_higher_than(version))
    }

    /// Widen this range across gaps in the known versions while preserving canonical identity.
    pub(crate) fn widen_versions(&self, versions: &[Version]) -> Self {
        Self::from_versions(self.encoded_versions.widen_versions(versions))
    }

    /// Narrow this range onto the known versions while preserving canonical identity.
    pub(crate) fn narrow_versions(&self, versions: &[Version]) -> Self {
        Self::from_versions(self.encoded_versions.narrow_versions(versions))
    }

    /// Return the internal PEP 440 bounds used for candidate iteration.
    pub(crate) fn encoded_versions(&self) -> &Ranges<Version> {
        &self.encoded_versions
    }

    /// Return `true` if this range represents a single PEP 440 version constraint.
    ///
    /// A constraint like `==1.0` includes local versions such as `1.0+local`, so its encoded range
    /// is not a singleton even though it should be prioritized like a pinned requirement.
    #[inline]
    pub(crate) fn is_singleton_constraint(&self) -> bool {
        self.encoded_versions.as_singleton().is_some() || self.is_local_version_sentinel()
    }

    /// Return `true` if this range represents one or more exact public-version constraints.
    #[inline]
    fn is_local_version_sentinel(&self) -> bool {
        self.encoded_versions.iter().all(|(lower, upper)| {
            let (Bound::Included(lower), Bound::Excluded(upper)) = (lower, upper) else {
                return false;
            };
            if !lower.local().is_empty() {
                return false;
            }
            if upper.local() != LocalVersionSlice::Max {
                return false;
            }
            *lower == upper.clone().without_local()
        })
    }

    /// Return `true` if this range contains only local versions of the same public version.
    pub(crate) fn is_local_version_complement(&self) -> bool {
        self.encoded_versions.iter().all(|(lower, upper)| {
            let (Bound::Excluded(lower), Bound::Excluded(upper)) = (lower, upper) else {
                return false;
            };
            lower.local().is_empty()
                && upper.local() == LocalVersionSlice::Max
                && *lower == upper.clone().without_local()
        })
    }

    /// Rewrite internal local-version sentinels into bounds suitable for diagnostics.
    pub(crate) fn without_local_version_sentinels(&self) -> Self {
        Self::from_versions(strip_local_version_sentinels(&self.encoded_versions))
    }

    pub(crate) fn complement(&self) -> Self {
        Self::from_parts(
            self.encoded_versions.complement(),
            self.canonical_versions.as_deref().map(Ranges::complement),
        )
    }

    pub(crate) fn intersection(&self, other: &Self) -> Self {
        self.binary_op(other, Ranges::intersection)
    }

    pub(crate) fn union(&self, other: &Self) -> Self {
        self.binary_op(other, Ranges::union)
    }

    /// Apply a set operation to both representations while preserving their equivalence.
    ///
    /// The encoded result retains sentinel bounds for candidate membership and diagnostic
    /// projection, while the logical result keeps subsequent PubGrub operations congruent with
    /// canonical equality.
    fn binary_op(
        &self,
        other: &Self,
        operation: impl Fn(&Ranges<Version>, &Ranges<Version>) -> Ranges<Version>,
    ) -> Self {
        let encoded_versions = operation(&self.encoded_versions, &other.encoded_versions);
        let canonical_versions = (self.canonical_versions.is_some()
            || other.canonical_versions.is_some())
        .then(|| operation(self.logical_versions(), other.logical_versions()));
        Self::from_parts(encoded_versions, canonical_versions)
    }
}

impl From<Ranges<Version>> for Range<Version> {
    fn from(versions: Ranges<Version>) -> Self {
        Self::from_versions(versions)
    }
}

impl FromIterator<(Bound<Version>, Bound<Version>)> for Range<Version> {
    fn from_iter<I: IntoIterator<Item = (Bound<Version>, Bound<Version>)>>(iter: I) -> Self {
        Self::from_versions(iter.into_iter().collect())
    }
}

impl From<VersionSpecifiers> for Range<Version> {
    fn from(specifiers: VersionSpecifiers) -> Self {
        Self::from_versions(Ranges::from(specifiers))
    }
}

impl VersionSet for Range<Version> {
    type V = Version;

    fn empty() -> Self {
        Self::empty()
    }

    fn singleton(version: Self::V) -> Self {
        Self::singleton(version)
    }

    fn complement(&self) -> Self {
        Self::complement(self)
    }

    fn intersection(&self, other: &Self) -> Self {
        Self::intersection(self, other)
    }

    fn contains(&self, version: &Self::V) -> bool {
        self.encoded_versions.contains(version)
    }

    fn full() -> Self {
        Self::full()
    }

    fn union(&self, other: &Self) -> Self {
        Self::union(self, other)
    }

    fn is_disjoint(&self, other: &Self) -> bool {
        self.logical_versions()
            .is_disjoint(other.logical_versions())
    }

    fn subset_of(&self, other: &Self) -> bool {
        self.logical_versions().subset_of(other.logical_versions())
    }

    fn relation(&self, other: &Self) -> SetRelation {
        self.logical_versions().relation(other.logical_versions())
    }
}

impl<T: Debug + Display + Clone + Eq + Ord> Display for Range<T> {
    fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
        Display::fmt(&self.encoded_versions, formatter)
    }
}

#[cfg(test)]
mod tests {
    use std::collections::hash_map::DefaultHasher;
    use std::hash::{Hash, Hasher};
    use std::str::FromStr;

    use pubgrub::{Ranges, VersionSet};

    use super::Range;
    use uv_pep440::{Version, VersionSpecifiers, canonicalize_version_ranges};

    fn range(specifiers: &str) -> Range<Version> {
        Range::from(
            VersionSpecifiers::from_str(specifiers).expect("valid version specifiers for test"),
        )
    }

    fn version(version: &str) -> Version {
        Version::from_str(version).expect("valid version")
    }

    fn hash(range: &Range<Version>) -> u64 {
        let mut hasher = DefaultHasher::new();
        range.hash(&mut hasher);
        hasher.finish()
    }

    #[test]
    fn equivalent_pep440_ranges_have_canonical_identity() {
        for (left, right) in [
            (">1.0a1", ">=1.0a2.dev0"),
            ("<=1.0", "<1.0.post0.dev0"),
            ("==1.0", ">=1.0,<1.0.post0.dev0"),
        ] {
            let left = range(left);
            let right = range(right);

            assert_eq!(left, right);
            assert_eq!(hash(&left), hash(&right));
            assert!(left.subset_of(&right));
            assert!(right.subset_of(&left));
        }
    }

    #[test]
    fn canonical_identity_preserves_original_bounds() {
        let encoded = Ranges::from(
            VersionSpecifiers::from_str("<=1.0").expect("valid version specifiers for test"),
        );
        let range = Range::from(encoded.clone());

        assert_eq!(range.encoded_versions(), &encoded);
        assert_eq!(range.to_string(), encoded.to_string());
        assert_ne!(range.encoded_versions(), range.logical_versions());
    }

    #[test]
    fn diagnostic_ranges_hide_local_version_sentinels() {
        let equality = range("==1.0");
        assert_eq!(equality.encoded_versions().to_string(), ">=1.0, <1.0+");
        assert_eq!(
            equality.without_local_version_sentinels().to_string(),
            "==1.0"
        );

        let upper_bound = range("<=1.0");
        assert_eq!(upper_bound.encoded_versions().to_string(), "<=1.0+");
        assert_eq!(
            upper_bound.without_local_version_sentinels().to_string(),
            "<=1.0"
        );
    }

    #[test]
    fn public_version_equality_is_a_singleton_constraint() {
        assert!(range("==1.0").is_singleton_constraint());
        assert!(Range::singleton(version("1.0+local")).is_singleton_constraint());
        assert!(!range(">=1.0").is_singleton_constraint());
    }

    #[test]
    fn range_algebra_preserves_canonical_identity() {
        let ranges = [
            "==0.dev0",
            "!=0.dev0",
            "<=1.0",
            "<1.0.post0.dev0",
            "<1.0",
            "<1.0.dev0",
            ">1.0a1",
            ">=1.0a2.dev0",
            "==1.0",
            ">=2.0b1,<3",
            ">=3.5,<4",
        ]
        .map(range);

        for left in &ranges {
            assert_matches_recanonicalized(&left.complement());
            for right in &ranges {
                assert_matches_recanonicalized(&left.intersection(right));
                assert_matches_recanonicalized(&left.union(right));
            }
        }
    }

    #[test]
    fn known_version_projection_preserves_canonical_identity() {
        let versions = [version("0.dev0"), version("1.0"), version("2.0")];
        let encoded = range("<=1.0");
        let canonical = range("<1.0.post0.dev0");

        assert_eq!(
            encoded.widen_versions(&versions),
            canonical.widen_versions(&versions)
        );
        assert_eq!(
            encoded.narrow_versions(&versions),
            canonical.narrow_versions(&versions)
        );

        let empty = range("<0.dev0");
        assert_eq!(empty.widen_versions(&versions), Range::empty());
        assert_eq!(empty.narrow_versions(&versions), Range::empty());
    }

    fn assert_matches_recanonicalized(range: &Range<Version>) {
        let canonical_versions = canonicalize_version_ranges(&range.encoded_versions)
            .unwrap_or_else(|| range.encoded_versions.clone());
        assert_eq!(range.logical_versions(), &canonical_versions);
    }

    #[test]
    fn pep440_floor_is_not_logically_empty() {
        let floor = version("0.dev0");
        let range = range("==0.dev0");

        assert!(range.contains(&floor));
        assert!(!range.is_disjoint(&Range::singleton(floor)));
        assert_ne!(range, Range::empty());
    }
}