-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathnoxfile.py
More file actions
307 lines (242 loc) · 10.1 KB
/
Copy pathnoxfile.py
File metadata and controls
307 lines (242 loc) · 10.1 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
#!/usr/bin/env -S uv run --script # noqa: EXE001
# /// script
# dependencies = ["nox", "nox_uv"]
# ///
"""Nox setup."""
import argparse
import os
import shutil
from enum import StrEnum, auto
from pathlib import Path
from typing import assert_never
import nox
from nox_uv import session
nox.needs_version = ">=2024.3.2"
nox.options.default_venv_backend = "uv"
DIR = Path(__file__).parent.resolve()
class PackageEnum(StrEnum):
"""Enum for package names."""
@staticmethod
def _generate_next_value_(name: str, *_: object, **__: object) -> str:
return name
def __repr__(self) -> str:
return f"{self.value!r}"
unxt = auto()
unxt_api = auto()
unxt_hypothesis = auto()
api = auto()
hypothesis = auto()
interop_gala = auto()
interop_matplotlib = auto()
interop_xarray = auto()
parametric = auto()
# =============================================================================
# Comprehensive sessions
@session(
uv_groups=["lint", "test", "docs"],
uv_extras=["all"],
reuse_venv=True,
default=True,
)
def all(s: nox.Session, /) -> None: # noqa: A001
"""Run all default sessions."""
s.notify("lint")
s.notify("test")
s.notify("docs")
# =============================================================================
# Linting
@session(uv_groups=["lint"], reuse_venv=True)
def lint(s: nox.Session, /) -> None:
"""Run the linter."""
s.notify("precommit")
for package in PackageEnum:
s.notify(f"pylint(package={package.value!r})")
@session(uv_groups=["lint"], reuse_venv=True)
def precommit(s: nox.Session, /) -> None:
"""Run the pre-commit hooks (via prek)."""
# Not a real commit -- no-commit-to-branch would always fail here.
# Merge into any SKIP already set, rather than clobber it.
skip = ",".join(filter(None, [os.environ.get("SKIP"), "no-commit-to-branch"]))
s.run("prek", "run", "--all-files", *s.posargs, env={"SKIP": skip})
@session(uv_groups=["typecheck"], uv_extras=["all"], reuse_venv=True)
def pyright(s: nox.Session, /) -> None:
"""Type-check the typing smoke fixture with pyright.
Scoped (via ``[tool.pyright]`` ``include``) to ``tests/typing`` -- the guard
for the ``Quantity(1, "m")`` constructor typing. Not the whole tree, which
is not yet pyright-clean.
"""
s.run("pyright", *s.posargs)
@session(uv_groups=["typecheck"], uv_extras=["all"], reuse_venv=True)
def ty(s: nox.Session, /) -> None:
"""Type-check the typing smoke fixture with ty.
Scoped (via ``[tool.ty.src]`` ``include``) to ``tests/typing`` -- the same
``Quantity(1, "m")`` constructor guard as the ``pyright`` session. Not the
whole tree, which ty is not yet clean on. ty (0.0.x) is pinned; expect
deliberate periodic bumps.
"""
s.run("ty", "check", *s.posargs)
@session(uv_groups=["typecheck"], uv_extras=["all"], reuse_venv=True)
def mypy(s: nox.Session, /) -> None:
"""Type-check the typing smoke fixture with mypy.
Scoped (via ``[tool.mypy]`` ``files``) to ``tests/typing`` -- the same
``Quantity(1, "m")`` constructor guard as the ``pyright`` session, not the
whole tree (which mypy is not yet clean on). mypy types the ``unit`` param
as ``Any``, so it does not discriminate the unit argument; what it gates is
that the fixture stays strict-mypy-clean and the constructors return the
right type. The raw-``value`` converter gap is suppressed per call site.
"""
s.run("mypy", *s.posargs)
def _parse_pylint_paths(package: PackageEnum, /) -> list[str]:
# Lint each package in isolation so the ``duplicate-code`` checker does not
# flag the intentional similarity between a shim and its canonical package.
match package:
case PackageEnum.unxt:
return ["src/unxt"]
case PackageEnum.unxt_api:
return ["packages/unxt-api/src"]
case PackageEnum.unxt_hypothesis:
return ["packages/unxt-hypothesis/src"]
case PackageEnum.api:
return ["packages/unxts.api/src"]
case PackageEnum.hypothesis:
return ["packages/unxts.hypothesis/src"]
case PackageEnum.interop_gala:
return ["packages/unxts.interop.gala/src"]
case PackageEnum.interop_matplotlib:
return ["packages/unxts.interop.matplotlib/src"]
case PackageEnum.interop_xarray:
return ["packages/unxts.interop.xarray/src"]
case PackageEnum.parametric:
return ["packages/unxts.parametric/src"]
case _:
assert_never(package)
@session(uv_groups=["lint"], uv_extras=["workspace"], reuse_venv=True)
@nox.parametrize("package", list(PackageEnum))
def pylint(s: nox.Session, /, package: PackageEnum) -> None:
"""Run PyLint."""
s.run("pylint", *_parse_pylint_paths(package), *s.posargs)
# =============================================================================
# Testing
@session(uv_groups=["test"], uv_extras=["workspace"], reuse_venv=True)
def test(s: nox.Session, /) -> None:
"""Run the unit and regular tests."""
for package in PackageEnum:
s.notify(f"pytest(package={package.value!r})", posargs=s.posargs)
# s.notify("pytest_benchmark", posargs=s.posargs)
def _parse_pytest_paths(package: PackageEnum, /) -> list[str]:
# The canonical ``unxts.*`` namespace packages point only at their ``tests``
# directory: pytest's namespace-package path insertion would otherwise let a
# leaf like ``unxts/interop/xarray`` shadow the real ``xarray`` when it
# collects the src doctests. Those doctests are exercised via the docs pages.
match package:
case PackageEnum.unxt:
return ["README.md", "docs", "src/", "tests/"]
case PackageEnum.unxt_api:
return ["packages/unxt-api/"]
case PackageEnum.unxt_hypothesis:
return ["packages/unxt-hypothesis/"]
case PackageEnum.api:
return ["packages/unxts.api/tests"]
case PackageEnum.hypothesis:
return ["packages/unxts.hypothesis/tests"]
case PackageEnum.interop_gala:
return ["packages/unxts.interop.gala/tests"]
case PackageEnum.interop_matplotlib:
return ["packages/unxts.interop.matplotlib/tests"]
case PackageEnum.interop_xarray:
return ["packages/unxts.interop.xarray/tests"]
case PackageEnum.parametric:
return ["packages/unxts.parametric/tests"]
case _:
assert_never(package)
# ``test-all`` (not ``test``) because the ``workspace`` extra pulls in
# matplotlib via ``unxt[interop-mpl]``, so the matplotlib integration tests are
# collected and need ``pytest-mpl`` to register the ``mpl_image_compare`` marker.
@session(uv_groups=["test-all"], uv_extras=["workspace"], reuse_venv=True)
@nox.parametrize("package", list(PackageEnum))
def pytest(s: nox.Session, /, package: PackageEnum) -> None:
"""Run the unit and regular tests."""
package_paths = _parse_pytest_paths(package)
s.run("pytest", *package_paths, *s.posargs)
@session(uv_groups=["test-all"], uv_extras=["interop-mpl"], reuse_venv=True)
@nox.parametrize("package", list(PackageEnum))
def pytest_all(s: nox.Session, /, package: PackageEnum) -> None:
"""Run the unit and regular tests."""
package_paths = _parse_pytest_paths(package)
s.run("pytest", *package_paths, *s.posargs)
@session(uv_groups=["test"], reuse_venv=True)
def pytest_benchmark(s: nox.Session, /) -> None:
"""Run the benchmarks."""
s.run("pytest", "tests/benchmark", "--codspeed", *s.posargs)
# =============================================================================
# Documentation
@session(uv_groups=["docs"], uv_extras=["workspace"], reuse_venv=True)
def docs(s: nox.Session, /) -> None:
"""Build the docs. Pass "--serve" to serve. Pass "-b linkcheck" to check links."""
parser = argparse.ArgumentParser()
parser.add_argument("--serve", action="store_true", help="Serve after building")
parser.add_argument(
"-b", dest="builder", default="html", help="Build target (default: html)"
)
parser.add_argument("--offline", action="store_true", help="run in offline mode")
parser.add_argument("--output-dir", dest="output_dir", default="_build")
args, posargs = parser.parse_known_args(s.posargs)
if args.builder != "html" and args.serve:
s.error("Must not specify non-HTML builder with --serve")
s.chdir("docs")
# Generate custom intersphinx inventories
s.run("python", "_static/generate_jaxtyping_inv.py")
s.run("python", "_static/generate_equinox_inv.py")
s.run("python", "_static/generate_quax_blocks_inv.py")
# Convert jupytext markdown files to notebooks
s.run(
"jupytext",
"--to",
"notebook",
"how-to/optimize-performance.md",
"--output",
"how-to/optimize-performance.ipynb",
)
if args.builder == "linkcheck":
s.run("sphinx-build", "-b", "linkcheck", ".", "_build/linkcheck", *posargs)
return
shared_args = (
"-n", # nitpicky mode
"-T", # full tracebacks
f"-b={args.builder}",
f"-d={args.output_dir}/doctrees",
"-D",
"language=en",
".",
f"{args.output_dir}/{args.builder}",
*posargs,
)
if args.serve:
s.run("sphinx-autobuild", *shared_args)
else:
s.run("sphinx-build", "--keep-going", *shared_args)
@session(uv_groups=["docs"], reuse_venv=True)
def build_api_docs(s: nox.Session, /) -> None:
"""Build (regenerate) API docs."""
s.chdir("docs")
s.run(
"sphinx-apidoc",
"-o",
"reference/api/",
"--module-first",
"--no-toc",
"--force",
"../src/unxt",
)
# =============================================================================
# Packaging
@session(uv_groups=["build"])
def build(s: nox.Session, /) -> None:
"""Build an SDist and wheel."""
build_path = DIR.joinpath("build")
if build_path.exists():
shutil.rmtree(build_path)
s.run("python", "-m", "build")
# =============================================================================
if __name__ == "__main__":
nox.main()