Skip to content

Commit

Permalink
Omit origin when comparing requirements (#9570)
Browse files Browse the repository at this point in the history
## Summary

I noticed that we consider two requirements to be different if they come
from different files. This seems like an oversight.
  • Loading branch information
charliermarsh authored Dec 2, 2024
1 parent 02c105c commit 0e6b2d9
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
45 changes: 42 additions & 3 deletions crates/uv-pypi-types/src/requirement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ pub enum RequirementError {
///
/// The main change is using [`RequirementSource`] to represent all supported package sources over
/// [`VersionOrUrl`], which collapses all URL sources into a single stringly type.
#[derive(
Hash, Debug, Clone, Eq, PartialEq, Ord, PartialOrd, serde::Serialize, serde::Deserialize,
)]
#[derive(Debug, Clone, Ord, PartialOrd, serde::Serialize, serde::Deserialize)]
pub struct Requirement {
pub name: PackageName,
#[serde(skip_serializing_if = "Vec::is_empty", default)]
Expand Down Expand Up @@ -97,6 +95,47 @@ impl Requirement {
}
}

impl std::hash::Hash for Requirement {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
let Self {
name,
extras,
marker,
source,
origin: _,
} = self;
name.hash(state);
extras.hash(state);
marker.hash(state);
source.hash(state);
}
}

impl PartialEq for Requirement {
fn eq(&self, other: &Self) -> bool {
let Self {
name,
extras,
marker,
source,
origin: _,
} = self;
let Self {
name: other_name,
extras: other_extras,
marker: other_marker,
source: other_source,
origin: _,
} = other;
name == other_name
&& extras == other_extras
&& marker == other_marker
&& source == other_source
}
}

impl Eq for Requirement {}

impl From<Requirement> for uv_pep508::Requirement<VerbatimUrl> {
/// Convert a [`Requirement`] to a [`uv_pep508::Requirement`].
fn from(requirement: Requirement) -> Self {
Expand Down
2 changes: 1 addition & 1 deletion crates/uv/tests/it/tool_install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1974,7 +1974,7 @@ fn tool_install_requirements_txt_arguments() {
----- stdout -----
----- stderr -----
Installed 2 executables: black, blackd
`black` is already installed
"###);

let requirements_txt = context.temp_dir.child("requirements.txt");
Expand Down

0 comments on commit 0e6b2d9

Please sign in to comment.