Skip to content

Commit

Permalink
Revert back to single iter
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Sep 30, 2024
1 parent 2f63d75 commit 3b21e91
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 23 deletions.
19 changes: 8 additions & 11 deletions crates/uv-distribution/src/metadata/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,7 @@ impl LoweredRequirement {
project_dir: &'data Path,
project_sources: &'data BTreeMap<PackageName, Sources>,
workspace: &'data Workspace,
) -> Result<
impl Iterator<Item = Result<LoweredRequirement, LoweringError>> + 'data,
LoweringError,
> {
) -> impl Iterator<Item = Result<LoweredRequirement, LoweringError>> + 'data {
let (source, origin) = if let Some(source) = project_sources.get(&requirement.name) {
(Some(source), Origin::Project)
} else if let Some(source) = workspace.sources().get(&requirement.name) {
Expand All @@ -51,14 +48,16 @@ impl LoweredRequirement {
// We require that when you use a package that's part of the workspace, ...
!workspace.packages().contains_key(&requirement.name)
// ... it must be declared as a workspace dependency (`workspace = true`), ...
|| source.as_ref().is_some_and(|source| source.iter().all(|source| {
|| source.as_ref().filter(|sources| !sources.is_empty()).is_some_and(|source| source.iter().all(|source| {
matches!(source, Source::Workspace { workspace: true, .. })
}))
// ... except for recursive self-inclusion (extras that activate other extras), e.g.
// `framework[machine_learning]` depends on `framework[cuda]`.
|| &requirement.name == project_name;
if !workspace_package_declared {
return Err(LoweringError::UndeclaredWorkspacePackage);
return Either::Left(std::iter::once(Err(
LoweringError::UndeclaredWorkspacePackage,
)));
}

let Some(source) = source else {
Expand All @@ -73,9 +72,7 @@ impl LoweredRequirement {
requirement.name
);
}
return Ok(Either::Left(std::iter::once(Ok(Self(Requirement::from(
requirement,
))))));
return Either::Left(std::iter::once(Ok(Self(Requirement::from(requirement)))));
};

// Determine whether the markers cover the full space for the requirement. If not, fill the
Expand All @@ -97,7 +94,7 @@ impl LoweredRequirement {
})
};

Ok(Either::Right(
Either::Right(
source
.into_iter()
.map(move |source| {
Expand Down Expand Up @@ -233,7 +230,7 @@ impl LoweredRequirement {
Ok(requirement) => !requirement.0.marker.is_false(),
Err(_) => true,
}),
))
)
}

/// Lower a [`pep508_rs::Requirement`] in a non-workspace setting (for example, in a PEP 723
Expand Down
2 changes: 2 additions & 0 deletions crates/uv-distribution/src/metadata/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ pub enum MetadataError {
Workspace(#[from] WorkspaceError),
#[error("Failed to parse entry for: `{0}`")]
LoweringError(PackageName, #[source] LoweringError),
#[error(transparent)]
Lower(#[from] LoweringError),
}

#[derive(Debug, Clone)]
Expand Down
15 changes: 7 additions & 8 deletions crates/uv-distribution/src/metadata/requires_dist.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::metadata::{LoweredRequirement, MetadataError};
use crate::Metadata;
use pypi_types::Requirement;

use std::collections::BTreeMap;
use std::path::Path;
use uv_configuration::SourceStrategy;
Expand Down Expand Up @@ -93,8 +93,6 @@ impl RequiresDist {
sources,
project_workspace.workspace(),
)
.into_iter()
.flatten()
.map(move |requirement| match requirement {
Ok(requirement) => Ok(requirement.into_inner()),
Err(err) => {
Expand All @@ -105,7 +103,7 @@ impl RequiresDist {
.collect::<Result<Vec<_>, _>>()?,
SourceStrategy::Disabled => dev_dependencies
.into_iter()
.map(Requirement::from)
.map(pypi_types::Requirement::from)
.collect(),
};
if dev_dependencies.is_empty() {
Expand All @@ -127,17 +125,18 @@ impl RequiresDist {
sources,
project_workspace.workspace(),
)
.into_iter()
.flatten()
.map(move |requirement| match requirement {
Ok(requirement) => Ok(requirement.into_inner()),
Err(err) => {
Err(MetadataError::LoweringError(requirement_name.clone(), err))
}
})
})
.collect::<Result<_, _>>()?,
SourceStrategy::Disabled => requires_dist.into_iter().map(Requirement::from).collect(),
.collect::<Result<Vec<_>, _>>()?,
SourceStrategy::Disabled => requires_dist
.into_iter()
.map(pypi_types::Requirement::from)
.collect(),
};

Ok(Self {
Expand Down
10 changes: 10 additions & 0 deletions crates/uv-workspace/src/pyproject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,16 @@ impl Sources {
pub fn iter(&self) -> impl Iterator<Item = &Source> {
self.0.iter()
}

/// Returns `true` if the sources list is empty.
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}

/// Returns the number of sources in the list.
pub fn len(&self) -> usize {
self.0.len()
}
}

impl IntoIterator for Sources {
Expand Down
6 changes: 2 additions & 4 deletions crates/uv-workspace/src/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1757,8 +1757,7 @@ mod tests {
"sources": {
"bird-feeder": [
{
"workspace": true,
"marker": null
"workspace": true
}
]
},
Expand All @@ -1778,8 +1777,7 @@ mod tests {
"sources": {
"bird-feeder": [
{
"workspace": true,
"marker": null
"workspace": true
}
]
},
Expand Down

0 comments on commit 3b21e91

Please sign in to comment.