Avoid unused extras check in pip install for source trees (#2811)
## 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 https://github.com/astral-sh/uv/issues/2801.
This commit is contained in:
@@ -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.
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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"]
|
||||
@@ -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]
|
||||
"###
|
||||
);
|
||||
|
||||
|
||||
@@ -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<()> {
|
||||
|
||||
Reference in New Issue
Block a user