Skip to content

unresolved-guarded-call (MUT207)

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

Strict mode of a call-owners contract (unresolved = "report"): calling a method 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.call-owners]] contract that sets unresolved = "report": reports the calls outside the call owners that MUT204 cannot tie to a target, so a clean MUT204 run says how much it did not check. A call of a method named like a target (the last name of the target: replace_reference for DataType.replace_reference, also for a function or class target; for a glob, Parser._*, a name Parser, a project subclass, or a project base gives its instances in project code, not one only a base outside the project gives them: _replace of a NamedTuple is none, configuration) is reported once, at the call, when its receiver 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 call 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.replace_reference(...), a bound-method alias of it (replace = obj.replace_reference), and getattr(obj, "replace_reference")(...) with a constant name count. A call of a value of unknown origin (callback()) names nothing and is not reported.

The rules of MUT204 for what is allowed apply: allowed callers, exempt methods, the targets themselves (they may call each other), functions that only run inside allowed callers, forbidden-callers (the calling function must be one), and allowed-protocols, also for a receiver heki cannot tie to a class that is typed with a named protocol declaring the method: a class object typed type[P] (cls._items_constraints(obj) with cls: type[KeywordsHost]), or an instance of a protocol whose implementations heki cannot list. MUT204 does not follow a call into a parameter that its function does not type, so the callers decide for such a receiver: the call is reported unless every caller path binds to it an instance of a project class whose method of that name is no target: a class outside the target's family (DataType and its subclasses for DataType.replace_reference) whose MRO finds no method the target names either (for the target reset, any project class's reset; a reset inherited from a library base is none). With attribution = "lexical", a function that only runs inside allowed callers is not allowed, and a caller path through an allowed caller counts like any other: the call is the function's own, and its callers only tell the receiver's class.

The help names up to three allowed callers that call a target of the method's name themselves, as for MUT204, after the contract's message: allowed callers of `replace_reference`: ....

What a report states

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

Its premises:

  • Named like a target: the method's name matches a target's (for a target with an argument list, its name before the list: heki does not bind the arguments of a call on an object of unknown class). 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?

MUT204 reports a call only when every function it may call is a target, which needs the receiver's class: a call on an object it cannot type is not seen, so a contract that passes says nothing about those calls. 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 receivers, without mixing them with the violations MUT204 proves.

Example

[[tool.heki.call-owners]]
name = "reference-mutators"
targets = ["DataType.replace_reference"]
allowed-callers = ["pkg.store.GenerationStore.*"]
unresolved = "report"
def swap(data_type, ref) -> None:  # no annotation: heki cannot tell what `data_type` is
    data_type.replace_reference(ref)  # MUT207


def rename(text: str) -> str:
    return text.replace("a", "b")  # not reported: a `str`

Use instead:

def swap(store: GenerationStore, data_type: DataType, ref: Reference) -> None:
    store.replace_data_type_ref(data_type, ref)

Fix safety

No automatic fix.

Options

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