Skip to content

Commit

Permalink
fix: look for uv next to python if it's not on PATH
Browse files Browse the repository at this point in the history
  • Loading branch information
cjolowicz committed Mar 7, 2024
1 parent 419b98a commit d317003
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 6 deletions.
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ repos:
- jinja2
- packaging
- importlib_metadata
- uv

- repo: https://github.com/codespell-project/codespell
rev: v2.2.6
Expand Down
4 changes: 2 additions & 2 deletions nox/sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
import nox.virtualenv
from nox._decorators import Func
from nox.logger import logger
from nox.virtualenv import CondaEnv, PassthroughEnv, ProcessEnv, VirtualEnv
from nox.virtualenv import UV, CondaEnv, PassthroughEnv, ProcessEnv, VirtualEnv

if TYPE_CHECKING:
from nox.manifest import Manifest
Expand Down Expand Up @@ -656,7 +656,7 @@ def install(self, *args: str, **kwargs: Any) -> None:
kwargs["silent"] = True

if isinstance(venv, VirtualEnv) and venv.venv_backend == "uv":
self._run("uv", "pip", "install", *args, external="error", **kwargs)
self._run(UV, "pip", "install", *args, external="error", **kwargs)
else:
self._run(
"python", "-m", "pip", "install", *args, external="error", **kwargs
Expand Down
23 changes: 20 additions & 3 deletions nox/virtualenv.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,23 @@
_SYSTEM = platform.system()


def find_uv() -> tuple[bool, str]:
uv = shutil.which("uv")
if uv is not None:
return True, uv

# Look for uv in Nox's environment as well, to handle `pipx install nox[uv]`.
with contextlib.suppress(ImportError, FileNotFoundError):
from uv import find_uv_bin

return True, find_uv_bin()

return False, "uv"


HAS_UV, UV = find_uv()


class InterpreterNotFound(OSError):
def __init__(self, interpreter: str) -> None:
super().__init__(f"Python interpreter {interpreter} not found")
Expand Down Expand Up @@ -325,7 +342,7 @@ class VirtualEnv(ProcessEnv):
"""

is_sandboxed = True
allowed_globals = ("uv",)
allowed_globals = (UV,)

def __init__(
self,
Expand Down Expand Up @@ -524,7 +541,7 @@ def create(self) -> bool:
cmd.extend(["-p", self._resolved_interpreter])
elif self.venv_backend == "uv":
cmd = [
"uv",
UV,
"venv",
"-p",
self._resolved_interpreter if self.interpreter else sys.executable,
Expand Down Expand Up @@ -560,5 +577,5 @@ def create(self) -> bool:
OPTIONAL_VENVS = {
"conda": shutil.which("conda") is not None,
"mamba": shutil.which("mamba") is not None,
"uv": shutil.which("uv") is not None,
"uv": HAS_UV,
}
2 changes: 1 addition & 1 deletion tests/test_sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -825,7 +825,7 @@ class SessionNoSlots(nox.sessions.Session):
with mock.patch.object(session, "_run", autospec=True) as run:
session.install("requests", "urllib3", silent=False)
run.assert_called_once_with(
"uv",
nox.virtualenv.UV,
"pip",
"install",
"requests",
Expand Down
27 changes: 27 additions & 0 deletions tests/test_virtualenv.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import shutil
import subprocess
import sys
import types
from textwrap import dedent
from typing import NamedTuple
from unittest import mock
Expand Down Expand Up @@ -538,6 +539,32 @@ def test_create_reuse_uv_environment(make_one):
assert reused


UV_IN_PIPX_VENV = "/home/user/.local/pipx/venvs/nox/bin/uv"


@pytest.mark.parametrize(
["which_result", "find_uv_bin_result", "expected"],
[
("/usr/bin/uv", UV_IN_PIPX_VENV, (True, "/usr/bin/uv")),
("/usr/bin/uv", FileNotFoundError, (True, "/usr/bin/uv")),
(None, UV_IN_PIPX_VENV, (True, UV_IN_PIPX_VENV)),
(None, FileNotFoundError, (False, "uv")),
],
) # fmt: skip
def test_find_uv(monkeypatch, which_result, find_uv_bin_result, expected):
def find_uv_bin():
if find_uv_bin_result is FileNotFoundError:
raise FileNotFoundError
return find_uv_bin_result

monkeypatch.setattr(shutil, "which", lambda _: which_result)
monkeypatch.setitem(
sys.modules, "uv", types.SimpleNamespace(find_uv_bin=find_uv_bin)
)

assert nox.virtualenv.find_uv() == expected


def test_create_reuse_venv_environment(make_one, monkeypatch):
# Making the reuse requirement more strict
monkeypatch.setenv("NOX_ENABLE_STALENESS_CHECK", "1")
Expand Down

0 comments on commit d317003

Please sign in to comment.