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

Uv index strategy respect env var; issue #3410 #3411

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions news/3410.feature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
When use_uv = True will try to choose index-strategy for uv based on UV_INDEX_STRATEGY environments variable;
if this variable isn't set will default to existing strategy which is "unsafe-first-match"
if resolution.respect-source-order is set to True in pyproject.toml and "unsafe-best-match" otherwise
13 changes: 10 additions & 3 deletions src/pdm/resolver/uv.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import logging
import os
import re
import subprocess
from dataclasses import dataclass, replace
Expand All @@ -26,6 +27,8 @@

GIT_URL = re.compile(r"(?P<repo>[^:/]+://[^\?#]+)(?:\?rev=(?P<ref>[^#]+?))?(?:#(?P<revision>[a-f0-9]+))$")

ALLOWED_INDEX_STRATEGIES = {"first-index", "unsafe-first-match", "unsafe-best-match"}


@dataclass
class UvResolver(Resolver):
Expand Down Expand Up @@ -69,10 +72,14 @@ def _build_lock_command(self) -> list[str]:
first_index = False
else:
cmd.extend(["--extra-index-url", source.url])
if self.project.pyproject.settings.get("resolution", {}).get("respect-source-order", False):
cmd.append("--index-strategy=unsafe-first-match")
if index_strategy := os.environ.get("UV_INDEX_STRATEGY"):
if index_strategy not in ALLOWED_INDEX_STRATEGIES:
raise ValueError(f"UV_INDEX_STRATEGY should be one of {' '.join(ALLOWED_INDEX_STRATEGIES)}")
elif self.project.pyproject.settings.get("resolution", {}).get("respect-source-order", False):
index_strategy = "unsafe-first-match"
else:
cmd.append("--index-strategy=unsafe-best-match")
index_strategy = "unsafe-best-match"
cmd.append(f"--index-strategy={index_strategy}")
if self.update_strategy != "all":
for name in self.tracked_names:
cmd.extend(["-P", name])
Expand Down
46 changes: 37 additions & 9 deletions tests/resolver/test_uv_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,28 @@
pytestmark = [pytest.mark.network, pytest.mark.uv]


def resolve(environment, requirements, target=None):
def get_resolver(environment, requirements, target=None):
from pdm.resolver.uv import UvResolver

reqs = []
for req in requirements:
if isinstance(req, str):
req = parse_requirement(req)
req.groups = ["default"]
reqs.append(req)

resolver = UvResolver(
environment,
requirements=reqs,
requirements=requirements,
target=target or environment.spec,
update_strategy="all",
strategies=set(),
)

return resolver


def resolve(environment, requirements, target=None):
reqs = []
for req in requirements:
if isinstance(req, str):
req = parse_requirement(req)
req.groups = ["default"]
reqs.append(req)
resolver = get_resolver(environment, reqs, target)
return resolver.resolve()


Expand Down Expand Up @@ -80,3 +85,26 @@ def test_resolve_dependencies_with_overrides(project, overrides):

mapping = {p.candidate.identify(): p.candidate for p in resolution.packages}
assert mapping["requests"].version == "2.31.0"


def test_index_strategy(project, monkeypatch):
from pdm.resolver.uv import ALLOWED_INDEX_STRATEGIES

environment = project.environment
resolver = get_resolver(environment, [], None)

command = resolver._build_lock_command()
assert "--index-strategy=unsafe-best-match" in command

project.pyproject.settings["resolution"] = {"respect-source-order": True}
command = resolver._build_lock_command()
assert "--index-strategy=unsafe-first-match" in command

for index_strategy in ALLOWED_INDEX_STRATEGIES:
monkeypatch.setenv("UV_INDEX_STRATEGY", index_strategy)
command = resolver._build_lock_command()
assert r"--index-strategy={strategy}" in command

with pytest.raises(ValueError):
monkeypatch.setenv("UV_INDEX_STRATEGY", "abcd")
command = resolver._build_lock_command()
Loading