Repository navigation
Expand file tree
/
Copy pathnoxfile.py
More file actions
352 lines (303 loc) · 9.15 KB
/
Copy pathnoxfile.py
File metadata and controls
352 lines (303 loc) · 9.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
"""Nox config."""
import os
import pathlib
import shutil
import nox
import nox_uv
# Options to modify nox behaviour
nox.options.default_venv_backend = "uv"
nox.options.reuse_existing_virtualenvs = True
nox.options.sessions = [
"lint",
"tests",
]
LATEST_PYTHON = ["3.14"]
ALL_PYTHON = [
"3.11",
"3.12",
"3.13",
*LATEST_PYTHON,
]
ARRAY_BACKENDS = {
"array_api_strict": "array-api-strict>=2",
"jax": "jax>=0.6.2",
}
GRAD_TESTS_LOC = pathlib.Path("tests/gradients")
REG_TESTS_LOC = pathlib.Path("tests/regression")
GLASS_REPO_URL = "https://github.com/glass-dev/glass"
SHARED_PYTEST_BENCHMARK_FLAGS = [
"--benchmark-calibration-precision=1000",
"--benchmark-columns=mean,stddev,rounds",
"--benchmark-max-time=5.0",
"--benchmark-sort=name",
"--benchmark-timer=time.process_time",
]
def _check_revision_count(
session_posargs: list[str],
*,
expected_count: int,
) -> None:
"""
Check that the correct number of revisions have been provided.
Parameters
----------
session_posargs
The positional arguments passed to the session.
expected_count
The expected number of revisions.
Raises
------
ValueError
If no revisions are provided.
ValueError
If the number of provided revisions does not match the expected count.
"""
if not session_posargs:
msg = f"{expected_count} revision(s) not provided"
raise ValueError(msg)
if len(session_posargs) < expected_count:
msg = (
f"Incorrect number of revisions provided ({len(session_posargs)}), "
f"expected at least {expected_count}"
)
raise ValueError(msg)
@nox_uv.session(
python=LATEST_PYTHON,
uv_no_install_project=True,
uv_only_groups=["lint"],
uv_sync_locked=False,
)
def lint(session: nox.Session) -> None:
"""Run the linter."""
session.run("prek", "run", "--all-files", "--color", "always", *session.posargs)
def _setup_array_backend(session: nox.Session) -> None:
"""Installs the requested array_backend."""
array_backend = os.environ.get("ARRAY_BACKEND")
if array_backend == "array_api_strict":
session.install(ARRAY_BACKENDS["array_api_strict"])
elif array_backend == "jax":
session.install(ARRAY_BACKENDS["jax"])
elif array_backend == "all":
session.install(*ARRAY_BACKENDS.values())
@nox_uv.session(
python=ALL_PYTHON,
uv_groups=["test"],
uv_sync_locked=False,
)
def tests(session: nox.Session) -> None:
"""Run the unit tests."""
_setup_array_backend(session)
session.run("pytest", *session.posargs)
@nox_uv.session(
python=ALL_PYTHON,
reuse_venv=False,
uv_groups=["test"],
uv_sync_locked=False,
)
def tests_backend_selection(session: nox.Session) -> None:
"""Check ARRAY_BACKEND selection with its requested library installed."""
_setup_array_backend(session)
session.run(
"pytest",
# print skip reasons for manual verification
"--report-chars=s",
"tests/core/test_array_api_utils.py::test_xp_fixture_selects_requested_backend",
"tests/core/test_array_api_utils.py::test_default_rng_jax",
"tests/core/test_array_api_utils.py::test_default_rng_array_api_strict",
*session.posargs,
)
@nox_uv.session(
python=ALL_PYTHON,
uv_groups=["test"],
uv_sync_locked=False,
)
def coverage(session: nox.Session) -> None:
"""Run tests and compute coverage for the core tests."""
_setup_array_backend(session)
session.run(
"pytest",
"--cov",
*session.posargs,
)
@nox_uv.session(
python=LATEST_PYTHON,
uv_groups=["test"],
uv_sync_locked=False,
)
def coverage_regression(session: nox.Session) -> None:
"""Run tests and compute coverage for the regression tests."""
_setup_array_backend(session)
session.run(
"pytest",
REG_TESTS_LOC,
"--cov",
*SHARED_PYTEST_BENCHMARK_FLAGS,
*session.posargs,
success_codes=[0, 5],
)
@nox_uv.session(
python=LATEST_PYTHON,
uv_groups=["test"],
uv_sync_locked=False,
)
def coverage_gradients(session: nox.Session) -> None:
"""Run tests and compute coverage for the JAX gradients tests."""
session.install(ARRAY_BACKENDS["jax"])
session.run(
"pytest",
GRAD_TESTS_LOC,
"--cov",
*session.posargs,
)
@nox_uv.session(
python=ALL_PYTHON,
uv_groups=["doctest"],
uv_no_install_project=True,
uv_sync_locked=False,
)
def doctests(session: nox.Session) -> None:
"""Run the doctests."""
session.posargs.extend(
[
"--doctest-plus",
"--doctest-plus-generate-diff=overwrite",
"glass",
],
)
session.run("pytest", *session.posargs)
@nox_uv.session(
python=LATEST_PYTHON,
uv_extras=["examples"],
uv_sync_locked=False,
)
def examples(session: nox.Session) -> None:
"""Run the example notebooks. Pass "html" to build html."""
if session.posargs:
if "html" in session.posargs:
session.log("Generating HTML for the example notebooks")
session.run(
"jupyter",
"nbconvert",
"--to",
"html",
"--embed-images",
"examples/**/*.ipynb",
)
else:
session.log("Unsupported argument to examples")
else:
session.run(
"jupyter",
"execute",
"--inplace",
*pathlib.Path().glob("examples/**/*.ipynb"),
*session.posargs,
)
@nox_uv.session(
python=LATEST_PYTHON,
uv_groups=["docs"],
uv_sync_locked=False,
)
def docs(session: nox.Session) -> None:
"""Build the docs. Pass "serve" to serve."""
session.chdir("docs")
session.run(
"sphinx-build",
"-M",
"html",
".",
"_build",
"--fail-on-warning",
)
if session.posargs:
if "serve" in session.posargs:
port = 8001
session.log(
f"Launching docs at http://localhost:{port}/ - use Ctrl-C to quit",
)
session.run("python", "-m", "http.server", f"{port}", "-d", "_build/html")
else:
session.log("Unsupported argument to docs")
@nox_uv.session(
python=LATEST_PYTHON,
uv_no_install_project=True,
uv_only_groups=["build"],
uv_sync_locked=False,
)
def build(session: nox.Session) -> None:
"""Build an SDist and wheel."""
session.run("python", "-m", "build")
@nox_uv.session(
python=LATEST_PYTHON,
uv_sync_locked=False,
)
def version(session: nox.Session) -> None:
"""
Check the current version of the package.
The intent of this check is to ensure that the package
is installed without any additional dependencies
through optional dependencies nor dependency groups.
"""
session.run("python", "-c", "import glass; print(glass.__version__)")
@nox_uv.session(
python=LATEST_PYTHON,
uv_no_install_project=True,
uv_only_groups=["test"],
uv_sync_locked=False,
)
def regression_tests(session: nox.Session) -> None:
"""
Run regression tests between two revisions.
Note it is not possible to pass extra options to pytest.
"""
minimum_posargs = 2
_check_revision_count(session.posargs, expected_count=minimum_posargs)
before_revision, after_revision = session.posargs[:minimum_posargs]
_setup_array_backend(session)
# make sure .benchmark directory is clean
benchmark_dir = pathlib.Path(".benchmarks")
if benchmark_dir.exists():
session.log(f"Deleting previous .benchmark directory: {benchmark_dir}")
shutil.rmtree(benchmark_dir)
session.log(f"Generating prior benchmark from revision {before_revision}")
session.install(f"git+{GLASS_REPO_URL}@{before_revision}")
session.run(
"pytest",
REG_TESTS_LOC,
"--benchmark-autosave",
*SHARED_PYTEST_BENCHMARK_FLAGS,
*session.posargs[2:],
)
# Allow no tests to have been found if the tests have been filtered by the user
success_codes = [0, 5] if "-k" in session.posargs[2:] else [0]
session.log(f"Comparing {before_revision} benchmark to revision {after_revision}")
if after_revision == "local":
session.log("Installing after-revision from local checkout")
session.install("--force-reinstall", ".")
else:
session.install("--force-reinstall", f"git+{GLASS_REPO_URL}@{after_revision}")
session.log("Running stable regression tests")
session.run(
"pytest",
REG_TESTS_LOC,
"-m",
"stable",
"--benchmark-compare=0001",
"--benchmark-compare-fail=mean:5%",
*SHARED_PYTEST_BENCHMARK_FLAGS,
*session.posargs[2:],
success_codes=success_codes,
)
session.log("Running unstable regression tests")
session.run(
"pytest",
REG_TESTS_LOC,
"-m",
"unstable",
"--benchmark-compare=0001",
# Absolute time comparison in seconds
"--benchmark-compare-fail=mean:0.0005",
*SHARED_PYTEST_BENCHMARK_FLAGS,
*session.posargs[2:],
success_codes=success_codes,
)