Skip to content

Commit

Permalink
Pin named indexes
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Sep 28, 2024
1 parent 1cfcecb commit ead24e0
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 5 deletions.
12 changes: 11 additions & 1 deletion crates/uv-workspace/src/pyproject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,7 @@ impl Source {
source: RequirementSource,
workspace: bool,
editable: Option<bool>,
index: Option<String>,
rev: Option<String>,
tag: Option<String>,
branch: Option<String>,
Expand Down Expand Up @@ -685,7 +686,16 @@ impl Source {
}

let source = match source {
RequirementSource::Registry { .. } => return Ok(None),
RequirementSource::Registry { index: Some(_), .. } => {
return Ok(None);
}
RequirementSource::Registry { index: None, .. } => {
if let Some(index) = index {
Source::Registry { index }
} else {
return Ok(None);
}
}
RequirementSource::Path { install_path, .. }
| RequirementSource::Directory { install_path, .. } => Source::Path {
editable,
Expand Down
17 changes: 16 additions & 1 deletion crates/uv/src/commands/project/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,13 @@ pub(crate) async fn add(
}
}

// If the user provides a single, named index, pin all requirements to that index.
let index = indexes
.first()
.as_ref()
.and_then(|index| index.name.as_ref())
.filter(|_| indexes.len() == 1);

// Add the requirements to the `pyproject.toml` or script.
let mut toml = match &target {
Target::Script(script, _) => {
Expand Down Expand Up @@ -395,6 +402,7 @@ pub(crate) async fn add(
requirement,
false,
editable,
index.cloned(),
rev.clone(),
tag.clone(),
branch.clone(),
Expand All @@ -410,6 +418,7 @@ pub(crate) async fn add(
requirement,
workspace,
editable,
index.cloned(),
rev.clone(),
tag.clone(),
branch.clone(),
Expand Down Expand Up @@ -678,7 +687,11 @@ async fn lock_and_sync(
};

// Only set a minimum version for registry requirements.
if edit.source.is_some() {
if edit
.source
.as_ref()
.is_some_and(|source| !matches!(source, Source::Registry { .. }))
{
continue;
}

Expand Down Expand Up @@ -869,6 +882,7 @@ fn resolve_requirement(
requirement: pypi_types::Requirement,
workspace: bool,
editable: Option<bool>,
index: Option<String>,
rev: Option<String>,
tag: Option<String>,
branch: Option<String>,
Expand All @@ -879,6 +893,7 @@ fn resolve_requirement(
requirement.source.clone(),
workspace,
editable,
index,
rev,
tag,
branch,
Expand Down
13 changes: 10 additions & 3 deletions crates/uv/tests/edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5064,7 +5064,7 @@ fn add_no_warn_index_url() -> Result<()> {

/// Add an index provided via `--index`.
#[test]
fn add_index_url() -> Result<()> {
fn add_index() -> Result<()> {
let context = TestContext::new("3.12");

let pyproject_toml = context.temp_dir.child("pyproject.toml");
Expand Down Expand Up @@ -5149,6 +5149,7 @@ fn add_index_url() -> Result<()> {
----- stdout -----
----- stderr -----
warning: Missing version constraint (e.g., a lower bound) for `jinja2`
Resolved 4 packages in [TIME]
Prepared 2 packages in [TIME]
Installed 2 packages in [TIME]
Expand Down Expand Up @@ -5178,6 +5179,9 @@ fn add_index_url() -> Result<()> {
[[tool.uv.index]]
url = "https://pypi.org/simple"
[tool.uv.sources]
jinja2 = { index = "pytorch" }
"###
);
});
Expand Down Expand Up @@ -5237,7 +5241,7 @@ fn add_index_url() -> Result<()> {
[package.metadata]
requires-dist = [
{ name = "iniconfig", specifier = "==2.0.0" },
{ name = "jinja2", specifier = ">=3.1.3" },
{ name = "jinja2", specifier = ">=3.1.3", index = "https://download.pytorch.org/whl/cu121" },
]
"###
);
Expand Down Expand Up @@ -5276,6 +5280,9 @@ fn add_index_url() -> Result<()> {
[[tool.uv.index]]
url = "https://pypi.org/simple"
[tool.uv.sources]
jinja2 = { index = "pytorch" }
"###
);
});
Expand Down Expand Up @@ -5341,7 +5348,7 @@ fn add_index_url() -> Result<()> {
[package.metadata]
requires-dist = [
{ name = "iniconfig", specifier = "==2.0.0" },
{ name = "jinja2", specifier = ">=3.1.3" },
{ name = "jinja2", specifier = ">=3.1.3", index = "https://test.pypi.org/simple" },
]
"###
);
Expand Down

0 comments on commit ead24e0

Please sign in to comment.