Skip to content

Commit

Permalink
Use uv instead of pip to manage nox virtualenvs
Browse files Browse the repository at this point in the history
Upgrade Nox to (at least) 2024.03.02 (which is the first version with
support for managing virtualenvs with uv.

Add uv as an indirect dependency by depending on "nox[uv]" instead of
"nox" (exception: inside the "lint" dependency group, we only depend on
"nox" in order for Mypy to access Nox' type annotations, uv is not
needed here).

Also we cannot use/depend on uv when using Python 3.7, since uv requires
Python >=v3.8. I'm not actually sure _why_ uv requires >=v3.8, as it is
apparently able to create venvs for Python v3.7 (see e.g.
https://github.com/astral-sh/uv?tab=readme-ov-file#python-discovery),
still astral-sh/uv#1239 prevents uv from being
installed on <=v3.7).

Finally, in noxfile.py, use uv as our default venv_backend instead of
the default (pip), but only when it is in fact available.

A final complication on Nix(OS) happens when we install requirements for
the current session; we do this in two steps, and then we make sure that
whatever we installed was patched appropriately:

    session.install("-r", str(requirments_txt))
    if include_self:
        session.install("-e", ".")

    if not session.virtualenv._reused:  # noqa: SLF001
        patch_binaries_if_needed(session, session.virtualenv.location)

However, with uv in the mix, we have to consider that session.install()
itself _runs_ uv at the same time as the first session.install() may
also _install_ uv itself into the virtualenv. The second
session.install() can then end up _running_ a uv that was _installed_
by the first session.install(), and this will break on Nix(OS) unless
the uv binary has been patched in the meantime.

We therefore need to insert a call to patch_binaries_if_needed()
_between_ the two session.install() calls. Since the second
session.install() only installs FawltyDeps itself (which does not
introduce any binaries to be patched), we can get away with simply
reordering the second session.install() and the call to
patch_binaries_if_needed():

    session.install("-r", str(requirments_txt))

    if not session.virtualenv._reused:  # noqa: SLF001
        patch_binaries_if_needed(session, session.virtualenv.location)

    if include_self:
        session.install("-e", ".")
  • Loading branch information
jherland committed May 7, 2024
1 parent a2c964c commit e6f968f
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 17 deletions.
9 changes: 7 additions & 2 deletions noxfile.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import hashlib
import os
import shutil
from pathlib import Path
from typing import Iterable

import nox

python_versions = ["3.7", "3.8", "3.9", "3.10", "3.11", "3.12"]

# Use 'uv' to manager Nox' virtualenvs, if available
nox.options.default_venv_backend = "uv" if shutil.which("uv") else "none"


def patch_binaries_if_needed(session: nox.Session, venv_dir: str) -> None:
"""If we are on Nix, auto-patch any binaries under `venv_dir`.
Expand Down Expand Up @@ -76,12 +80,13 @@ def install_groups(
hashfile.write_text(digest)

session.install("-r", str(requirements_txt))
if include_self:
session.install("-e", ".")

if not session.virtualenv._reused: # noqa: SLF001
patch_binaries_if_needed(session, session.virtualenv.location)

if include_self:
session.install("-e", ".")


@nox.session(python=python_versions)
def tests(session):
Expand Down
47 changes: 35 additions & 12 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 9 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ setuptools = [
optional = true

[tool.poetry.group.nox.dependencies]
nox = "^2022.11.21"
nox = [
{version="^2024.03.02", python = "<3.8"},
{version="^2024.03.02", extras=["uv"], python = ">=3.8"},
]

[tool.poetry.group.test]
optional = true
Expand All @@ -74,7 +77,7 @@ optional = true
[tool.poetry.group.lint.dependencies]
hypothesis = "^6.68.2"
mypy = "^1.0.1"
nox = "^2022.11.21"
nox = "^2024.03.02"
pytest = "^7.1.0"
ruff = ">=0.3"
types-setuptools = "^65.6.0.2"
Expand All @@ -97,7 +100,10 @@ optional = true
codespell = "^2.2.4"
hypothesis = "^6.68.2"
mypy = "^1.0.1"
nox = "^2022.11.21"
nox = [
{version="^2024.03.02", python = "<3.8"},
{version="^2024.03.02", extras=["uv"], python = ">=3.8"},
]
pytest = "^7.1.0"
ruff = ">=0.3"
types-setuptools = "^65.6.0.2"
Expand Down

0 comments on commit e6f968f

Please sign in to comment.