Coverage for src/rhiza_task/config.py: 100%
187 statements
« prev ^ index » next coverage.py v7.15.4, created at 2026-11-30 09:28 +0000
« prev ^ index » next coverage.py v7.15.4, created at 2026-11-30 09:28 +0000
1"""Configuration, and the resolution order that replaces make's ``?=`` and ``+=``.
3The make layer builds its settings from three overlapping mechanisms: ``?=`` defaults in
4the fragment that owns a setting, ``+=`` accumulation from other fragments
5(``DEPTRY_FOLDERS``, ``LICENSE_IGNORE_PACKAGES``, ``RHIZA_CHECKS``), and a repo-owned
6Makefile or ``local.mk`` assigning over the top. The precedence is a consequence of
7include order, which is why rhiza.mk has to explain that ``-include .rhiza/make.d/*.mk``
8comes last and ``-include local.mk`` last of all.
10Here the order is explicit and testable, lowest precedence first:
121. The dataclass defaults below.
132. ``.rhiza/.env`` -- kept unchanged, because it is already the file consumers edit and
14 the reusable workflows read it too. Now a developer-local channel rather than a
15 committed one: rhiza no longer ships ``.rhiza/.gitignore``, whose entire content was
16 the ``!.env`` negation that kept this file tracked, so it falls under the shipped
17 ``.gitignore``'s ``.env`` rule and a CI checkout never contains it.
183. ``rhiza.toml`` -- the language-neutral settings file, and the only committed one a Go
19 module can have: it has no manifest to hide a table in. Read for every project, so a
20 polyglot repository has one place to look rather than one per layer.
214. ``[tool.rhiza-task]`` in the language manifest -- ``Cargo.toml``, then
22 ``pyproject.toml``. This is the new home for what used to require editing a synced
23 ``.mk`` file or shadowing a target. Cargo ignores unknown top-level tables, so the
24 table is as harmless there as it is in pyproject.
255. ``RHIZA_*`` (or bare make-style) environment variables.
266. Command-line flags.
28Layers 3 and 4 are two files rather than one because neither alone covers the three
29language layers: pyproject is Python-only, and a repo that already moved its settings
30there should not have to move them again. ``rhiza.toml`` ranks *below* the manifest so
31that adding it to a Python repo cannot silently outrank the table already there.
33The ``+=`` accumulators do not survive as a mechanism, and do not need to: every one of
34them was a bundle contributing something it owned, which the task body can now *derive*
35by asking whether the contributing task is registered. See ``tasks/python.py``'s ``deps``
36and ``license``.
38**This module used to be the one that ranks B on maintainability** -- see issues #153 and
39#156, which are worth reading together because the second corrects the first. The blocks
40that put it there are ``Config.load``'s six-layer walk and ``_coerce``'s dispatch on value
41shape, both flat by choice: the walk *is* the precedence order this docstring spends thirty
42lines explaining, and reading it as a sequence of ``raw.update`` calls is the point.
44#153 recorded that as deliberate and gated the rank so the figure was read back by something.
45#156 found the gate was measuring the wrong thing. radon's MI counts length and comments
46count as length, so **writing the note that explained the ceiling moved the figure down** --
471.78 points for 19 lines of prose, with no branch added or removed. Worse, MI's verdict did
48not agree with any measure of complexity: it ranked this module B and ``tasks/fences.py`` A,
49while fences.py carries *more* blocks at rank B or worse and the same total.
51So the ceiling is now an accumulation count -- blocks at CC >= 6, per module -- in
52``.github/scripts/accumulation_ceiling.py``, run from ``ci.yml``'s ``gates`` job. It measures
53what MI was standing in for and no amount of prose can move it. The rank of this module is no
54longer gated at all, which is the honest outcome: it was never the complexity signal it
55looked like.
56"""
58from __future__ import annotations
60import json
61import os
62import tomllib
63from collections.abc import Callable, Mapping, Sequence
64from dataclasses import dataclass, field, fields
65from pathlib import Path
66from typing import Any
68from dotenv import dotenv_values
70TYPECHECKERS = ("ty", "mypy", "both")
72LAYERS = ("python", "rust", "go")
73"""The language layers, in the order :func:`~rhiza_task.spec.lookup` tries them.
75Python first because it is the layer a polyglot repository is most likely to have grown
76*into* -- a crate or a module that acquires a pyproject has acquired a Python package, and
77the gates that package needs are the ones that would otherwise stop running.
78"""
80LAYER_MANIFESTS = {"python": "pyproject.toml", "rust": "Cargo.toml", "go": "go.mod"}
81"""What makes a repository a member of a layer.
83The make layer answered this at sync time -- exactly one of python.mk, rust.mk and go.mk
84was ever synced into a repo, and `rhiza.mk`'s ``-include`` did the rest. A pinned CLI
85carries all three, so the question moves to runtime, and the manifest is the honest
86answer: it is what the toolchain itself looks for.
87"""
89NEUTRAL_RHIZA_CHECKS = (
90 "pytest_rhiza.checks.test_readme",
91 "pytest_rhiza.checks.test_release_tags",
92 "pytest_rhiza.checks.test_readme_validation",
93)
94"""The checks every repository gets, whatever it is written in."""
96LAYER_RHIZA_CHECKS = {
97 "python": ("pytest_rhiza.checks.test_pyproject", "pytest_rhiza.checks.test_docstrings"),
98 "rust": ("pytest_rhiza.checks.test_cargo_toml",),
99 "go": ("pytest_rhiza.checks.test_go_module",),
100}
101"""What each layer contributes, enumerated rather than globbed.
103pytest-rhiza ships all three layers' modules in one distribution, so
104``--pyargs pytest_rhiza.checks`` would collect checks that cannot pass -- ``test_go_module``
105against a Python project asserts a ``go.mod`` that is not there. In the make layer each
106language fragment appended its own with ``RHIZA_CHECKS +=``; here the accumulator is
107replaced by the same derivation the ``+=`` was standing in for, from the layer set rather
108than from include order.
109"""
111DEFAULT_RHIZA_CHECKS = NEUTRAL_RHIZA_CHECKS + LAYER_RHIZA_CHECKS["python"]
112"""The Python resolution, kept as a name because it is the set consumers know.
114This is jointview's ``RHIZA_CHECKS`` list, promoted from a shadowed make variable to a
115default: the 60-line override in its Makefile exists only because the make layer had
116nowhere else to put it.
117"""
120def rhiza_checks_for(layers: Sequence[str]) -> tuple[str, ...]:
121 """Return the check set for a repository's layers.
123 Args:
124 layers: The active layers.
126 Returns:
127 The neutral checks followed by each layer's own, in layer order, deduplicated.
128 """
129 checks = list(NEUTRAL_RHIZA_CHECKS)
130 for layer in layers:
131 checks += [c for c in LAYER_RHIZA_CHECKS.get(layer, ()) if c not in checks]
132 return tuple(checks)
135def detect_layers(root: Path) -> tuple[str, ...]:
136 """Return the language layers a repository belongs to, by its manifests.
138 Args:
139 root: Repository root.
141 Returns:
142 The layers whose manifest is present, in :data:`LAYERS` order; ``("python",)``
143 when a repository has none, because that is what every gate assumed before there
144 was a choice, and a repo with no manifest at all has nothing for another layer's
145 gates to measure either.
146 """
147 found = tuple(layer for layer in LAYERS if (root / LAYER_MANIFESTS[layer]).is_file())
148 return found or ("python",)
151DEFAULT_CI_OS_MATRIX = ("ubuntu-latest",)
152"""The OS every consumer gets unless it asks for more.
154Named rather than inlined because two callers need the same value: the field default
155below, and the floor in ``rhiza-task ci-os-matrix`` that stops an explicitly empty
156setting reaching GitHub as a zero-job matrix.
157"""
160@dataclass
161class Config:
162 """Resolved settings for one repository.
164 Field names are the lowercased make variables, so the mapping to what a consumer
165 already knows stays one-to-one and greppable.
166 """
168 source_folder: str = "src"
169 tests_folder: str = "tests"
170 # The prose documentation tree, which ``docs-examples`` checks the fenced examples in.
171 # A setting rather than a literal ``docs`` for the reason every other folder here is
172 # one: ``marimo_folder`` and ``paper_folder`` already default to paths *inside* it, so a
173 # repo that keeps its documentation somewhere else would otherwise have to move two
174 # settings and hardcode the third.
175 docs_folder: str = "docs"
176 marimo_folder: str = "docs/notebooks"
177 book_output: str = "_book"
178 python_version: str = "3.13"
180 coverage_fail_under: int = 90
182 # The floor ``docs-coverage`` hands interrogate. 100 because a docstring is present or
183 # it is not -- unlike a line of code, which can be covered by a test that asserts
184 # nothing -- so there is no honest reason to aim lower in a repo starting from scratch.
185 #
186 # A setting rather than the literal it was, because the repos adopting this are not
187 # starting from scratch. A codebase arriving at 89% has one gate it cannot pass and no
188 # way to say so, and the two answers left were both worse than a number: write
189 # docstrings for every nested helper in one sitting, or shadow the whole recipe in
190 # ``local.mk`` -- which is CI-invisible, so the gate then passes locally and fails in
191 # CI. Both were observed on the same upgrade; see jebel-quant/rhiza#1654.
192 #
193 # Lowering it is a ratchet to raise again, not a destination. The default is unchanged,
194 # so a repo that says nothing is gated exactly as before.
195 docs_coverage_fail_under: int = 100
197 # The ceiling the ``complexity`` gate enforces, as radon's own cyclomatic-complexity
198 # number rather than its A-F rank. A number and not a rank because rank C spans 11-20,
199 # which is too coarse to hold a decision: this repository's four deliberate C blocks
200 # sit at 12-14, and a gate that accepted the whole of C would accept 20 without anyone
201 # choosing it. 15 is what src/rhiza_task/config.py's own note commits to.
202 #
203 # Above the default rather than at it is the normal case for a consumer: 15 is a
204 # ceiling for a codebase that already argues its C blocks in comments, and a repo that
205 # does not should either raise it or not run the gate.
206 complexity_max: int = 15
208 # The per-test timeout, in seconds, that ``test``, ``coverage`` and ``test-lowest`` pass as
209 # ``--timeout``. It lived in the template's pytest.ini as ``timeout = 60`` until
210 # jebel-quant/rhiza#1832, and that was the wrong home: the key means something only to
211 # pytest-timeout, which only these tasks inject, so a bare ``uv run pytest`` warned about
212 # an unknown option and ran with no timeout -- and the ini could never adopt
213 # ``--strict-config``, which turns that warning into an error. The flag goes where the
214 # plugin is provisioned. 0 disables it, which is pytest-timeout's own meaning for 0.
215 test_timeout: int = 60
217 # The ref ``update`` moves .rhiza/template.yml to, e.g. ``v1.8.0``. Empty means "sync at
218 # the ref the file already names", which is the re-sync case rather than the upgrade one.
219 #
220 # A setting and not a flag because a task body takes a Config and nothing else -- see
221 # spec.Task.run. That is not a workaround: the environment layer already spells it
222 # (``TEMPLATE_REF=v1.8.0 rhiza-task update``), and a repo pinning its next target in
223 # pyproject.toml is a use the flag would not have served.
224 template_ref: str = ""
226 # ty | mypy | both. python.mk documents that ``both`` masks ty's exit status behind
227 # mypy's, and jointview sets ``ty`` in .rhiza/.env for that reason. The shell ``case``
228 # whose fourth branch validated this is replaced by __post_init__, so a typo now fails
229 # before any tool is provisioned rather than after.
230 typechecker: str = "ty"
232 # What ``typecheck`` provisions each checker *as*: appended to the checker's own name, so
233 # `==0.0.78` becomes `--with ty==0.0.78`. The shape `zensical_version` already uses, and
234 # an empty value means the bare name -- whatever is newest, which is what both settings
235 # replace.
236 #
237 # ty is pinned and mypy is not, and the asymmetry belongs to the checkers rather than to
238 # this package. ty is 0.0.x and its diagnostics change between patch releases, so an
239 # unpinned `--with ty` makes the verdict a fact about the day the job ran: a consumer
240 # migrating to this CLI saw one warning locally, where the project's lock supplied ty
241 # 0.0.18 for `--with ty` to reuse, and twenty-four errors on a runner that had no ty to
242 # reuse -- same commit, different checker. mypy is post-1.0 and moves slowly enough that
243 # the same pin would mostly buy staleness. See jebel-quant/rhiza-task#170.
244 #
245 # Moving the pin is a deliberate edit here, like the `pytest-benchmark` pin in
246 # tasks/extras.py: both are places where the tool's *output* is the gate's meaning, so
247 # what version produced it is part of the gate. A consumer wanting a different ty sets
248 # this rather than forking the task; one wanting the newest sets it empty, which is a
249 # manifest-only spelling for the reason `pytest_rhiza` above documents -- `_from_environ`
250 # and `_from_env_file` drop an empty value as unset, and only TOML tells an empty string
251 # from an absent key.
252 ty_version: str = "==0.0.78"
253 mypy_version: str = ""
255 # Matched as substrings -- see ``--partial-match`` in the ``license`` task.
256 license_fail_on: tuple[str, ...] = ("GPL", "LGPL", "AGPL")
257 license_ignore_packages: tuple[str, ...] = ()
259 deptry_ignore: tuple[str, ...] = ()
261 # rust.mk's CARGO_FLAGS and go.mk's GO_FLAGS / GO_TEST_FLAGS. `-race` and `-shuffle=on`
262 # are go.mk's default: the Go idiom for a CI run, and the flag that catches tests
263 # depending on declaration order.
264 cargo_flags: tuple[str, ...] = ()
265 go_flags: tuple[str, ...] = ()
266 go_test_flags: tuple[str, ...] = ("-race", "-shuffle=on")
267 # Non-empty on purpose, and it pairs with ``zensical_version`` below: this setting is
268 # the other half of "what the book gate provisions", and only one half was ever set.
269 #
270 # rhiza's ``book`` bundle ships ``docs/mkdocs-base.yml`` with ``mkdocstrings`` enabled
271 # unconditionally, and every consumer inherits it via ``INHERIT``. With this empty,
272 # ``book`` invoked ``uvx zensical build`` with no ``--with``, and zensical refused:
273 #
274 # Error: mkdocstrings plugin is enabled, but mkdocstrings is not installed.
275 #
276 # So the bundle shipped a config that could not build with its own default. The default
277 # belongs here rather than in the bundle because a bundle has nowhere to put it: core
278 # ships no ``.rhiza/.env`` (jebel-quant/rhiza#1545 deleted it), ``pyproject.toml`` is
279 # repo-owned, and ``_from_manifest`` reads only pyproject/Cargo -- so a ``book`` +
280 # ``go-core`` repo has no TOML surface at all. This is the one layer every consumer has.
281 #
282 # A repo that wants no plugins sets ``mkdocs-extra-packages = []`` in its manifest.
283 # That is TOML-only, deliberately -- see :func:`_from_env_file`.
284 mkdocs_extra_packages: tuple[str, ...] = ("mkdocstrings[python]",)
286 # The five bundle-owned fragments' settings. docker.mk's DOCKER_FOLDER was a `:=`
287 # rather than a `?=` -- not configurable at all -- and paper.mk hard-coded the
288 # PRESENTATION.md equivalent; both are ordinary settings here, because there is no
289 # longer a cost to making one.
290 docker_folder: str = "docker"
291 # Empty rather than a computed default: docker.mk's `?= $(shell basename $(CURDIR))`
292 # cannot be spelled as a dataclass default, and resolving it in the task body keeps
293 # `rhiza-task print docker_image` honest about the setting being unset.
294 docker_image: str = ""
295 paper_folder: str = "docs/paper"
296 presentation_file: str = "PRESENTATION.md"
297 # Unpinned, because `npm install -g @marp-team/marp-cli` was too: presentation.mk
298 # installed whatever latest resolved to. Set it to `@marp-team/marp-cli@4.2.3` to pin.
299 marp_package: str = "@marp-team/marp-cli"
300 zensical_version: str = ">=0.0.36"
301 uv_sync_args: tuple[str, ...] = ("--all-extras", "--all-groups")
302 ci_os_matrix: tuple[str, ...] = DEFAULT_CI_OS_MATRIX
304 # Whether rhiza's reusable book workflow publishes to GitHub Pages. No task here reads
305 # it -- `ci_os_matrix` already had that shape, and for the same reason: a workflow needs
306 # a *repo-owned* answer, and this is the only config surface every consumer has.
307 #
308 # The alternative was the one it replaces. `rhiza_book.yml` takes a `deploy-pages`
309 # input, so the choice could only be spelled in the caller -- which for a rhiza consumer
310 # is the `github-book` bundle's workflow stub, a template-owned file. Editing it works,
311 # is reviewed, is merged, and is overwritten by the next sync; rhiza-hooks'
312 # `check-managed-files` now refuses the commit outright. So the toggle existed and had
313 # nowhere to live short of excluding the file from management.
314 #
315 # Defaults true, which is what the input already defaulted to: a repo that says nothing
316 # deploys exactly as before.
317 deploy_pages: bool = True
319 # Pinned to a tag rather than a branch: a gate that moves under you is not a gate.
320 #
321 # Set it **empty** and `rhiza-test` passes no `--with` at all, resolving pytest-rhiza
322 # from the project environment instead -- the way to try an unreleased check against a
323 # real subject without publishing one first. Like `mkdocs_extra_packages` above, that is
324 # a manifest-only spelling: `_from_environ` and `_from_env_file` drop an empty value as
325 # unset, and only TOML tells an empty string from an absent key. See
326 # `tasks/quality.py`'s `_provider`, which also records why `"."` is not the shorthand it
327 # looks like.
328 pytest_rhiza: str = "pytest-rhiza @ git+https://github.com/Jebel-Quant/pytest-rhiza@v0.7.0"
330 # Both are empty by default and filled in `_validate_layers`, because both depend on
331 # the repository rather than on a constant: the layers come from the manifests present,
332 # and the check set follows from the layers. Setting either explicitly -- in
333 # pyproject.toml, or RHIZA_LAYERS=rust -- switches detection off for that field, which
334 # is what a repository carrying two manifests and wanting one gate set needs.
335 layers: tuple[str, ...] = ()
336 rhiza_checks: tuple[str, ...] = ()
338 # Turns Skip into failure. The answer to jointview's own complaint about "a green gate
339 # measuring nothing": set it in CI and a missing folder is a red build rather than a
340 # yellow line nobody reads.
341 strict: bool = False
343 root: Path = field(default_factory=Path.cwd)
345 # A flat sequence of calls, one per group of settings, and so A (1). It was C (13) --
346 # one branch per validated field -- carrying a comment that named **C (15)** as the
347 # point where the flat form stopped paying for itself, and per-group helpers as the
348 # answer. #124 honoured that ceiling at two branches of headroom instead of waiting for
349 # `rhiza-task complexity` to report it, and the shape below is the one that comment
350 # named: the readable one-field-per-step order survives, each step just bounded.
351 #
352 # The helpers are **methods**, which the `Guard`/`_clauses` precedent would have argued
353 # against -- on the grounds that radon scores a class as the *sum* of its methods, so a
354 # new method relocates the figure rather than reducing it. That premise is wrong: radon
355 # scores a class as the **mean** of its methods, so a small method *lowers* it. A
356 # two-method probe under `uvx radon cc -s` is the check -- a lone method of 5 scores its
357 # class 6, and adding a second of 1 scores it 4 -- and `Config` here went B (6) -> A (4)
358 # rather than up. What survives of that precedent is the *other* half, which was never
359 # about radon: `_clauses` is a module-level generator because it needs no `self` and
360 # because laziness preserves the guards' evaluation order. These five need `self` and
361 # have no order to protect, so they are methods.
362 def __post_init__(self) -> None:
363 """Normalise the list fields, then validate the enumerated and numeric ones.
365 Each step is a helper, so this method's own branch count is zero and the ceiling the
366 history above describes no longer applies to it. A new validated setting adds a call
367 here and its branches to its own helper -- which is what makes "one branch per
368 validated setting" stop being an open-ended growth rule.
370 Raises:
371 ValueError: When ``typechecker`` is not one of ty, mypy, both, either of
372 ``coverage_fail_under`` and ``docs_coverage_fail_under`` is outside 0-100,
373 ``complexity_max`` is below 1, ``test_timeout`` is negative,
374 ``layers`` names a layer that does not exist, :attr:`root` is not an
375 existing directory, or a ``*_folder`` escapes it. Each helper below raises
376 for its own settings and documents the message it uses.
377 """
378 self._coerce_sequence_fields()
379 self._validate_layers()
380 self._validate_typechecker()
381 self._validate_coverage()
382 self._validate_complexity_max()
383 self._validate_test_timeout()
384 # Order matters, and only between these two: `_validate_folders` asks whether a
385 # setting escapes the root, which presupposes there is a root to escape.
386 self._validate_root()
387 self._validate_folders()
389 def _coerce_sequence_fields(self) -> None:
390 """Turn a ``str`` or ``list`` on a ``tuple[str, ...]`` field into a tuple.
392 :func:`_coerce` sees a string without knowing which field it is destined for, so it
393 can only recognise the two shapes that announce themselves -- a JSON array and a
394 ``;``-separated list. Everything else it leaves as a ``str``, and a ``str`` reaching
395 a ``tuple[str, ...]`` field is splatted one *character* per argument at the call
396 site: ``UV_SYNC_ARGS="--group test"`` became ``uv sync - - g r o u p``.
398 The field's type is known here and nowhere lower, so this is where a string becomes
399 a tuple. Splitting on whitespace is the make layer's own format -- python.mk
400 documented ``LICENSE_IGNORE_PACKAGES`` as space-separated and ``RHIZA_CHECKS``
401 accumulated space-separated module names -- so a ``.rhiza/.env`` written for make
402 keeps working, as does the ``UV_SYNC_ARGS`` that rhiza's synced
403 ``.devcontainer/bootstrap.sh`` exports.
404 """
405 for f in fields(self):
406 if str(f.type).replace(" ", "") != "tuple[str,...]":
407 continue
408 value = getattr(self, f.name)
409 if isinstance(value, str):
410 object.__setattr__(self, f.name, tuple(value.split()))
411 elif isinstance(value, list):
412 object.__setattr__(self, f.name, tuple(value))
414 def _validate_layers(self) -> None:
415 """Default :attr:`layers` and :attr:`rhiza_checks` from the repository, then check them.
417 Both are empty by default and filled here, because both depend on the repository
418 rather than on a constant. Setting either explicitly switches detection off for that
419 field, which is what a repository carrying two manifests and wanting one gate set
420 needs -- so the defaulting is conditional and the membership check is not.
422 Raises:
423 ValueError: When ``layers`` names a layer that does not exist.
424 """
425 if not self.layers:
426 object.__setattr__(self, "layers", detect_layers(self.root))
427 unknown = [layer for layer in self.layers if layer not in LAYERS]
428 if unknown:
429 msg = f"unknown layer(s) {', '.join(unknown)}; known: {', '.join(LAYERS)}"
430 raise ValueError(msg)
431 if not self.rhiza_checks:
432 object.__setattr__(self, "rhiza_checks", rhiza_checks_for(self.layers))
434 def _validate_typechecker(self) -> None:
435 """Reject a :attr:`typechecker` outside the enumerated set.
437 Raises:
438 ValueError: When ``typechecker`` is not one of ty, mypy, both.
439 """
440 if self.typechecker not in TYPECHECKERS:
441 msg = f"typechecker must be one of {', '.join(TYPECHECKERS)} (got {self.typechecker!r})"
442 raise ValueError(msg)
444 def _validate_coverage(self) -> None:
445 """Reject a coverage floor that is not a percentage.
447 Both floors are checked in one loop rather than in two helpers, because they are
448 the same validation and not merely a similar one: whatever a third percentage
449 setting turns out to be, it joins the tuple and adds no branch here. That is the
450 ceiling this helper commits to -- a name, never an ``if``.
452 Raises:
453 ValueError: When ``coverage_fail_under`` or ``docs_coverage_fail_under`` is
454 outside 0-100.
455 """
456 for name in ("coverage_fail_under", "docs_coverage_fail_under"):
457 value = getattr(self, name)
458 if not 0 <= int(value) <= 100:
459 msg = f"{name} must be a percentage (got {value!r})"
460 raise ValueError(msg)
462 def _validate_complexity_max(self) -> None:
463 """Reject a :attr:`complexity_max` below 1.
465 A ceiling of 0 would fail every block including the empty ones, which is a typo
466 rather than a very strict policy.
468 Raises:
469 ValueError: When ``complexity_max`` is below 1.
470 """
471 if int(self.complexity_max) < 1:
472 msg = f"complexity_max must be at least 1 (got {self.complexity_max!r})"
473 raise ValueError(msg)
475 def _validate_test_timeout(self) -> None:
476 """Reject a negative :attr:`test_timeout`.
478 0 is allowed and means no timeout, as it does to pytest-timeout. A negative value has
479 no meaning there, and is a typo rather than a policy.
481 Raises:
482 ValueError: When ``test_timeout`` is negative.
483 """
484 if int(self.test_timeout) < 0:
485 msg = f"test_timeout must be 0 (off) or a number of seconds (got {self.test_timeout!r})"
486 raise ValueError(msg)
488 def _validate_root(self) -> None:
489 """Reject a :attr:`root` that is not an existing directory.
491 Every other setting is validated here and this one was not, so a mistyped ``--root``
492 travelled all the way into a task body and surfaced as whatever the first tool did
493 with a working directory that is not there: ``FileNotFoundError`` from
494 ``subprocess._execute_child`` for a gate that shells out, ``NotADirectoryError`` from
495 ``Path._scandir`` for one that walks the tree. Both are tracebacks through a private
496 stdlib frame, and neither names the flag the user got wrong.
498 The two cases are separated because they are different mistakes: a path that is not
499 there is usually a typo, and a path that is a file is usually a missing ``dirname``.
501 Deliberately not folded into :meth:`_validate_folders`. That method asks whether a
502 *setting* escapes the root, which presupposes there is a root to escape -- so this
503 has to run first, and merging them would make one message answer two questions.
505 Raises:
506 ValueError: When ``root`` does not exist, or exists and is not a directory.
507 """
508 if not self.root.is_dir():
509 reason = "is not a directory" if self.root.exists() else "does not exist"
510 msg = f"root {str(self.root)!r} {reason}"
511 raise ValueError(msg)
513 def _validate_folders(self) -> None:
514 """Reject a ``*_folder`` setting that resolves outside :attr:`root`.
516 Not a sandbox: the folder settings arrive from ``.rhiza/.env``, ``rhiza.toml``, the
517 manifest and the environment, which sit at the same trust level as the code they
518 configure. It is the containment the enumerated fields above already get.
519 ``SOURCE_FOLDER=../../elsewhere`` silently points a gate at a different checkout,
520 and that is a typo far more often than an intention -- so it should say the field's
521 name rather than run and report on somebody else's tree.
523 Both sides are resolved because ``root`` itself is routinely a symlink -- macOS
524 ``/tmp``, and every :func:`tempfile.mkdtemp` under it -- and comparing a resolved
525 child against an unresolved parent would reject every folder in such a checkout.
527 Raises:
528 ValueError: When a folder setting escapes ``root``.
529 """
530 root = self.root.resolve()
531 for name, value in self.folders.items():
532 if not (root / value).resolve().is_relative_to(root):
533 msg = f"{name} must stay inside the repository root (got {value!r})"
534 raise ValueError(msg)
536 @property
537 def folders(self) -> dict[str, str]:
538 """Return the folder settings, for :meth:`~rhiza_task.spec.Guard.check`.
540 Returns:
541 Mapping of field name to configured relative path.
542 """
543 return {f.name: getattr(self, f.name) for f in fields(self) if f.name.endswith("_folder")}
545 def path(self, folder_field: str) -> Path:
546 """Resolve a folder field to an absolute path.
548 No containment check here: :meth:`_validate_folders` did it once at construction,
549 so every field this resolves is already known to stay under ``root``.
551 Args:
552 folder_field: A field name such as ``source_folder``.
554 Returns:
555 The absolute path.
556 """
557 # The annotation is load-bearing under `mypy --strict`: `getattr` is typed to return
558 # `Any`, `Path / Any` is `Any` too, and returning that from a `-> Path` function is
559 # what `no-any-return` reports. Naming the type here is also the honest spelling --
560 # every field this reaches ends in `_folder` and holds a `str`, which is the same
561 # assumption `folders` above encodes in its `dict[str, str]`.
562 value: str = getattr(self, folder_field)
563 return self.root / value
565 @staticmethod
566 def field_for(name: str) -> str:
567 """Normalise a make-style variable name to a field name.
569 Public because the spelling rule is not private to the layer readers below: the
570 ``print`` command has to answer for ``SOURCE_FOLDER`` exactly as ``.rhiza/.env``
571 does, and a second normaliser written against the same rule is a second thing to
572 keep in step. A caller outside this module asking "which field is this?" is asking
573 :class:`Config`, so it is spelled as a question :class:`Config` can be asked.
575 The ``RHIZA_`` prefix is optional, so it is stripped -- but only when what remains
576 is actually a field. Stripping unconditionally made ``RHIZA_CHECKS`` resolve to
577 the unknown field ``checks``, so the setting was silently dropped and
578 ``rhiza_checks`` was reachable from the environment only as ``RHIZA_RHIZA_CHECKS``.
579 Trying the whole name as a fallback fixes that without disturbing the fields whose
580 prefix *is* redundant: ``RHIZA_CI_OS_MATRIX`` still resolves to ``ci_os_matrix``,
581 and the doubled spelling keeps working for anyone who found it.
583 Args:
584 name: e.g. ``RHIZA_CI_OS_MATRIX``, ``SOURCE_FOLDER`` or ``rhiza-checks``.
586 Returns:
587 e.g. ``ci_os_matrix``, ``source_folder``, ``rhiza_checks``.
589 Examples:
590 >>> Config.field_for("SOURCE_FOLDER"), Config.field_for("rhiza-checks")
591 ('source_folder', 'rhiza_checks')
592 """
593 lowered = name.lower().replace("-", "_")
594 stripped = lowered.removeprefix("rhiza_")
595 if stripped in _FIELD_NAMES or lowered not in _FIELD_NAMES:
596 return stripped
597 return lowered
599 @classmethod
600 def load(cls, root: Path | None = None, **overrides: Any) -> Config:
601 """Build a config by walking the six layers in order.
603 Args:
604 root: Repository root; defaults to the current directory.
605 **overrides: Layer 5, the command-line flags. ``None`` values are ignored so
606 an unset flag does not shadow a configured value.
608 Returns:
609 The resolved config.
611 Examples:
612 Layer 4 -- ``[tool.rhiza-task]`` in the manifest -- over the dataclass
613 defaults, with an unset flag passed as ``None`` and correctly *not* shadowing
614 what the manifest said:
616 >>> import tempfile
617 >>> from pathlib import Path
618 >>> manifest = '''
619 ... [tool.rhiza-task]
620 ... source_folder = "lib"
621 ... coverage_fail_under = 100
622 ... uv_sync_args = "--group test"
623 ... '''
624 >>> with tempfile.TemporaryDirectory() as tmp:
625 ... root = Path(tmp)
626 ... _ = (root / "pyproject.toml").write_text(manifest)
627 ... cfg = Config.load(root, source_folder=None, typechecker="mypy")
628 >>> cfg.source_folder, cfg.coverage_fail_under, cfg.typechecker
629 ('lib', 100, 'mypy')
631 A ``tuple[str, ...]`` field given as a string is split on whitespace rather
632 than one character per argument, which is the make layer's own format and the
633 bug ``__post_init__`` exists to prevent:
635 >>> cfg.uv_sync_args
636 ('--group', 'test')
638 The manifest that carried the table is also what put the repository in a
639 layer, and the check set follows from the layers rather than from a list
640 anyone maintains:
642 >>> cfg.layers
643 ('python',)
644 >>> cfg.rhiza_checks[-1]
645 'pytest_rhiza.checks.test_docstrings'
647 An unreadable setting fails here, before any tool is provisioned -- the shell
648 ``case`` that used to validate it ran after:
650 >>> with tempfile.TemporaryDirectory() as tmp:
651 ... Config.load(Path(tmp), typechecker="pyright")
652 Traceback (most recent call last):
653 ...
654 ValueError: typechecker must be one of ty, mypy, both (got 'pyright')
655 """
656 root = (root or Path.cwd()).absolute()
657 raw: dict[str, Any] = {}
658 raw.update(_from_env_file(root / ".rhiza" / ".env"))
659 raw.update(_from_rhiza_toml(root / "rhiza.toml"))
660 # Cargo before pyproject, so a repo carrying both -- a Rust crate with a Python
661 # binding package, say -- resolves to the same settings as the Python-only repo it
662 # grew out of, rather than to whichever manifest happened to be read last.
663 raw.update(_from_manifest(root / "Cargo.toml"))
664 raw.update(_from_manifest(root / "pyproject.toml"))
665 raw.update(_from_environ(os.environ))
666 raw.update({k: v for k, v in overrides.items() if v is not None})
668 # .python-version wins over a configured python_version for the reason python.mk
669 # reads it: it is what uv itself honours, so a second source of truth could only
670 # ever disagree.
671 pv = root / ".python-version"
672 if pv.is_file() and (text := pv.read_text().strip()):
673 raw["python_version"] = text
675 known = {f.name for f in fields(cls)} - {"root"}
676 return cls(root=root, **{k: v for k, v in raw.items() if k in known})
679_FIELD_NAMES = frozenset(f.name for f in fields(Config))
680"""Every field name, for :meth:`Config.field_for`. Built once rather than per variable."""
683def _from_env_file(path: Path) -> dict[str, Any]:
684 """Read ``.rhiza/.env``.
686 An empty assignment (``RHIZA_CI_OS_MATRIX=``) is dropped rather than carried as an
687 empty string, for the reason given in :func:`_from_environ`: that rule mirrors make's
688 ``?=``, which is what ``rhiza_ci.yml`` relies on when it exports the matrix empty for
689 every consumer so their own ``.rhiza/.env`` can answer.
691 **A consequence, now that one default is non-empty.** The rule used to be justified as
692 "no field's type has a meaningful empty value", and that stopped being true when
693 :attr:`Config.mkdocs_extra_packages` gained a default: for it, empty means "install no
694 plugins", which is a real choice rather than an absent one. It is expressible as
695 ``mkdocs-extra-packages = []`` in a manifest, where TOML distinguishes an empty array
696 from an absent key -- and *not* here or in the environment, where both arrive as the
697 same empty string. The behaviour is unchanged and deliberate: the matrix case above
698 needs it, and a dotenv layer cannot tell the two apart. Only the reasoning is amended,
699 so a reader does not conclude from the old wording that ``[]`` is meaningless anywhere.
701 Args:
702 path: Path to the dotenv file.
704 Returns:
705 Parsed settings; empty when the file is absent.
706 """
707 if not path.is_file():
708 return {}
709 return {Config.field_for(k): _coerce(v) for k, v in dotenv_values(path).items() if v and v.strip()}
712def _from_manifest(path: Path) -> dict[str, Any]:
713 """Read ``[tool.rhiza-task]`` from a language manifest.
715 ``pyproject.toml`` and ``Cargo.toml`` alike: the table is namespaced under ``tool``,
716 which cargo ignores as readily as any Python build backend does, so one reader serves
717 both and a Rust crate needs no file a Python project does not have.
719 Args:
720 path: Path to pyproject.toml or Cargo.toml.
722 Returns:
723 Parsed settings; empty when the file or the table is absent.
724 """
725 return _table(path, lambda data: data.get("tool", {}).get("rhiza-task", {}))
728def _from_rhiza_toml(path: Path) -> dict[str, Any]:
729 """Read ``rhiza.toml``, the manifest-free settings file.
731 Settings sit at the top level, because a file named after this tool has nothing to
732 namespace against. A ``[tool.rhiza-task]`` table is honoured too, and wins when both
733 are present: the pyproject spelling is what a reader will have seen first, and
734 silently ignoring it would be the worst of the three possible behaviours.
736 Args:
737 path: Path to rhiza.toml.
739 Returns:
740 Parsed settings; empty when the file is absent.
741 """
742 return _table(path, lambda data: data.get("tool", {}).get("rhiza-task") or data)
745def _table(path: Path, select: Callable[[dict[str, Any]], Any]) -> dict[str, Any]:
746 """Read a TOML file and normalise the selected table's keys and values.
748 Values here are already typed by TOML, so they bypass :func:`_coerce` -- a TOML array
749 arrives as a list and is tupled, nothing is parsed out of a string. Keys that are not
750 field names are left in place and dropped by :meth:`Config.load`, which is what makes
751 reading ``rhiza.toml``'s top level safe.
753 Args:
754 path: Path to the TOML file.
755 select: Picks the settings table out of the parsed document.
757 Returns:
758 Parsed settings; empty when the file is absent or unreadable.
760 Raises:
761 ValueError: When the file is not valid TOML. A settings file that does not parse
762 is a mistake worth reporting, not a file to skip -- unlike an absent one.
763 """
764 if not path.is_file():
765 return {}
766 try:
767 data = tomllib.loads(path.read_text())
768 except tomllib.TOMLDecodeError as exc:
769 msg = f"{path.name} is not valid TOML: {exc}"
770 raise ValueError(msg) from exc
771 table = select(data)
772 if not isinstance(table, dict):
773 return {}
774 return {
775 k.replace("-", "_"): tuple(v) if isinstance(v, list) else v for k, v in table.items() if not isinstance(v, dict)
776 }
779def _from_environ(environ: Mapping[str, str]) -> dict[str, Any]:
780 """Read settings from the process environment.
782 Both ``RHIZA_CI_OS_MATRIX`` and bare ``SOURCE_FOLDER`` are accepted, because the
783 reusable workflows currently pass bare make-style names on the command line and those
784 jobs must keep working through the transition.
786 **An empty value counts as unset.** This is not a nicety, it is the make semantics
787 this layer replaces. rhiza_ci.yml's ``generate-matrix`` job exports
789 RHIZA_CI_OS_MATRIX: ${{ github.repository == 'jebel-quant/rhiza'
790 && '["ubuntu-latest","macos-latest"]' || '' }}
792 -- one variable, set for the mother repo and *deliberately empty* for every consumer,
793 whose own ``.rhiza/.env`` is then meant to answer. make's ``?=`` treats an exported
794 empty string as set, which is why the retired ``ci-os-matrix`` recipe resolved through
795 ``$(or ...)``; dropping empties here is the same rule, applied one layer earlier so
796 every setting gets it rather than only the one whose recipe remembered to ask.
798 Args:
799 environ: The environment mapping.
801 Returns:
802 Parsed settings.
804 """
805 settings = ((Config.field_for(k), v) for k, v in environ.items())
806 return {k: _coerce(v) for k, v in settings if k in _FIELD_NAMES and v.strip()}
809def _coerce(value: str) -> Any:
810 """Turn a string setting into the field's type.
812 Three shapes exist in ``.rhiza/.env`` today and all must keep parsing: a JSON array
813 (``RHIZA_CI_OS_MATRIX=["ubuntu-latest","macos-latest"]``), a semicolon-separated list
814 (``LICENSE_FAIL_ON=GPL;LGPL;AGPL``), and a plain scalar.
816 Args:
817 value: The raw string.
819 Returns:
820 A ``str``, ``int``, ``bool`` or ``tuple[str, ...]``.
821 """
822 value = value.strip()
823 if value.startswith("["):
824 return tuple(json.loads(value))
825 if ";" in value:
826 return tuple(p for p in value.split(";") if p)
827 if value.lower() in {"true", "false"}:
828 return value.lower() == "true"
829 if value.lstrip("-").isdigit():
830 return int(value)
831 return value