Skip to content

Commit

Permalink
Added a virtualenv backend for using uv
Browse files Browse the repository at this point in the history
This also causes session.install to use uv instead of pip.
  • Loading branch information
alex authored and henryiii committed Feb 17, 2024
1 parent c70b9d7 commit 5d4b3ac
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 16 deletions.
4 changes: 2 additions & 2 deletions nox/_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ def _session_completer(
" ``'virtualenv'`` by default but any of ``('virtualenv', 'conda', 'mamba',"
" 'venv')`` are accepted."
),
choices=["none", "virtualenv", "conda", "mamba", "venv"],
choices=["none", "virtualenv", "conda", "mamba", "venv", "uv"],
),
_option_set.Option(
"force_venv_backend",
Expand All @@ -371,7 +371,7 @@ def _session_completer(
" the default backend. Any of ``('virtualenv', 'conda', 'mamba', 'venv')``"
" are accepted."
),
choices=["none", "virtualenv", "conda", "mamba", "venv"],
choices=["none", "virtualenv", "conda", "mamba", "venv", "uv"],
),
_option_set.Option(
"no_venv",
Expand Down
4 changes: 2 additions & 2 deletions nox/sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,7 @@ def install(self, *args: str, **kwargs: Any) -> None:
if "silent" not in kwargs:
kwargs["silent"] = True

if getattr(venv, "venv_prog", "") == "uv":
if isinstance(venv, VirtualEnv) and venv.venv_backend == "uv":
self._run("uv", "pip", "install", *args, external="error", **kwargs)
else:
self._run(
Expand Down Expand Up @@ -758,7 +758,7 @@ def _create_venv(self) -> None:
self.envdir,
interpreter=self.func.python, # type: ignore[arg-type]
reuse_existing=reuse_existing,
venv=backend or "virtualenv",
venv_backend=backend or "virtualenv",
venv_params=self.func.venv_params,
)
elif backend in {"conda", "mamba"}:
Expand Down
16 changes: 10 additions & 6 deletions nox/virtualenv.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,15 +320,15 @@ def __init__(
interpreter: str | None = None,
reuse_existing: bool = False,
*,
venv: str = "virtualenv",
venv_backend: str = "virtualenv",
venv_params: Any = None,
):
self.location_name = location
self.location = os.path.abspath(location)
self.interpreter = interpreter
self._resolved: None | str | InterpreterNotFound = None
self.reuse_existing = reuse_existing
self.venv_prog = venv
self.venv_backend = venv_backend
self.venv_params = venv_params or []
super().__init__(env={"VIRTUAL_ENV": self.location})

Expand Down Expand Up @@ -360,7 +360,11 @@ def _check_reused_environment_type(self) -> bool:
old_env = (
"virtualenv" if any(pattern.match(line) for line in fp) else "venv"
)
return old_env == self.venv_prog
# We can't distinguish a uv env from a venv env, so just treat them
# the same.
return old_env == self.venv_backend or (
{old_env, self.venv_backend} == {"uv", "venv"}
)

def _check_reused_environment_interpreter(self) -> bool:
"""Check if reused environment interpreter is the same."""
Expand Down Expand Up @@ -475,11 +479,11 @@ def create(self) -> bool:

return False

if self.venv_prog == "virtualenv":
if self.venv_backend == "virtualenv":
cmd = [sys.executable, "-m", "virtualenv", self.location]
if self.interpreter:
cmd.extend(["-p", self._resolved_interpreter])
elif self.venv_prog == "uv":
elif self.venv_backend == "uv":
cmd = [
"uv",
"virtualenv",
Expand All @@ -494,7 +498,7 @@ def create(self) -> bool:
resolved_interpreter_name = os.path.basename(self._resolved_interpreter)

logger.info(
f"Creating virtual environment ({self.venv_prog}) using"
f"Creating virtual environment ({self.venv_backend}) using"
f" {resolved_interpreter_name} in {self.location_name}"
)
nox.command.run(cmd, silent=True, log=nox.options.verbose or False)
Expand Down
4 changes: 2 additions & 2 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def lint(session: nox.Session) -> None:
)


@nox.session(venv_backend="uv")
@nox.session
def docs(session: nox.Session) -> None:
"""Build the documentation."""
output_dir = os.path.join(session.create_tmp(), "output")
Expand All @@ -95,7 +95,7 @@ def docs(session: nox.Session) -> None:
)
shutil.rmtree(output_dir, ignore_errors=True)
session.install("-r", "requirements-test.txt")
session.install("nox @ .")
session.install(".")
session.cd("docs")
sphinx_args = ["-b", "html", "-W", "-d", doctrees, ".", html]

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ tox_to_nox = [
"tox<4",
]
uv = [
"uv"
"uv",
]
[project.urls]
bug-tracker = "https://github.com/wntrblm/nox/issues"
Expand Down
32 changes: 32 additions & 0 deletions tests/test_sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ def make_session_and_runner(self):
runner.venv = mock.create_autospec(nox.virtualenv.VirtualEnv)
runner.venv.env = {}
runner.venv.bin_paths = ["/no/bin/for/you"]
runner.venv.venv_backend = "venv"
return nox.sessions.Session(runner=runner), runner

