forked from PlasmaPy/PlasmaPy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnoxfile.py
More file actions
642 lines (493 loc) · 19.6 KB
/
noxfile.py
File metadata and controls
642 lines (493 loc) · 19.6 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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
"""
Nox is an automation tool used by PlasmaPy to run tests, build
documentation, and perform other checks. Nox sessions are defined in
noxfile.py.
Running `nox` without arguments will run tests with the version of
Python that `nox` is installed under, skipping slow tests. To invoke a
nox session, enter the top-level directory of this repository and run
`nox -s "<session>"`, where <session> is replaced with the name of the
session. To list available sessions, run `nox -l`.
The tests can be run with the following options:
* "all": run all tests
* "skipslow": run tests, except tests decorated with `@pytest.mark.slow`
* "cov": run all tests with code coverage checks
* "lowest-direct" : run all tests with lowest version of direct dependencies
Doctests are run only for the most recent versions of Python and
PlasmaPy dependencies, and not when code coverage checks are performed.
Some of the checks require the most recent supported version of Python
to be installed.
Nox documentation: https://nox.thea.codes
"""
import os
import pathlib
import re
import shutil
import sys
import nox
# SPEC 0 indicates that scientific Python packages should support
# versions of Python that have been released in the last 3 years, or
# equivalently the most three recently released versions of Python.
# The minimum version of Python should be incremented immediately
# following the first release after October of each year.
supported_python_versions: tuple[str, ...] = ("3.11", "3.12", "3.13")
supported_operating_systems: tuple[str, ...] = ("linux", "macos", "windows")
maxpython = max(supported_python_versions)
minpython = min(supported_python_versions)
# The documentation should be build always using the same version of
# Python, which should be the latest version of Python supported by Read
# the Docs. Because Read the Docs takes some time to support new
# releases of Python, we should not link docpython to maxpython.
docpython = "3.12"
current_python = f"{sys.version_info.major}.{sys.version_info.minor}"
nox.options.sessions = [f"tests-{current_python}(skipslow)"]
nox.options.default_venv_backend = "uv|virtualenv"
uv_sync = ("uv", "sync", "--no-progress", "--frozen")
running_on_ci = os.getenv("CI")
running_on_rtd = os.environ.get("READTHEDOCS") == "True"
uv_requirement = "uv >= 0.6.5"
def _create_requirements_pr_message(uv_output: str, session: nox.Session) -> None:
"""
Create the pull request message during requirements updates.
This function copies a GitHub flavored Markdown template to a new
file and appends a table containing the updated requirements, with
links to the corresponding PyPI pages. This file is then used as the
body of the pull request message used in the workflow for updating
requirements.
Parameters
----------
uv_output : str
The multi-line output of ``session.run(..., silent=True)``.
"""
pr_template = pathlib.Path("./.github/content/update-requirements-pr-template.md")
pr_message = pathlib.Path("./.github/content/update-requirements-pr-body.md")
shutil.copy(pr_template, pr_message)
lines = [
"",
"| package | old version | new version |",
"| :-----: | :---------: | :---------: |",
]
for package_update in uv_output.splitlines():
if not package_update.startswith("Updated"):
session.debug(f"Line not added to table: {package_update}")
continue
try:
# An example line is "Updated nbsphinx v0.9.6 -> v0.9.7"
_, package_, old_version_, _, new_version_ = package_update.split()
except ValueError:
session.debug(f"Line not added to table: {package_update}:")
continue
old_version = f"{old_version_.removeprefix('v')}"
new_version = f"{new_version_.removeprefix('v')}"
pypi_link = f"https://pypi.org/project/{package_}/{new_version}"
package = f"[`{package_}`]({pypi_link})"
lines.append(f"| {package} | `{old_version}` | `{new_version}` |")
with pr_message.open(mode="a") as file:
file.write("\n".join(lines))
@nox.session
def requirements(session: nox.Session) -> None:
"""
Regenerate the pinned requirements for running tests and building
documentation.
This workflow updates :file:`uv.lock` to contain pinned requirements
for different versions of Python, different operating systems, and
different dependency sets (i.e., `docs` or `tests`).
When run in CI, this session will create a file that contains the
pull request message for the GitHub workflow that updates the pinned
requirements (:file:`.github/workflows/update-pinned-reqs.yml`).
"""
session.install(uv_requirement)
uv_lock_upgrade = ["uv", "lock", "--upgrade", "--no-progress"]
# When silent is `True`, `session.run()` returns a multi-line string
# with the standard output and standard error.
uv_output: str | bool = session.run(*uv_lock_upgrade, silent=running_on_ci)
if running_on_ci:
session.log(uv_output)
_create_requirements_pr_message(uv_output=uv_output, session=session)
@nox.session
def validate_requirements(session: nox.Session) -> None:
"""
Verify that the requirements in :file:`uv.lock` are compatible
with the requirements in `pyproject.toml`.
"""
session.install(uv_requirement)
session.log(
"🛡 If this check fails, regenerate the pinned requirements in "
"`uv.lock` with `nox -s requirements`."
)
# Generate the cache without updating uv.lock by syncing the
# current environment. If there ends up being a `--dry-run` option
# for `uv sync`, we could probably use it here.
session.run("uv", "sync", "--frozen", "--all-extras", "--no-progress")
# Verify that uv.lock will be unchanged. Using --offline makes it
# so that only the information from the cache is used.
session.run("uv", "lock", "--check", "--offline", "--no-progress")
pytest_command: tuple[str, ...] = (
"pytest",
"--pyargs",
"--durations=5",
"--tb=short",
"-n=auto",
"--dist=loadfile",
)
with_doctests: tuple[str, ...] = ("--doctest-modules", "--doctest-continue-on-failure")
with_coverage: tuple[str, ...] = (
"--cov=plasmapy",
"--cov-report=xml",
"--cov-config=pyproject.toml",
"--cov-append",
"--cov-report",
"xml:coverage.xml",
)
skipslow: tuple[str, ...] = ("-m", "not slow")
test_specifiers: list = [
nox.param("run all tests", id="all"),
nox.param("skip slow tests", id="skipslow"),
nox.param("with code coverage", id="cov"),
nox.param("lowest-direct", id="lowest-direct"),
]
@nox.session(python=supported_python_versions)
@nox.parametrize("test_specifier", test_specifiers)
def tests(session: nox.Session, test_specifier: nox._parametrize.Param) -> None:
"""Run tests with pytest."""
session.install(uv_requirement)
options: list[str] = []
if test_specifier == "skip slow tests":
options += skipslow
if test_specifier == "with code coverage":
options += with_coverage
# Doctests are only run with the most recent versions of Python and
# other dependencies because there may be subtle differences in the
# output between different versions of Python, NumPy, and Astropy.
if session.python == maxpython and test_specifier not in {"lowest-direct", "cov"}:
options += with_doctests
if gh_token := os.getenv("GH_TOKEN"):
session.env["GH_TOKEN"] = gh_token
match test_specifier:
case "lowest-direct":
session.install(".[tests]", "--resolution=lowest-direct")
case _:
# From https://nox.thea.codes/en/stable/cookbook.html#using-a-lockfile
session.run_install(
*uv_sync,
"--extra=tests",
env={"UV_PROJECT_ENVIRONMENT": session.virtualenv.location},
)
session.run(*pytest_command, *options, *session.posargs)
@nox.session(python=maxpython)
@nox.parametrize(
["repository"],
[
nox.param("numpy", id="numpy"),
nox.param("https://github.com/astropy/astropy", id="astropy"),
nox.param("https://github.com/pydata/xarray", id="xarray"),
nox.param("https://github.com/lmfit/lmfit-py", id="lmfit"),
nox.param("https://github.com/pandas-dev/pandas", id="pandas"),
],
)
def run_tests_with_dev_version_of(session: nox.Session, repository: str) -> None:
"""
Run tests against the development branch of a dependency.
Running this session helps us catch problems resulting from breaking
changes in an upstream dependency before its official release.
"""
session.install(uv_requirement)
if repository == "numpy":
# From: https://numpy.org/doc/1.26/dev/depending_on_numpy.html
session.run_install(
"uv",
"pip",
"install",
"-U",
"--pre",
"--only-binary",
":all:",
"-i",
"https://pypi.anaconda.org/scientific-python-nightly-wheels/simple",
"numpy",
)
else:
session.install(f"git+{repository}")
session.install(".[tests]")
session.run(*pytest_command, *session.posargs)
if running_on_rtd:
rtd_output_path = pathlib.Path(os.environ.get("READTHEDOCS_OUTPUT")) / "html"
rtd_output_path.mkdir(parents=True, exist_ok=True)
doc_build_dir = str(rtd_output_path)
else:
doc_build_dir = "docs/_build/html"
sphinx_base_command: list[str] = [
"sphinx-build",
"docs/",
doc_build_dir,
"--nitpicky",
"--keep-going",
]
if not running_on_rtd:
sphinx_base_command.extend(
[
"--fail-on-warning",
"--quiet",
]
)
build_html: tuple[str, ...] = ("--builder", "html")
check_hyperlinks: tuple[str, ...] = ("--builder", "linkcheck")
doc_troubleshooting_message = """
📘 Tips for troubleshooting common documentation build failures are in
PlasmaPy's documentation guide at:
🔗 https://docs.plasmapy.org/en/latest/contributing/doc_guide.html#troubleshooting
"""
@nox.session(python=docpython)
def docs(session: nox.Session) -> None:
"""
Build documentation with Sphinx.
This session may require installation of pandoc and graphviz.
"""
if running_on_ci:
session.log(doc_troubleshooting_message)
session.install(uv_requirement)
session.run_install(
*uv_sync,
"--extra=docs",
env={"UV_PROJECT_ENVIRONMENT": session.virtualenv.location},
)
session.run(*sphinx_base_command, *build_html, *session.posargs)
landing_page = pathlib.Path(doc_build_dir) / "index.html"
if landing_page.exists():
session.log(f"The documentation may be previewed at {landing_page}")
else:
session.error(f"Documentation preview landing page not found: {landing_page}")
@nox.session(python=docpython)
@nox.parametrize(
["site", "repository"],
[
nox.param("github", "sphinx-doc/sphinx", id="sphinx"),
nox.param("github", "readthedocs/sphinx_rtd_theme", id="sphinx_rtd_theme"),
nox.param("github", "spatialaudio/nbsphinx", id="nbsphinx"),
],
)
def build_docs_with_dev_version_of(
session: nox.Session, site: str, repository: str
) -> None:
"""
Build documentation against the development branch of a dependency.
The purpose of this session is to catch bugs and breaking changes
so that they can be fixed or updated earlier rather than later.
"""
session.install(f"git+https://{site}.com/{repository}", ".[docs]")
session.run(*sphinx_base_command, *build_html, *session.posargs)
LINKCHECK_TROUBLESHOOTING = """
The Sphinx configuration variables `linkcheck_ignore` and
`linkcheck_allowed_redirects` in `docs/conf.py` can be used to specify
hyperlink patterns to be ignored along with allowed redirects. For more
information, see:
🔗 https://www.sphinx-doc.org/en/master/usage/configuration.html#confval-linkcheck_ignore
🔗 https://www.sphinx-doc.org/en/master/usage/configuration.html#confval-linkcheck_allowed_redirects
These variables are in the form of Python regular expressions:
🔗 https://docs.python.org/3/howto/regex.html
"""
@nox.session(python=docpython)
def linkcheck(session: nox.Session) -> None:
"""Check hyperlinks in documentation."""
if running_on_ci:
session.log(LINKCHECK_TROUBLESHOOTING)
session.install(uv_requirement)
session.run_install(
*uv_sync,
"--extra=docs",
env={"UV_PROJECT_ENVIRONMENT": session.virtualenv.location},
)
session.run(*sphinx_base_command, *check_hyperlinks, *session.posargs)
MYPY_TROUBLESHOOTING = """
🛡 To learn more about type hints, check out mypy's cheat sheet at:
https://mypy.readthedocs.io/en/stable/cheat_sheet_py3.html
For more details about specific mypy errors, go to:
🔗 https://mypy.readthedocs.io/en/stable/error_codes.html
🪧 Especially difficult errors can be ignored with an inline comment of
the form: `# type: ignore[error]`, where `error` is replaced with the
mypy error code. Please use sparingly!
🛠 To automatically add type hints for common patterns, run:
nox -s 'autotyping(safe)'
"""
@nox.session(python=maxpython)
def mypy(session: nox.Session) -> None:
"""Perform static type checking."""
session.install(uv_requirement)
session.run_install(
*uv_sync,
"--extra=tests",
env={"UV_PROJECT_ENVIRONMENT": session.virtualenv.location},
)
if running_on_ci:
session.log(MYPY_TROUBLESHOOTING)
MYPY_COMMAND: tuple[str, ...] = (
"mypy",
".",
"--install-types",
"--non-interactive",
"--show-error-context",
"--show-error-code-links",
"--pretty",
)
session.run(*MYPY_COMMAND, *session.posargs)
@nox.session(name="import")
def try_import(session: nox.Session) -> None:
"""Install PlasmaPy and import it."""
session.install(".")
session.run("python", "-c", "import plasmapy", *session.posargs)
@nox.session
def build(session: nox.Session) -> None:
"""Build & verify the source distribution and wheel."""
session.install("twine", "build")
build_command = ("python", "-m", "build")
session.run(*build_command, "--sdist")
session.run(*build_command, "--wheel")
session.run("twine", "check", "dist/*", *session.posargs)
AUTOTYPING_SAFE: tuple[str, ...] = (
"--none-return",
"--scalar-return",
"--annotate-magics",
)
AUTOTYPING_RISKY: tuple[str, ...] = (
*AUTOTYPING_SAFE,
"--bool-param",
"--int-param",
"--float-param",
"--str-param",
"--bytes-param",
"--annotate-imprecise-magics",
)
@nox.session
@nox.parametrize("draft", [nox.param(False, id="draft"), nox.param(True, id="final")])
def changelog(session: nox.Session, final: str) -> None:
"""
Build the changelog with towncrier.
- 'final': build the combined changelog for the release, and delete
the individual changelog entries in `changelog`.
- 'draft': print the draft changelog to standard output, without
writing to files
When executing this session, provide the version of the release, as
in this example:
nox -s 'changelog(final)' -- 2024.7.0
"""
if len(session.posargs) != 1:
raise TypeError(
"Please provide the version of PlasmaPy to be released "
"(i.e., `nox -s changelog -- 2024.9.0`"
)
source_directory = pathlib.Path("./changelog")
extraneous_files = source_directory.glob("changelog/*[0-9]*.*.rst?*")
if final and extraneous_files:
session.error(
"Please delete the following extraneous files before "
"proceeding, as the presence of these files may cause "
f"towncrier errors: {extraneous_files}"
)
version = session.posargs[0]
year_pattern = r"(202[4-9]|20[3-9][0-9]|2[1-9][0-9]{2}|[3-9][0-9]{3,})"
month_pattern = r"(1[0-2]|[1-9])"
patch_pattern = r"(0?[0-9]|[1-9][0-9])"
version_pattern = rf"^{year_pattern}\.{month_pattern}\.{patch_pattern}$"
if not re.match(version_pattern, version):
raise ValueError(
"Please provide a version of the form YYYY.M.PATCH, where "
"YYYY is the year past 2024, M is the one or two digit month, "
"and PATCH is a non-negative integer."
)
session.install(".", "towncrier")
options = ("--yes",) if final else ("--draft", "--keep")
session.run(
"towncrier",
"build",
"--config",
"pyproject.toml",
"--dir",
".",
"--version",
version,
*options,
)
if final:
original_file = pathlib.Path("./CHANGELOG.rst")
destination = pathlib.Path(f"./docs/changelog/{version}.rst")
original_file.rename(destination)
@nox.session
@nox.parametrize(
"options",
[
nox.param(AUTOTYPING_SAFE, id="safe"),
nox.param(AUTOTYPING_RISKY, id="aggressive"),
],
)
def autotyping(session: nox.Session, options: tuple[str, ...]) -> None:
"""
Automatically add type hints with autotyping.
The `safe` option generates very few incorrect type hints, and can
be used in CI. The `aggressive` option may add type hints that are
incorrect, so please perform a careful code review when using this
option.
To check specific files, pass them after a `--`, such as:
nox -s 'autotyping(safe)' -- noxfile.py
"""
session.install(".[tests,docs]", "autotyping", "typing_extensions")
DEFAULT_PATHS = ("src", "tests", "tools", "*.py", ".github", "docs/*.py")
paths = session.posargs or DEFAULT_PATHS
session.run("python", "-m", "autotyping", *options, *paths)
@nox.session
def monkeytype(session: nox.Session) -> None:
"""
Add type hints to a module based on variable types from running pytest.
Examples
--------
nox -s monkeytype -- plasmapy.particles.atomic
"""
if not session.posargs:
session.error(
"Please add at least one module using a command like: "
"`nox -s monkeytype -- plasmapy.particles.atomic`"
)
session.install(".[tests]")
session.install("MonkeyType", "pytest-monkeytype", "pre-commit")
database = pathlib.Path("./monkeytype.sqlite3")
if not database.exists():
session.log(f"File {database.absolute()} not found. Running MonkeyType.")
session.run("pytest", f"--monkeytype-output={database.absolute()}")
else:
session.log(f"File {database.absolute()} found.")
for module in session.posargs:
session.run("monkeytype", "apply", module)
session.run("pre-commit", "run", "ruff", "--all-files")
session.run("pre-commit", "run", "ruff-format", "--all-files")
session.log("Please inspect newly added type hints for correctness.")
session.log("Check new type hints with `nox -s mypy`.")
@nox.session
def cff(session: nox.Session) -> None:
"""Validate CITATION.cff against the metadata standard."""
session.install("cffconvert")
session.run("cffconvert", "--validate", *session.posargs)
@nox.session
def manifest(session: nox.Session) -> None:
"""
Check for missing files in MANIFEST.in.
When run outside of CI, this check may report files that were
locally created but not included in version control. These false
positives can be ignored by adding file patterns and paths to
`ignore` under `[tool.check-manifest]` in `pyproject.toml`.
"""
session.install("check-manifest")
session.run("check-manifest", *session.posargs)
@nox.session
def lint(session: nox.Session) -> None:
"""Run all pre-commit hooks on all files."""
session.install("pre-commit")
session.run(
"pre-commit",
"run",
"--all-files",
"--show-diff-on-failure",
*session.posargs,
)
# /// script
# dependencies = ["nox"]
# ///
if __name__ == "__main__":
nox.main()