Skip to content

Commit 5335cfc

Browse files
authored
Adjust noxfile to play nice with uvx and tools (#217)
Per the official documentation of uv, `uv tool run` or `uvx` purposely ignore configuration files that select the python version to use. This leads to complications when using nox, not wanting nox as a required dev dependency, and wanting to leverage `uvx nox` or using `nox` as an installed tool. Solution: Reapply the `python=False` arguements for all sessions. For the `test` session which is expected to be run on multiple versions; check for the existance of `UV_PYTHON` and add that requirement to the cli flags as needed. For all other sessions, default to the `.python-version` pinned interpreter.
1 parent ae4dd5f commit 5335cfc

File tree

1 file changed

+19
-18
lines changed

1 file changed

+19
-18
lines changed

noxfile.py

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
import os
34
import functools
45
import pathlib
56
import shutil
@@ -52,7 +53,11 @@
5253
]
5354

5455
# Default args for all 'uv sync' and 'uv run' calls
55-
UV_ARGS = ["--frozen", "--quiet", "--active"]
56+
UV_ARGS = [
57+
"--frozen",
58+
"--quiet",
59+
"--active",
60+
]
5661

5762

5863
@nox.session(name="dev", python=False)
@@ -61,10 +66,12 @@ def dev_session(session: nox.Session) -> None:
6166
session.run_install("uv", "sync")
6267

6368

64-
@nox.session(name="test")
69+
@nox.session(name="test", python=False)
6570
def run_tests_with_coverage(session: nox.Session) -> None:
6671
"""Run pytest in isolated environment, display coverage. Extra arguements passed to pytest."""
67-
uv_args = UV_ARGS + [f"--python={session.virtualenv.location}"]
72+
uv_args = UV_ARGS
73+
if "UV_PYTHON" in os.environ:
74+
uv_args = UV_ARGS + [f"--python={os.environ['UV_PYTHON']}"]
6875

6976
partial = "partial-coverage" in session.posargs
7077

@@ -83,41 +90,35 @@ def run_tests_with_coverage(session: nox.Session) -> None:
8390
coverage("html")
8491

8592

86-
@nox.session(name="combine")
93+
@nox.session(name="combine", python=False)
8794
def combine_coverage(session: nox.Session) -> None:
8895
"""Combine parallel-mode coverage files and produce reports."""
89-
uv_args = UV_ARGS + [f"--python={session.virtualenv.location}"]
90-
91-
session.run_install("uv", "sync", *uv_args)
96+
session.run_install("uv", "sync", *UV_ARGS)
9297

93-
coverage = functools.partial(session.run, "uv", "run", *uv_args, "coverage")
98+
coverage = functools.partial(session.run, "uv", "run", *UV_ARGS, "coverage")
9499

95100
coverage("combine")
96101
coverage("report", "--show-missing")
97102
coverage("html")
98103
coverage("json")
99104

100105

101-
@nox.session(name="lint")
106+
@nox.session(name="lint", python=False)
102107
def run_linters(session: nox.Session) -> None:
103108
"""Run code linters, and type checking against all files."""
104-
uv_args = UV_ARGS + [f"--python={session.virtualenv.location}"]
105-
106-
session.run_install("uv", "sync", "--group", "lint", *uv_args)
109+
session.run_install("uv", "sync", "--group", "lint", *UV_ARGS)
107110

108111
for linter_args in LINTERS:
109-
session.run("uv", "run", *uv_args, *linter_args)
112+
session.run("uv", "run", *UV_ARGS, *linter_args)
110113

111114

112-
@nox.session(name="format")
115+
@nox.session(name="format", python=False)
113116
def run_formatters(session: nox.Session) -> None:
114117
"""Run code formatters against all files."""
115-
uv_args = UV_ARGS + [f"--python={session.virtualenv.location}"]
116-
117-
session.run_install("uv", "sync", "--group", "format", *uv_args)
118+
session.run_install("uv", "sync", "--group", "format", *UV_ARGS)
118119

119120
for formatter_args in FORMATTERS:
120-
session.run("uv", "run", *uv_args, *formatter_args)
121+
session.run("uv", "run", *UV_ARGS, *formatter_args)
121122

122123

123124
@nox.session(name="build", python=False)

0 commit comments

Comments
 (0)