Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement "Requires" field in pip show #2347

Merged
merged 10 commits into from
Mar 12, 2024
17 changes: 17 additions & 0 deletions crates/uv/src/commands/pip_show.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::collections::BTreeSet;
use std::fmt::Write;

use anyhow::Result;
Expand Down Expand Up @@ -62,6 +63,9 @@ pub(crate) fn pip_show(
// Build the installed index.
let site_packages = SitePackages::from_executable(&venv)?;

// Determine the markers to use for resolution.
let markers = venv.interpreter().markers();

// Sort and deduplicate the packages, which are keyed by name.
packages.sort_unstable();
packages.dedup();
Expand Down Expand Up @@ -116,6 +120,19 @@ pub(crate) fn pip_show(
.expect("package path is not root")
.simplified_display()
)?;

let mut requires = distribution
.metadata()
.unwrap()
.requires_dist
.into_iter()
.filter(|req| req.evaluate_markers(markers, &[]))
.map(|req| req.name.to_string())
.collect::<BTreeSet<_>>()
.into_iter()
.collect::<Vec<_>>();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you try collecting into a BTreeSet instead? There are some cases where names occur twice in the requirements. This would remove those duplicates and skip the sorting step

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you happen to remember which particular package has duplicates (was thinking if I should add a test using it)? https://github.com/pypa/pip/blob/fb5f63f0f4c3a204ada8353ce9858d4d4b777c2a/src/pip/_internal/commands/show.py#L104 does seem to suggest the same.

requires.sort();
writeln!(printer.stdout(), "Requires: {}", requires.join(", "))?;
}

// Validate that the environment is consistent.
Expand Down
123 changes: 123 additions & 0 deletions crates/uv/tests/pip_show.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,124 @@ fn show_empty() {
);
}

#[test]
fn show_requires_multiple() -> Result<()> {
let context = TestContext::new("3.12");

let requirements_txt = context.temp_dir.child("requirements.txt");
requirements_txt.touch()?;
requirements_txt.write_str("requests==2.31.0")?;

uv_snapshot!(install_command(&context)
.arg("-r")
.arg("requirements.txt")
.arg("--strict"), @r###"
success: true
exit_code: 0
----- stdout -----

----- stderr -----
Resolved 5 packages in [TIME]
Downloaded 5 packages in [TIME]
Installed 5 packages in [TIME]
+ certifi==2023.11.17
+ charset-normalizer==3.3.2
+ idna==3.4
+ requests==2.31.0
+ urllib3==2.1.0
"###
);

context.assert_command("import requests").success();
let filters = [(
r"Location:.*site-packages",
"Location: [WORKSPACE_DIR]/site-packages",
)]
.to_vec();

// Guards against the package names being sorted.
uv_snapshot!(filters, Command::new(get_bin())
.arg("pip")
.arg("show")
.arg("requests")
.arg("--cache-dir")
.arg(context.cache_dir.path())
.env("VIRTUAL_ENV", context.venv.as_os_str())
.current_dir(&context.temp_dir), @r###"
success: true
exit_code: 0
----- stdout -----
Name: requests
Version: 2.31.0
Location: [WORKSPACE_DIR]/site-packages
Requires: certifi, charset-normalizer, idna, urllib3

----- stderr -----
"###
);

Ok(())
}

// Asserts python version marker in the metadata is correctly evaluated.
// click 8.1.7 requires importlib-metadata but only when python_version < "3.8"
#[test]
fn show_python_version_marker() -> Result<()> {
let context = TestContext::new("3.12");

let requirements_txt = context.temp_dir.child("requirements.txt");
requirements_txt.touch()?;
requirements_txt.write_str("click==8.1.7")?;

uv_snapshot!(install_command(&context)
.arg("-r")
.arg("requirements.txt")
.arg("--strict"), @r###"
success: true
exit_code: 0
----- stdout -----

----- stderr -----
Resolved 1 package in [TIME]
Downloaded 1 package in [TIME]
Installed 1 package in [TIME]
+ click==8.1.7
"###
);

context.assert_command("import click").success();
let mut filters = [(
r"Location:.*site-packages",
"Location: [WORKSPACE_DIR]/site-packages",
)]
.to_vec();
if cfg!(windows) {
filters.push(("Requires: colorama", "Requires: "));
}

uv_snapshot!(filters, Command::new(get_bin())
.arg("pip")
.arg("show")
.arg("click")
.arg("--cache-dir")
.arg(context.cache_dir.path())
.env("VIRTUAL_ENV", context.venv.as_os_str())
.current_dir(&context.temp_dir), @r###"
success: true
exit_code: 0
----- stdout -----
Name: click
Version: 8.1.7
Location: [WORKSPACE_DIR]/site-packages
Requires:

----- stderr -----
"###
);

Ok(())
}

#[test]
fn show_found_single_package() -> Result<()> {
let context = TestContext::new("3.12");
Expand Down Expand Up @@ -101,6 +219,7 @@ fn show_found_single_package() -> Result<()> {
Name: markupsafe
Version: 2.1.3
Location: [WORKSPACE_DIR]/site-packages
Requires:

----- stderr -----
"###
Expand Down Expand Up @@ -162,10 +281,12 @@ fn show_found_multiple_packages() -> Result<()> {
Name: markupsafe
Version: 2.1.3
Location: [WORKSPACE_DIR]/site-packages
Requires:
---
Name: pip
Version: 21.3.1
Location: [WORKSPACE_DIR]/site-packages
Requires:

----- stderr -----
"###
Expand Down Expand Up @@ -227,6 +348,7 @@ fn show_found_one_out_of_two() -> Result<()> {
Name: markupsafe
Version: 2.1.3
Location: [WORKSPACE_DIR]/site-packages
Requires:

----- stderr -----
warning: Package(s) not found for: flask
Expand Down Expand Up @@ -378,6 +500,7 @@ fn show_editable() -> Result<()> {
Name: poetry-editable
Version: 0.1.0
Location: [WORKSPACE_DIR]/site-packages
Requires: numpy

----- stderr -----
"###
Expand Down