Skip to content

Commit 7467a0d

Browse files
authored
Unify override and exclude code (#20689)
We always apply overrides and excludes together, there's no need to always pass around two fields and apply both in sequence. Being unified into one type, we can attach future dependency modifiers to that too.
1 parent 6cf775b commit 7467a0d

21 files changed

Lines changed: 362 additions & 357 deletions

File tree

‎crates/uv-configuration/src/constraints.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ impl Constraints {
8383

8484
/// Apply the constraints to a set of requirements.
8585
///
86-
/// NB: Change this method together with [`Overrides::apply`].
86+
/// NB: Change this method together with `Overrides::apply_for_package`.
8787
pub fn apply<'a>(
8888
&'a self,
8989
requirements: impl IntoIterator<Item = Cow<'a, Requirement>>,
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
use std::borrow::Cow;
2+
3+
use uv_distribution_types::{Requirement, ResolutionRecorder};
4+
use uv_normalize::PackageName;
5+
use uv_pep440::Version;
6+
7+
use crate::{Excludes, Overrides};
8+
9+
/// Overrides and exclusions to apply to dependency requirements.
10+
#[derive(Debug, Default, Clone)]
11+
pub struct DependencyModifiers {
12+
overrides: Overrides,
13+
excludes: Excludes,
14+
}
15+
16+
/// The package scope in which to apply dependency modifiers.
17+
#[derive(Debug, Clone, Copy)]
18+
pub enum DependencyModifierScope<'a> {
19+
/// Apply global overrides and exclusions.
20+
Global,
21+
/// Apply global and package-scoped overrides and exclusions to regular package metadata.
22+
Package(&'a PackageName, &'a Version),
23+
/// Apply global modifiers and package-scoped exclusions to a dependency group.
24+
DependencyGroup(&'a PackageName, &'a Version),
25+
}
26+
27+
impl DependencyModifiers {
28+
/// Record which settings are consulted while resolving runtime dependencies.
29+
#[must_use]
30+
pub fn with_recorder(mut self, recorder: Option<ResolutionRecorder>) -> Self {
31+
self.overrides = self.overrides.with_recorder(recorder.clone());
32+
self.excludes = self.excludes.with_recorder(recorder);
33+
self
34+
}
35+
36+
pub fn new(overrides: Overrides, excludes: Excludes) -> Self {
37+
Self {
38+
overrides,
39+
excludes,
40+
}
41+
}
42+
43+
/// Return whether any modifiers are scoped to this package.
44+
pub fn has_scoped_package(&self, package: &PackageName) -> bool {
45+
self.overrides.has_scoped_package(package) || self.excludes.has_scoped_package(package)
46+
}
47+
48+
/// Return all global override requirements that are not excluded.
49+
pub fn global_overrides(&self) -> impl Iterator<Item = &Requirement> {
50+
self.overrides
51+
.global_requirements()
52+
.filter(|requirement| !self.excludes.contains(&requirement.name))
53+
}
54+
55+
/// Return all scoped override requirements that are not excluded in their scope.
56+
pub fn scoped_overrides(&self) -> impl Iterator<Item = &Requirement> {
57+
self.overrides
58+
.scoped_requirements()
59+
.filter(|(package, version, requirement)| {
60+
!self.excludes.contains_for_scope(
61+
&self.overrides,
62+
package,
63+
*version,
64+
&requirement.name,
65+
)
66+
})
67+
.map(|(_, _, requirement)| requirement)
68+
}
69+
70+
/// Return the scoped override requirements that apply to this package version.
71+
pub fn scoped_overrides_for(
72+
&self,
73+
package: &PackageName,
74+
version: &Version,
75+
) -> impl Iterator<Item = &Requirement> {
76+
self.overrides
77+
.scoped_requirements_for(package, version)
78+
.filter(|requirement| {
79+
!self
80+
.excludes
81+
.contains_for(package, version, &requirement.name)
82+
})
83+
}
84+
85+
/// Return whether a dependency is globally excluded.
86+
pub fn is_excluded(&self, dependency: &PackageName) -> bool {
87+
self.excludes.contains(dependency)
88+
}
89+
90+
/// Return whether a dependency is excluded from a specific package version.
91+
pub fn is_excluded_for(
92+
&self,
93+
package: &PackageName,
94+
version: &Version,
95+
dependency: &PackageName,
96+
) -> bool {
97+
self.excludes.contains_for(package, version, dependency)
98+
}
99+
100+
/// Apply dependency overrides and exclusions in the given scope.
101+
pub fn apply<'a, 'scope, I>(
102+
&'a self,
103+
scope: DependencyModifierScope<'scope>,
104+
requirements: I,
105+
) -> impl Iterator<Item = Cow<'a, Requirement>> + use<'a, 'scope, I>
106+
where
107+
I: IntoIterator<Item = &'a Requirement>,
108+
{
109+
let (overrides, excludes) = match scope {
110+
DependencyModifierScope::Global => (None, None),
111+
DependencyModifierScope::Package(package, version) => {
112+
(Some((package, version)), Some((package, version)))
113+
}
114+
DependencyModifierScope::DependencyGroup(package, version) => {
115+
(None, Some((package, version)))
116+
}
117+
};
118+
self.overrides
119+
.apply_for_package(overrides, requirements)
120+
.filter(move |requirement| {
121+
!self
122+
.excludes
123+
.contains_for_package(excludes, &requirement.name)
124+
})
125+
}
126+
}

‎crates/uv-configuration/src/excludes.rs‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ struct ScopedExclusions {
8383
impl Excludes {
8484
/// Record which settings are consulted while resolving runtime dependencies.
8585
#[must_use]
86-
pub fn with_recorder(mut self, recorder: Option<ResolutionRecorder>) -> Self {
86+
pub(crate) fn with_recorder(mut self, recorder: Option<ResolutionRecorder>) -> Self {
8787
self.recorder = recorder;
8888
self
8989
}
@@ -142,20 +142,20 @@ impl Excludes {
142142
}
143143

144144
/// Check if a package is excluded.
145-
pub fn contains(&self, name: &PackageName) -> bool {
145+
pub(crate) fn contains(&self, name: &PackageName) -> bool {
146146
if let Some(recorder) = &self.recorder {
147147
recorder.exclusion(name);
148148
}
149149
self.global.contains(name)
150150
}
151151

152152
/// Return whether any exclusions are scoped to the given package.
153-
pub fn has_scoped_package(&self, package: &PackageName) -> bool {
153+
pub(crate) fn has_scoped_package(&self, package: &PackageName) -> bool {
154154
self.scoped.contains_key(package)
155155
}
156156

157157
/// Check if a dependency is excluded from a specific package version.
158-
pub fn contains_for(
158+
pub(crate) fn contains_for(
159159
&self,
160160
package: &PackageName,
161161
version: &Version,
@@ -168,7 +168,7 @@ impl Excludes {
168168
///
169169
/// A versionless scope remains eligible if any exact-version exclusion allows the dependency
170170
/// at a version where the override is not shadowed by an exact override scope.
171-
pub fn contains_for_scope(
171+
pub(crate) fn contains_for_scope(
172172
&self,
173173
overrides: &Overrides,
174174
package: &PackageName,
@@ -201,7 +201,7 @@ impl Excludes {
201201
}
202202

203203
/// Check if a dependency is excluded with optional package-version context.
204-
pub fn contains_for_package(
204+
pub(crate) fn contains_for_package(
205205
&self,
206206
package: Option<(&PackageName, &Version)>,
207207
dependency: &PackageName,

‎crates/uv-configuration/src/lib.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ pub use concurrency::*;
66
pub use constraints::*;
77
pub use dependency_groups::*;
88
pub use dependency_mode::*;
9+
pub use dependency_modifier::*;
910
pub use dry_run::*;
1011
pub use editable::*;
1112
pub use env_file::*;
@@ -40,6 +41,7 @@ mod concurrency;
4041
mod constraints;
4142
mod dependency_groups;
4243
mod dependency_mode;
44+
mod dependency_modifier;
4345
mod dry_run;
4446
mod editable;
4547
mod env_file;

‎crates/uv-configuration/src/normalized_requirements.rs‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,9 @@ mod tests {
552552
use uv_pep508::{MarkerTree, Requirement as Pep508Requirement};
553553
use uv_pypi_types::VerbatimParsedUrl;
554554

555-
use crate::{ExcludeDependency, Excludes, Overrides};
555+
use crate::{
556+
DependencyModifierScope, DependencyModifiers, ExcludeDependency, Excludes, Overrides,
557+
};
556558

557559
use super::{
558560
NormalizedExcludes, NormalizedRequirements, allows_prereleases, allows_yanked,
@@ -575,10 +577,13 @@ mod tests {
575577
fn overridden_optional_requirements() -> Result<()> {
576578
let original = requirements(&["a; extra == 'x'", "a; extra == 'y'"])?;
577579
let normalized = NormalizedRequirements::from(original.clone());
578-
let overrides = Overrides::from_requirements(requirements(&["a>=2"])?);
580+
let modifiers = DependencyModifiers::new(
581+
Overrides::from_requirements(requirements(&["a>=2"])?),
582+
Excludes::default(),
583+
);
579584
let overridden = |requirements: &[Requirement]| {
580-
overrides
581-
.apply(requirements)
585+
modifiers
586+
.apply(DependencyModifierScope::Global, requirements)
582587
.fold(MarkerTree::FALSE, |marker, requirement| {
583588
marker.or(requirement.marker)
584589
})

‎crates/uv-configuration/src/overrides.rs‎

Lines changed: 7 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ pub enum ScopedOverrideSourceError {
123123
impl Overrides {
124124
/// Record which settings are consulted while resolving runtime dependencies.
125125
#[must_use]
126-
pub fn with_recorder(mut self, recorder: Option<ResolutionRecorder>) -> Self {
126+
pub(crate) fn with_recorder(mut self, recorder: Option<ResolutionRecorder>) -> Self {
127127
self.recorder = recorder;
128128
self
129129
}
@@ -214,14 +214,14 @@ impl Overrides {
214214
}
215215

216216
/// Return an iterator over all global [`Requirement`]s in the override set.
217-
pub fn global_requirements(&self) -> impl Iterator<Item = &Requirement> {
217+
pub(crate) fn global_requirements(&self) -> impl Iterator<Item = &Requirement> {
218218
self.global
219219
.values()
220220
.flat_map(|requirements| requirements.iter())
221221
}
222222

223223
/// Return all scoped [`Requirement`]s with the package and version they apply to.
224-
pub fn scoped_requirements(
224+
pub(crate) fn scoped_requirements(
225225
&self,
226226
) -> impl Iterator<Item = (&PackageName, Option<&Version>, &Requirement)> {
227227
self.scoped.iter().flat_map(|(package, entries)| {
@@ -236,7 +236,7 @@ impl Overrides {
236236
}
237237

238238
/// Return the scoped [`Requirement`]s that apply to a specific package version.
239-
pub fn scoped_requirements_for(
239+
pub(crate) fn scoped_requirements_for(
240240
&self,
241241
package: &PackageName,
242242
version: &Version,
@@ -247,7 +247,7 @@ impl Overrides {
247247
}
248248

249249
/// Return whether any overrides are scoped to the given package.
250-
pub fn has_scoped_package(&self, package: &PackageName) -> bool {
250+
pub(crate) fn has_scoped_package(&self, package: &PackageName) -> bool {
251251
self.scoped.contains_key(package)
252252
}
253253

@@ -281,49 +281,14 @@ impl Overrides {
281281
})
282282
}
283283

284-
/// Apply the overrides to a set of requirements.
284+
/// Apply overrides with optional package-version context.
285285
///
286286
/// NB: Change this method together with [`Constraints::apply`].
287-
pub fn apply<'a, I>(
288-
&'a self,
289-
requirements: I,
290-
) -> impl Iterator<Item = Cow<'a, Requirement>> + use<'a, I>
291-
where
292-
I: IntoIterator<Item = &'a Requirement>,
293-
{
294-
self.apply_inner(requirements, None)
295-
}
296-
297-
/// Apply the overrides to the dependencies of a specific package version.
298-
pub fn apply_for<'a, I>(
299-
&'a self,
300-
package: &PackageName,
301-
version: &Version,
302-
requirements: I,
303-
) -> impl Iterator<Item = Cow<'a, Requirement>> + use<'a, I>
304-
where
305-
I: IntoIterator<Item = &'a Requirement>,
306-
{
307-
self.apply_inner(requirements, Some((package, version)))
308-
}
309-
310-
/// Apply overrides with optional package-version context.
311-
pub fn apply_for_package<'a, I>(
287+
pub(crate) fn apply_for_package<'a, I>(
312288
&'a self,
313289
package: Option<(&PackageName, &Version)>,
314290
requirements: I,
315291
) -> impl Iterator<Item = Cow<'a, Requirement>> + use<'a, I>
316-
where
317-
I: IntoIterator<Item = &'a Requirement>,
318-
{
319-
self.apply_inner(requirements, package)
320-
}
321-
322-
fn apply_inner<'a, I>(
323-
&'a self,
324-
requirements: I,
325-
package: Option<(&PackageName, &Version)>,
326-
) -> impl Iterator<Item = Cow<'a, Requirement>> + use<'a, I>
327292
where
328293
I: IntoIterator<Item = &'a Requirement>,
329294
{

‎crates/uv-dispatch/src/lib.rs‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ use uv_build_frontend::{SourceBuild, SourceBuildContext};
1818
use uv_cache::Cache;
1919
use uv_client::RegistryClient;
2020
use uv_configuration::{
21-
BuildKind, BuildOptions, Constraints, IndexStrategy, NoSources, Overrides, Reinstall,
21+
BuildKind, BuildOptions, Constraints, DependencyModifiers, IndexStrategy, NoSources, Reinstall,
2222
};
23-
use uv_configuration::{BuildOutput, Concurrency, Excludes};
23+
use uv_configuration::{BuildOutput, Concurrency};
2424
use uv_distribution::DistributionDatabase;
2525
use uv_distribution_filename::DistFilename;
2626
use uv_distribution_types::{
@@ -328,13 +328,11 @@ impl BuildContext for BuildDispatch<'_> {
328328
.clone()
329329
.augment_with_requirements(requirements.iter())
330330
.map_err(uv_requirements::Error::from)?;
331-
let overrides = Overrides::default();
332-
let excludes = Excludes::default();
331+
let modifiers = DependencyModifiers::default();
333332
let (lookaheads, hasher) = LookaheadResolver::new(
334333
requirements,
335334
self.constraints,
336-
&overrides,
337-
&excludes,
335+
&modifiers,
338336
&hasher,
339337
&self.shared_state.index,
340338
DistributionDatabase::new(

0 commit comments

Comments
 (0)