From eba60bcb66734a5e5ee90bab2d17830b07b0c771 Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Thu, 31 Oct 2024 12:33:47 -0400 Subject: [PATCH] refactor: pulled out get_virtualenv & better typing Signed-off-by: Henry Schreiner --- nox/_decorators.py | 2 +- nox/registry.py | 4 ++-- nox/sessions.py | 43 ++++++++++++++--------------------------- nox/virtualenv.py | 44 +++++++++++++++++++++++++++++++++++++++--- tests/test_sessions.py | 2 +- 5 files changed, 59 insertions(+), 36 deletions(-) diff --git a/nox/_decorators.py b/nox/_decorators.py index f7e31744..62db1452 100644 --- a/nox/_decorators.py +++ b/nox/_decorators.py @@ -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, *, diff --git a/nox/registry.py b/nox/registry.py index d107a3f9..db3db9fb 100644 --- a/nox/registry.py +++ b/nox/registry.py @@ -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 = ..., @@ -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, diff --git a/nox/sessions.py b/nox/sessions.py index 3a51d751..18f72b68 100644 --- a/nox/sessions.py +++ b/nox/sessions.py @@ -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 @@ -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() diff --git a/nox/virtualenv.py b/nox/virtualenv.py index 514b2f3e..8b9525c6 100644 --- a/nox/virtualenv.py +++ b/nox/virtualenv.py @@ -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 @@ -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 @@ -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", ): @@ -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) @@ -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, + ) diff --git a/tests/test_sessions.py b/tests/test_sessions.py index 6f55988d..4181f0b3 100644 --- a/tests/test_sessions.py +++ b/tests/test_sessions.py @@ -793,7 +793,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()