Skip to content

unresolved-guarded-read (MUT206)

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

Strict mode of a read-owners contract (unresolved = "report"): reading an attribute named like a target on an object of unknown class.

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

The strict mode of a [[tool.heki.read-owners]] contract that sets unresolved = "report": reports the reads outside the read owners that MUT203 cannot tie to a target class, so a clean MUT203 run says how much it did not check. A read of an attribute named like a target attribute (children for Reference.children; for Reference.*, a name Reference, a project subclass, or a project base gives its instances in project code, not one only a base outside the project gives them, configuration) is reported once, at the read, when its object is of unknown class: an unannotated parameter, a loop variable, the result of a call heki does not resolve, a value annotated Any or object (unless an isinstance test around the read narrows it to a class), a protocol whose implementations heki cannot list (one without members, or implemented by more than eight classes), or a variable of an enclosing function that a nested scope rebinds (nonlocal). obj.children, a local alias of it, getattr(obj, "children") with a constant name, and obj.__dict__["children"] (also vars(obj)["children"]) count, as for MUT203.

The rules of MUT203 for what is allowed apply: allowed readers, exempt methods, functions that only run inside allowed readers, forbidden-readers (the reading function must be one), and allowed-protocols, also for an object heki cannot tie to a class that is typed with a named protocol declaring the attribute: a class object typed type[P] (a parameter, a local, or an attribute annotated so), or an instance of a protocol whose implementations heki cannot list. MUT203 does not follow a read into a parameter that its function does not type, so the callers decide for such a parameter: the read is reported unless every caller path binds an instance of a project class outside the targets' classes to it. A parameter some caller binds to an instance of a target class is reported here: that read is a read of the target that no other rule sees. With attribution = "lexical", a function that only runs inside allowed readers is not allowed, and a caller path through an allowed reader counts like any other: the read is the function's own, and its callers only tell the object's class.

The help names up to three allowed readers that read a target of the attribute's name themselves, as for MUT203, after the contract's message: allowed readers of `children`: ....

What a report states

A report states that, outside the read owners, the function reads an attribute named like a target of the read-owners contract on an object whose class heki does not know.

Its premises:

  • Named like a target: the attribute's name matches a target's. Established.
  • Unknown class: heki did not resolve the class of the object. Established: the claim is about heki's own resolution.
  • Outside the owners: the function is not in the allowed modules and classes, nor runs only inside allowed contexts (with attribution = "lexical": the function holding the access is not allowed). Established from the contract.

Why is this bad?

MUT203 reports what it can back with a class: a read through an object it cannot type is not followed, so a contract that passes says nothing about those reads. Name-based checkers report them all; heki reports them separately, under their own code, so they can be counted, ratcheted with a baseline, and reduced by annotating the objects, without mixing them with the violations MUT203 proves.

Example

[[tool.heki.read-owners]]
name = "reference-children"
targets = ["Reference.children"]
allowed-readers = ["*GenerationIndex*"]
unresolved = "report"
def render(ref) -> str:  # no annotation: heki cannot tell what `ref` is
    return ", ".join(map(str, ref.children))  # MUT206


def size(ref: Reference) -> int:
    return len(ref.children)  # MUT203: a typed read

Use instead:

def render(ref: Reference, index: GenerationIndex) -> str:
    return ", ".join(map(str, index.children_of(ref)))

Fix safety

No automatic fix.

Options

  • [[tool.heki.read-owners]]:
  • unresolved: "ignore" (default) or "report".
  • targets, allowed-readers, forbidden-readers, exempt-methods, attribution, message: as for MUT203.