Skip to content

Commit

Permalink
Avoid unused extras check in pip install for source trees (#2811)
Browse files Browse the repository at this point in the history
## Summary

This was an oversight in the `-r pyproject.toml` refactor. We can't
enforce unused extras if we have a source tree. We made the correct
changes to `pip compile`, but not `pip install`. This PR just mirrors
those changes to `pip install`, and adds a few tests.

Closes #2801.
  • Loading branch information
charliermarsh authored Apr 3, 2024
1 parent 691ed09 commit 8eaaf65
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 25 deletions.
47 changes: 25 additions & 22 deletions crates/uv/src/commands/pip_install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -385,13 +385,13 @@ async fn read_requirements(
extras: &ExtrasSpecification<'_>,
client_builder: &BaseClientBuilder<'_>,
) -> Result<RequirementsSpecification, Error> {
// If the user requests `extras` but does not provide a pyproject toml source
if !matches!(extras, ExtrasSpecification::None)
&& !requirements
.iter()
.any(|source| matches!(source, RequirementsSource::PyprojectToml(_)))
{
return Err(anyhow!("Requesting extras requires a pyproject.toml input file.").into());
// If the user requests `extras` but does not provide a valid source (e.g., a `pyproject.toml`),
// return an error.
if !extras.is_empty() && !requirements.iter().any(RequirementsSource::allows_extras) {
return Err(anyhow!(
"Requesting extras requires a `pyproject.toml`, `setup.cfg`, or `setup.py` file."
)
.into());
}

// Read all requirements from the provided sources.
Expand All @@ -404,21 +404,24 @@ async fn read_requirements(
)
.await?;

// Check that all provided extras are used
if let ExtrasSpecification::Some(extras) = extras {
let mut unused_extras = extras
.iter()
.filter(|extra| !spec.extras.contains(extra))
.collect::<Vec<_>>();
if !unused_extras.is_empty() {
unused_extras.sort_unstable();
unused_extras.dedup();
let s = if unused_extras.len() == 1 { "" } else { "s" };
return Err(anyhow!(
"Requested extra{s} not found: {}",
unused_extras.iter().join(", ")
)
.into());
// If all the metadata could be statically resolved, validate that every extra was used. If we
// need to resolve metadata via PEP 517, we don't know which extras are used until much later.
if spec.source_trees.is_empty() {
if let ExtrasSpecification::Some(extras) = extras {
let mut unused_extras = extras
.iter()
.filter(|extra| !spec.extras.contains(extra))
.collect::<Vec<_>>();
if !unused_extras.is_empty() {
unused_extras.sort_unstable();
unused_extras.dedup();
let s = if unused_extras.len() == 1 { "" } else { "s" };
return Err(anyhow!(
"Requested extra{s} not found: {}",
unused_extras.iter().join(", ")
)
.into());
}
}
}

Expand Down
1 change: 1 addition & 0 deletions crates/uv/src/commands/pip_sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ pub(crate) async fn pip_sync(
printer: Printer,
) -> Result<ExitStatus> {
let start = std::time::Instant::now();

let client_builder = BaseClientBuilder::new()
.connectivity(connectivity)
.native_tls(native_tls)
Expand Down
19 changes: 16 additions & 3 deletions crates/uv/tests/pip_compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,10 @@ authors = ["Astral Software Inc. <hey@astral.sh>"]
[tool.poetry.dependencies]
python = "^3.10"
anyio = "^3"
pytest = { version = "*", optional = true }
[tool.poetry.extras]
test = ["pytest"]
[build-system]
requires = ["poetry-core"]
Expand All @@ -427,20 +431,29 @@ build-backend = "poetry.core.masonry.api"
)?;

uv_snapshot!(context.compile()
.arg("pyproject.toml"), @r###"
.arg("pyproject.toml")
.arg("--extra")
.arg("test"), @r###"
success: true
exit_code: 0
----- stdout -----
# This file was autogenerated by uv via the following command:
# uv pip compile --cache-dir [CACHE_DIR] --exclude-newer 2024-03-25T00:00:00Z pyproject.toml
# uv pip compile --cache-dir [CACHE_DIR] --exclude-newer 2024-03-25T00:00:00Z pyproject.toml --extra test
anyio==3.7.1
idna==3.6
# via anyio
iniconfig==2.0.0
# via pytest
packaging==24.0
# via pytest
pluggy==1.4.0
# via pytest
pytest==8.1.1
sniffio==1.3.1
# via anyio
----- stderr -----
Resolved 3 packages in [TIME]
Resolved 7 packages in [TIME]
"###
);

Expand Down
49 changes: 49 additions & 0 deletions crates/uv/tests/pip_install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,55 @@ fn install_requirements_txt() -> Result<()> {
Ok(())
}

/// Install a `pyproject.toml` file with a `poetry` section.
#[test]
fn install_pyproject_toml_poetry() -> Result<()> {
let context = TestContext::new("3.12");
let pyproject_toml = context.temp_dir.child("pyproject.toml");
pyproject_toml.write_str(
r#"[tool.poetry]
name = "poetry-editable"
version = "0.1.0"
description = ""
authors = ["Astral Software Inc. <hey@astral.sh>"]
[tool.poetry.dependencies]
python = "^3.10"
anyio = "^3"
iniconfig = { version = "*", optional = true }
[tool.poetry.extras]
test = ["iniconfig"]
[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
"#,
)?;

uv_snapshot!(context.install()
.arg("-r")
.arg("pyproject.toml")
.arg("--extra")
.arg("test"), @r###"
success: true
exit_code: 0
----- stdout -----
----- stderr -----
Resolved 4 packages in [TIME]
Downloaded 4 packages in [TIME]
Installed 4 packages in [TIME]
+ anyio==3.7.1
+ idna==3.6
+ iniconfig==2.0.0
+ sniffio==1.3.1
"###
);

Ok(())
}

/// Respect installed versions when resolving.
#[test]
fn respect_installed_and_reinstall() -> Result<()> {
Expand Down

0 comments on commit 8eaaf65

Please sign in to comment.