Skip to content

Commit ae4dd5f

Browse files
authored
Pass python version to uv (#216)
This change reverts the use of `python=False` in all nox sessions. Sessions for tests, linting, and formatting will use nox virtual environments. In these sessions the location of the virtual environment will be passed to `uv run` and `uv sync`. This means uv will use the python version that nox is executed with. This change is needed because nox correctly masks all environment variables when starting a `session.run` or `session.run_install`. This is ideal to maintain repeatable behavior. This is not ideal when attempting to use `UV_PYTHON` to control which version is being run. Enforcing that uv use the python version that nox is executed with is the least surprising behavior. Even if this ignores the `.python-version` file in the repo.
1 parent a26b381 commit ae4dd5f

File tree

1 file changed

+23
-16
lines changed

1 file changed

+23
-16
lines changed

noxfile.py

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -51,25 +51,26 @@
5151
("black", LINT_PATH, TESTS_PATH),
5252
]
5353

54-
# Default args for all uv sync calls
55-
SYNC_ARGS = ["--frozen", "--quiet"]
54+
# Default args for all 'uv sync' and 'uv run' calls
55+
UV_ARGS = ["--frozen", "--quiet", "--active"]
5656

5757

5858
@nox.session(name="dev", python=False)
5959
def dev_session(session: nox.Session) -> None:
6060
"""Create a development environment."""
61-
session.run_install("uv", "sync", *SYNC_ARGS)
61+
session.run_install("uv", "sync")
6262

6363

64-
@nox.session(name="test", python=False)
64+
@nox.session(name="test")
6565
def run_tests_with_coverage(session: nox.Session) -> None:
6666
"""Run pytest in isolated environment, display coverage. Extra arguements passed to pytest."""
67+
uv_args = UV_ARGS + [f"--python={session.virtualenv.location}"]
68+
6769
partial = "partial-coverage" in session.posargs
68-
extra: list[str] = []
6970

70-
session.run_install("uv", "sync", *SYNC_ARGS, *extra)
71+
session.run_install("uv", "sync", *uv_args)
7172

72-
coverage = functools.partial(session.run, "uv", "run", *extra, "coverage")
73+
coverage = functools.partial(session.run, "uv", "run", *uv_args, "coverage")
7374

7475
coverage("erase")
7576

@@ -82,35 +83,41 @@ def run_tests_with_coverage(session: nox.Session) -> None:
8283
coverage("html")
8384

8485

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

90-
coverage = functools.partial(session.run, "uv", "run", "coverage")
93+
coverage = functools.partial(session.run, "uv", "run", *uv_args, "coverage")
9194

9295
coverage("combine")
9396
coverage("report", "--show-missing")
9497
coverage("html")
9598
coverage("json")
9699

97100

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

103108
for linter_args in LINTERS:
104-
session.run("uv", "run", *linter_args)
109+
session.run("uv", "run", *uv_args, *linter_args)
105110

106111

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

112119
for formatter_args in FORMATTERS:
113-
session.run("uv", "run", *formatter_args)
120+
session.run("uv", "run", *uv_args, *formatter_args)
114121

115122

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

0 commit comments

Comments
 (0)