Skip to content

write-without-invalidation (MUT208)

Family: Mutation & State (MUT) · Heuristic · Runs when its contract is declared · Severity error (a bug) · No automatic fix

Writing a declared invalidation target (directly, via alias, or via helper) without reaching one of its invalidators on every path to the function's return.

Heuristic

This rule is heuristic: a premise of its claim can be open ("What a report states" lists them), so a report can be wrong about the code. It runs when its contract is declared. It has not been measured yet. See Rule Precision.

What it does

Checks writes to attributes declared in an [[tool.heki.invalidation]] contract: after the write, every path to the function's normal return must call one of the contract's invalidators (the call that announces the change to whatever derives facts from the attribute: an index, a cache, a revision counter).

A write is followed through local aliases, container methods (.append(), [k] = v, and a project method changing its own self in place called on the attribute's value), setattr / __dict__, and helpers: a private helper that writes without announcing the change passes the obligation to its callers, and the call of such a helper must be announced like a direct write (one report at the call stands for every write behind it, and a Ratchet baseline counts each of them: sites in JSON output). A private generator's writes run where it is iterated, or inside the function it is handed to that iterates it, and are reported there. A helper changing an argument it does not type (_add(xs): xs.append(1) called as _add(model.items), _set(o): o.x = 1 called as _set(model)) writes the field or instance the caller binds to it: the caller's call is the write, once for each class the instance may be (a protocol-typed one is the protocol and each implementation), against that class's targets. An invalidator counts when it is called directly, in each branch, or through calls (a helper that calls an invalidator on every path to its own return), also behind a boolean flag set after the write (changed = True ... if changed: notify()); one called before the write does not count, nor an async invalidator that is not awaited. A call of a method announces the change when every implementation it may run does (the definition the receiver's class finds and the overrides of its subclasses; an @abstractmethod never runs): calling hook.done() on a Hook whose own done does nothing announces nothing, whatever a subclass does.

It does not report:

  • a function named in exempt-writers (the owner's operations announce or batch the change themselves), nor calls from one;
  • writes to objects that did not exist before the call: the class's own initialization (an initializer writing its own self, directly or through a helper it hands self to; an object reached through self's attributes, such as a model it was given, existed before) and objects the function constructs or copies, which nothing has derived facts from yet; objects it gets from a call are left alone too, since heki cannot tell whether they are new;
  • a function whose every caller announces the change after calling it (invalidation in bulk by a caller, at any distance);
  • paths that leave the function by raising: exception edges are not followed and only normal returns count.

The help starts with the contract's message and then names up to three announcing writers of the target the report names: public functions and methods that write it themselves and announce every write of it, their own and their helpers', on every path, and exempt-writers that write it themselves, by fully qualified name (announcing writers of `DataModel.fields`: `pkg.passes.narrow`). Private functions (_name) and special methods do not count. JSON output gives them with their files as allowed_examples (fqn, path).

What a report states

A report states that, after a write to a target of an invalidation contract, some path to the function's normal return calls none of the contract's invalidators.

Its premises:

  • Target: the object is an instance of a class the contract names, or of a subclass, by construction or annotation (assumption R2). Open: an alias bound on one path is enough.
  • Writes: a store, a container method, setattr / __dict__, or a helper's write. Open through a helper: a write on one of its paths is enough.
  • Not announced: some path from the write to the normal return reaches no invalidator. Established on the function's control flow.

Why is this bad?

Code that keeps derived facts (an index of a model graph, a memo, a cached rendering) relies on every writer announcing its change. A writer that forgets it on one path leaves the derived facts stale: nothing fails at the write, and the wrong value shows up later, on an input that takes that path.

Example

[[tool.heki.invalidation]]
name = "graph-index"
targets = ["DataType.data_types", "DataModel.fields"]
invalidators = ["GenerationStore.note_changed_models", "GenerationStore.refresh_now"]
exempt-writers = ["pkg.parser.generation.*"]
message = "tell the store which models changed"
def narrow(store: GenerationStore, owner: DataModel, node: DataType, item: DataType) -> None:
    node.data_types = [item]  # MUT208: the path where `owner.fields` is empty tells nobody
    if owner.fields:
        store.note_changed_models(owner)

Use instead:

def narrow(store: GenerationStore, owner: DataModel, node: DataType, item: DataType) -> None:
    node.data_types = [item]
    store.note_changed_models(owner)

Known limitations

  • Deferred modes: a context manager whose exit announces the change (with store.defer_refresh(): ...) is not followed. List such deferred writers in exempt-writers, or call the invalidator.
  • A generator a function returns is iterated by its callers: its writes are not followed there. A helper that announces the change counts when its body runs at its call; a generator may be iterated only in part, so an invalidator inside one does not count.
  • Two different helpers that each announce the change on one branch are not combined (two direct invalidator calls are); a helper counts when it announces the change on all of its own paths.
  • A branch is read as two feasible paths unless its condition is a constant or a boolean flag the path sets (changed = True after the write, then if changed: notify()): a helper whose result says whether it wrote (if model.unbind(names): notify()) is not followed.
  • Callers heki cannot resolve (a function passed as a callable, a method of a class held in a variable) are not seen: a public function without visible callers announces its own changes or is listed in exempt-writers, like a construction method; a private helper without visible callers is not checked.

Fix safety

No automatic fix: where to announce the change is a design decision.

Options

  • [[tool.heki.invalidation]]: name, targets (Class.attr, subclasses included), invalidators (functions or methods), exempt-writers, message. The diagnostic target is <name>:<target> (graph-index:DataModel.fields): two contracts naming one write are two reports.