Skip to content

Commit

Permalink
refactor: pulled out get_virtualenv & better typing (#882)
Browse files Browse the repository at this point in the history
Signed-off-by: Henry Schreiner <[email protected]>
  • Loading branch information
henryiii authored Nov 4, 2024
1 parent 6f3a459 commit 622458c
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 36 deletions.
2 changes: 1 addition & 1 deletion nox/_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def __init__(
reuse_venv: bool | None = None,
name: str | None = None,
venv_backend: Any = None,
venv_params: Any = None,
venv_params: Sequence[str] = (),
should_warn: Mapping[str, Any] | None = None,
tags: Sequence[str] | None = None,
*,
Expand Down
4 changes: 2 additions & 2 deletions nox/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def session_decorator(
reuse_venv: bool | None = ...,
name: str | None = ...,
venv_backend: Any | None = ...,
venv_params: Any | None = ...,
venv_params: Sequence[str] = ...,
tags: Sequence[str] | None = ...,
*,
default: bool = ...,
Expand All @@ -55,7 +55,7 @@ def session_decorator(
reuse_venv: bool | None = None,
name: str | None = None,
venv_backend: Any | None = None,
venv_params: Any | None = None,
venv_params: Sequence[str] = (),
tags: Sequence[str] | None = None,
*,
default: bool = True,
Expand Down
43 changes: 14 additions & 29 deletions nox/sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,13 @@
from nox._decorators import Func
from nox.logger import logger
from nox.popen import DEFAULT_INTERRUPT_TIMEOUT, DEFAULT_TERMINATE_TIMEOUT
from nox.virtualenv import CondaEnv, PassthroughEnv, ProcessEnv, VirtualEnv
from nox.virtualenv import (
CondaEnv,
PassthroughEnv,
ProcessEnv,
VirtualEnv,
get_virtualenv,
)

if TYPE_CHECKING:
from typing import IO
Expand Down Expand Up @@ -991,34 +997,13 @@ def _create_venv(self) -> None:
or "virtualenv"
).split("|")

# Support fallback backends
for bk in backends:
if bk not in nox.virtualenv.ALL_VENVS:
msg = f"Expected venv_backend one of {list(nox.virtualenv.ALL_VENVS)!r}, but got {bk!r}."
raise ValueError(msg)

for bk in backends[:-1]:
if bk not in nox.virtualenv.OPTIONAL_VENVS:
msg = f"Only optional backends ({list(nox.virtualenv.OPTIONAL_VENVS)!r}) may have a fallback, {bk!r} is not optional."
raise ValueError(msg)

for bk in backends:
if nox.virtualenv.OPTIONAL_VENVS.get(bk, True):
backend = bk
break
else:
msg = f"No backends present, looked for {backends!r}."
raise ValueError(msg)

if backend == "none" or self.func.python is False:
self.venv = nox.virtualenv.ALL_VENVS["none"]()
else:
self.venv = nox.virtualenv.ALL_VENVS[backend](
self.envdir,
interpreter=self.func.python,
reuse_existing=reuse_existing,
venv_params=self.func.venv_params,
)
self.venv = get_virtualenv(
*backends,
reuse_existing=reuse_existing,
envdir=self.envdir,
interpreter=self.func.python,
venv_params=self.func.venv_params,
)

self.venv.create()

Expand Down
44 changes: 41 additions & 3 deletions nox/virtualenv.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import shutil
import subprocess
import sys
from collections.abc import Callable, Mapping
from collections.abc import Callable, Mapping, Sequence
from pathlib import Path
from socket import gethostbyname
from typing import Any, ClassVar
Expand All @@ -33,6 +33,7 @@

import nox
import nox.command
from nox._typing import Python
from nox.logger import logger

# Problematic environment variables that are stripped from all commands inside
Expand Down Expand Up @@ -286,7 +287,7 @@ def __init__(
location: str,
interpreter: str | None = None,
reuse_existing: bool = False,
venv_params: Any = None,
venv_params: Sequence[str] = (),
*,
conda_cmd: str = "conda",
):
Expand Down Expand Up @@ -422,7 +423,7 @@ def __init__(
reuse_existing: bool = False,
*,
venv_backend: str = "virtualenv",
venv_params: Any = None,
venv_params: Sequence[str] = (),
):
self.location_name = location
self.location = os.path.abspath(location)
Expand Down Expand Up @@ -662,3 +663,40 @@ def venv_backend(self) -> str:
"micromamba": shutil.which("micromamba") is not None,
"uv": HAS_UV,
}


def get_virtualenv(
*backends: str,
envdir: str,
reuse_existing: bool,
interpreter: Python = None,
venv_params: Sequence[str] = (),
) -> ProcessEnv:
# Support fallback backends
for bk in backends:
if bk not in ALL_VENVS:
msg = f"Expected venv_backend one of {sorted(ALL_VENVS)!r}, but got {bk!r}."
raise ValueError(msg)

for bk in backends[:-1]:
if bk not in OPTIONAL_VENVS:
msg = f"Only optional backends ({sorted(OPTIONAL_VENVS)!r}) may have a fallback, {bk!r} is not optional."
raise ValueError(msg)

for bk in backends:
if OPTIONAL_VENVS.get(bk, True):
backend = bk
break
else:
msg = f"No backends present, looked for {backends!r}."
raise ValueError(msg)

if backend == "none" or interpreter is False:
return ALL_VENVS["none"]()

return ALL_VENVS[backend](
envdir,
interpreter=interpreter,
reuse_existing=reuse_existing,
venv_params=venv_params,
)
2 changes: 1 addition & 1 deletion tests/test_sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -794,7 +794,7 @@ def lint(session):
if "-x" in session.posargs:
raise RuntimeError("invalid option: -x")

config = _options.options.namespace(posargs=[])
config = _options.options.namespace(posargs=[], envdir=".nox")
manifest = nox.manifest.Manifest(registry, config)

assert manifest["test"].execute()
Expand Down

0 comments on commit 622458c

Please sign in to comment.