Skip to main content

write.add_intent

write.add_intent(
    spec,
    unit,
    *,
    scope,
    invariant,
    reason,
    item_id=None,
    evaluations=(),
    decisions=(),
    dry_run=False,
)

Append one [[intent]] block to a unit record: the writer behind babregen add-intent.

The prose is given, never drafted: the loader rejects an empty required string, and a placeholder that passed it would pass in silence. Every evaluation must be defined in an evaluation file of the unit and every decision must exist; all wrong names are reported at once, each with a similar name when there is one.

Parameters

Name Type Description Default
spec RegenSpec The loaded spec. required
unit str A unit name or a unique suffix of it, as resolve_unit takes. required
scope str Where the requirement applies (prose). required
invariant str What must remain true, whatever the implementation (prose). required
reason str Why the invariant matters (prose). required
item_id str | None The id of the item. By default the dominant PREFIX- of the existing ids plus the highest number plus one; with no such item, the first three letters of the module name in upper case and -1. None
evaluations Sequence[str] Test functions in the evaluation files of the unit. ()
decisions Sequence[str] Ids of decision records. ()
dry_run bool Check everything and return the text, but write nothing. False

Returns

Name Type Description
Written Written The record, the appended text and the findings of check_unit for it.

Raises

Name Type Description
SpecError If the unit or an evaluation or a decision is unknown, the id exists, the record keeps its intent as an inline array, a prose value holds a blank line, a name holds a line break or the result would not load.

Examples

from pathlib import Path
from tempfile import TemporaryDirectory

from babregen import add_intent, load_spec, make_sample_repo

with TemporaryDirectory() as tmp:
    root = make_sample_repo(Path(tmp) / "demo")
    done = add_intent(
        load_spec(root),
        "core.calc",
        scope="add",
        invariant="add(0, x) == x",
        reason="zero is the identity",
        evaluations=["test_add"],
        dry_run=True,
    )
    print(done.text)

[[intent]]
id = "C-2"
scope = "add"
invariant = "add(0, x) == x"
reason = "zero is the identity"
evaluations = ["test_add"]