def test_create_tmp(self):
Expand Down Expand Up @@ -629,6 +630,7 @@ def test_install(self):
)
runner.venv = mock.create_autospec(nox.virtualenv.VirtualEnv)
runner.venv.env = {}
runner.venv.venv_backend = "venv"

class SessionNoSlots(nox.sessions.Session):
pass
Expand Down Expand Up @@ -658,6 +660,7 @@ def test_install_non_default_kwargs(self):
)
runner.venv = mock.create_autospec(nox.virtualenv.VirtualEnv)
runner.venv.env = {}
runner.venv.venv_backend = "venv"

class SessionNoSlots(nox.sessions.Session):
pass
Expand Down Expand Up @@ -794,6 +797,35 @@ def test_session_venv_reused_with_no_install(self, no_install, reused, run_calle

assert run.called is run_called

def test_install_uv(self):
runner = nox.sessions.SessionRunner(
name="test",
signatures=["test"],
func=mock.sentinel.func,
global_config=_options.options.namespace(posargs=[]),
manifest=mock.create_autospec(nox.manifest.Manifest),
)
runner.venv = mock.create_autospec(nox.virtualenv.VirtualEnv)
runner.venv.env = {}
runner.venv.venv_backend = "uv"

class SessionNoSlots(nox.sessions.Session):
pass

session = SessionNoSlots(runner=runner)

with mock.patch.object(session, "_run", autospec=True) as run:
session.install("requests", "urllib3", silent=False)
run.assert_called_once_with(
"uv",
"pip",
"install",
"requests",
"urllib3",
silent=False,
external=True,
)

def test___slots__(self):
session, _ = self.make_session_and_runner()
with pytest.raises(AttributeError):
Expand Down
18 changes: 15 additions & 3 deletions tests/test_virtualenv.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

IS_WINDOWS = nox.virtualenv._SYSTEM == "Windows"
HAS_CONDA = shutil.which("conda") is not None
HAS_UV = shutil.which("uv") is not None
RAISE_ERROR = "RAISE_ERROR"
VIRTUALENV_VERSION = virtualenv.__version__

Expand Down Expand Up @@ -240,6 +241,17 @@ def test_condaenv_detection(make_conda):
assert path_regex.search(output).group("env_dir") == dir_.strpath


@pytest.mark.skipif(not HAS_UV, reason="Missing uv command.")
def test_uv_creation(make_one):
venv, _ = make_one(venv_backend="uv")
assert venv.location
assert venv.interpreter is None
assert venv.reuse_existing is False
assert venv.venv_backend == "uv"

venv.create()


def test_constructor_defaults(make_one):
venv, _ = make_one()
assert venv.location
Expand Down Expand Up @@ -417,7 +429,7 @@ def test_create_reuse_stale_venv_environment(make_one):

@enable_staleness_check
def test_create_reuse_stale_virtualenv_environment(make_one):
venv, location = make_one(reuse_existing=True, venv=True)
venv, location = make_one(reuse_existing=True, venv_backend="venv")
venv.create()

# Drop a virtualenv-style pyvenv.cfg into the environment.
Expand All @@ -442,7 +454,7 @@ def test_create_reuse_stale_virtualenv_environment(make_one):

@enable_staleness_check
def test_create_reuse_venv_environment(make_one):
venv, location = make_one(reuse_existing=True, venv="venv")
venv, location = make_one(reuse_existing=True, venv_backend="venv")
venv.create()

# Place a spurious occurrence of "virtualenv" in the pyvenv.cfg.
Expand Down Expand Up @@ -516,7 +528,7 @@ def test_create_reuse_python2_environment(make_one):


def test_create_venv_backend(make_one):
venv, dir_ = make_one(venv=True)
venv, dir_ = make_one(venv_backend="venv")
venv.create()


Expand Down

0 comments on commit 5d4b3ac

Please sign in to comment.