State Ownership and Mutation¶
heki finds in-place changes of read-only parameters, cached results, and frozen fields, and enforces your own rules about who may change, read, or call what.
Python has no owner for a value: any code that reaches an object can change it. Type checkers see a change made on a value of a read-only type. heki follows the value through aliases and helper calls, so it also sees the change that a function three calls below makes. This page has three parts: who may change, read, or call what, how to leave a borrowed or shared value alone, and values that may be used only once. A table at the end maps the ideas to the words Rust uses for them. Contracts has every key, and Rules has one page per rule.
Each entry names its rule and its tier, as heki rule --all gives it. An exact rule states a fact about the code. A heuristic rule can be wrong about the code, runs with --heuristic (or lint.heuristic = true) when it is on by default, and its reports count as warnings. An opt-in rule is off until you name it in extend-select, for example --extend-select MUT115; a prefix such as MUT or ALL never enables it. An experimental rule can change or go away between releases, and Experimental Features says why each one is marked.
📜 Who may change, read, or call what¶
Some rules belong to your project, not to Python: only app.store may append to Order.items, only app.billing reads a card token, nothing changes the settings object that load_settings returns. Write them down once and heki check enforces them across files, aliases, and helpers.
| You want | Contract | Rules | Tier |
|---|---|---|---|
| Only some functions change an attribute, a container, the items of a container class, or a name of a module | write-owners |
MUT201, MUT205 |
heuristic, contract |
| Only some functions read an attribute or a name of a module | read-owners |
MUT203, MUT206 |
heuristic, contract |
| Only some functions call a function, method, or class | call-owners |
MUT204, MUT207 |
heuristic, contract |
| Nobody changes a shared value that a producer returns | readonly-returns |
MUT202 |
heuristic, contract |
| Every change of an attribute is followed by a call that announces it | invalidation |
MUT208 |
heuristic, contract |
The second rule of each owner contract (MUT205, MUT206, MUT207) is its strict mode. It reports what heki cannot place: an attribute named like a target that is written on an object of unknown class. Set unresolved = "report" in the contract to get it.
A contract rule is on as soon as a contract table exists. Its reports are warnings until you set fail = true in the table. Record today's findings in a Ratchet baseline first, so only new breaks fail.
A contract that lists every allowed site itself can keep its reports out of the baseline with baseline = "never".
Each exception an allowed-* or exempt-* list grants can say why: list it in a group with a reason, and set allowance-reasons = "required" on the contract or its defaults table so that an entry without one is a configuration error. With baseline-exact = true, a baseline entry that can shrink fails the run, so a fixed break cannot come back unnoticed.
An exception outlives the code it allowed: a function that no longer writes the target keeps its place in allowed-writers, and a later write there passes unseen. Set unused-allowances = "warning" or "error" (opt-in, default "ignore") on a write-owners, read-owners, or call-owners contract or its defaults table, and heki check over the whole project reports each allowed-*, exempt-methods, and allowed-protocols entry that allowed no access of the targets.
An exception can also live in the code, one function at a time: exempt-decorators names decorators (a pytest marker such as pytest.mark.allow_fail) that exempt the functions they mark from the contract when they state a reason (@pytest.mark.allow_fail("the plugin under test calls fail")). A listed decorator without a reason exempts nothing and is reported. An invalidation contract takes the key too: a decorated function is an exempt writer. --report-suppressed lists what each decorator hid, and report-unused-suppressions reports a decorator that hid nothing.
Only the store changes the order¶
# heki.toml
[[write-owners]]
name = "order-store"
targets = ["app.models.Order.items"]
allowed-writers = ["app.store.*"]
message = "change Order.items through app.store"
fail = true
# app/store.py
from app.models import Order
def add_item(order: Order, item: str) -> None:
order.items.append(item)
app/checkout.py changes the list through a helper in another file. Every import here is allowed
by the import layers, and heki follows the write through the helper.
# app/checkout.py
from app.models import Order
from app.util import append_to
def add_gift_wrap(order: Order) -> None:
append_to(order.items, "gift-wrap")
$ heki check
app/checkout.py:7:5: MUT201 (write-owner-violation) [error, heuristic] `add_gift_wrap` writes `app.models.Order.items` (through a helper) via `app.util.append_to` outside the write owners of `order-store`
|
7 | append_to(order.items, "gift-wrap")
| ^
= related: app/checkout.py:7:5: `add_gift_wrap` calls `app.util.append_to`
= help: change Order.items through app.store; allowed writers of `app.models.Order.items`: `app.store.add_item`
Found 1 diagnostic(s) in 4 file(s).
The files pass Ruff, and mypy, Pyright, Pyrefly, and ty in strict mode; the run exits with code 1. read-owners and call-owners take the same shape with allowed-readers and allowed-callers. A target covers the subclasses of its class too, and a read or call through a value typed with a project Protocol that only the class and its subclasses implement: with Order the one class implementing HasItems, cart.items with cart: HasItems reads Order.items (read-owners). A class target of call-owners also covers the copies dataclasses.replace(order, ...) and copy.replace(order, ...) make of an instance of a stdlib dataclass (or a NamedTuple, for copy.replace), which call the class with the changed fields; where a class along the MRO defines its own __replace__, that method decides what the copy is, and the call is not reported (MUT204, heuristic, contract).
A call-owners target may also name a class outside the project. With references = "calls-and-bases" or "reads", a class statement naming it among its bases (class T(unittest.TestCase)) subclasses it, and, unless subclasses = false, so does one naming a project class deriving from it in any module (class T(Base) with class Base(unittest.TestCase)). A base heki cannot name, such as a parameter, is not reported (MUT204, heuristic, contract, opt-in through references; call-owners).
A call-owners target can end in an argument list and then names only the calls passing its constants (MUT204, heuristic, contract). An entry names a parameter (DataType(is_dict=True)), a position (0="#", the receiver of a method not counted), or is a bare value for the next position: builtins.str.split("#") names text.split("#"), text.split(sep="#"), and str.split(text, "#") alike, since the stubs declare sep at position 0, and *(0="#") names every call passing "#" first, whatever it calls. A function outside the project takes an argument list too (os.getenv("HOME")); where heki knows no signature, a position is the one written (call-owners).
A write-owners target can also name the items of a project class that derives from a builtin dict, list, or set: Imports[*] names every item of its instances and Imports["typing"] one entry. An item store or deletion, a mutating method or in-place operator the class inherits (imports |= other), and a change in place of an item are writes of them, also through an attribute that holds the instance and through a helper it is handed to (Items of a container class; MUT201, heuristic, contract). With unlisted-attributes = "report", the contract also reports a write of an attribute that no write-owners target names on an object of a class its targets cover, under the same allowed writers, and with unresolved = "report" such an attribute written on an object of unknown class, and an item stored or deleted on one (box["k"] = v), which may be one of the items (MUT205, heuristic, contract). With unlisted-attributes = "inventory" it also reports the attributes the class body declares and the class's own initializers set, so a table of every attribute of the contract classes can be checked for completeness. allowed-writers-marked-by and allowed-writers-decorated-by restrict the functions allowed-writers may name to the ones a list or a decorator marks, and lint.inherited-contract-entries reports a Class.attr entry declared only on a base class (Entries that match nothing).
A write-owners or read-owners target can also name a name of a module's namespace: app.api:run is the run that app.api binds, and app.api:* every name it binds. An assignment, del, setattr, or delattr through the module object, and a store in a function that declares the name global, write it; app.api.LIMIT, getattr(api, "LIMIT"), and an import of the name with each use of what it binds read it (Names of a module's namespace; MUT201, MUT203, heuristic, contract). A write-owners target also names an attribute of a module or class outside the project by its full name, such as sys.stdin, pathlib.Path.*, or the entries sys.modules["app.*"], so one contract keeps tests from patching them (Symbols outside the project). A target without a key, such as os.environ, counts every change of the mapping in place: item stores and del, os.environ.update(...), pop, setdefault, clear, and popitem, so targets = ["os.environ"] with allowed-writers = ["app.cli:main"] names the one function that sets environment variables. A patch call writes what it replaces, as the assignment it stands for: monkeypatch.setattr(api, "run", f), monkeypatch.setattr("pkg.api.run", f), mock.patch("pkg.api.run") in a with or as a decorator, mocker.patch.object(api.Client, "send"), monkeypatch.setitem(sys.modules, "pkg.api", m), monkeypatch.setenv("DEBUG", "1") (writes os.environ["DEBUG"]) (Patch calls; MUT201, heuristic, contract). A store or deletion through a project class object writes the class attribute the same way: Client.send = f, setattr(Client, "send", f), del Client.send, and delattr(Client, "send"), through an import or a local (MUT201, heuristic, contract).
A read-owners target names an attribute outside the project the same way (sys._getframe): every load of it heki resolves reads it, and a name loaded only to be called there is a call (MUT204). Where a library object serves its attributes through __getattr__ (heki's catalog lists them: pytest.mark, whose MarkGenerator makes a mark for any name not starting with an underscore), a name is read wherever it is loaded, also where it is called: @pytest.mark.skip(reason="x"), and @mark.skipif(...) after from pytest import mark, read pytest.mark.skip and pytest.mark.skipif, and the decorator call is a call of them too. A target without a glob that the analyzed files only call (pytest.skip) can never match a read: with unmatched-contract-entries = "error" it is reported, and the message says it names no read outside the project, only calls of it, and no library __getattr__ heki knows serves it. Tier: heuristic, contract (Symbols outside the project; MUT203).
Every change must be announced¶
# heki.toml
[[invalidation]]
name = "order-total"
targets = ["app.models.Order.items"]
invalidators = ["app.cache.order_changed"]
message = "call order_changed() after changing Order.items"
add appends and then calls order_changed(). remove does not:
$ heki check
app/cart.py:11:5: MUT208 (write-without-invalidation) [error, heuristic] `remove` writes `app.models.Order.items` (in place) and returns on some path without calling an invalidator of `order-total`
|
11 | order.items.remove(name)
| ^
= help: call order_changed() after changing Order.items; announcing writers of `app.models.Order.items`: `app.cart.add`
...
Without fail = true this run exits with code 0, because a heuristic report counts as a warning.
Which contract do I need?¶
| If the rule you have in mind says | Use |
|---|---|
| "only X may change this field" | write-owners |
| "only X may read this field" | read-owners |
| "only X may call this function" or "only X may construct this class" | call-owners |
| "nobody may change what this function returns" | readonly-returns, with producers = [...] and deep = true for what is below the result |
| "after changing this field, always call that function" | invalidation |
| "this code must not write, read, or call anything of that kind, however deep" | effect-boundaries, see Async and Execution Effects |
A contract names a method as Class.method, and a subclass that overrides the method is another function. Set include-overrides = true on a contract (or in its defaults table) and an entry naming a method in allowed-*, forbidden-*, exempt-methods, invalidators, or exempt-writers also names the overrides of it in the project subclasses of Class. The key is off by default because it allows a subclass written later to do what its base class may, with no change of the configuration. See Allowed and forbidden functions.
A value that an annotation declares as a union (Model | Node, Optional[...], a TypeAlias, a TypeVar bound) is each of its classes when it has at most eight, and a method call on it runs the method of each. A value that different assignments join keeps at most three classes. Past the limit the value has no class, and a write on it stays an unresolved write. A loop that writes the keys of a TypedDict (for name, value in td.items(): setattr(obj, name, value)) writes the keys the class declares only when the TypedDict is closed (closed=True, PEP 728, or a base that is closed). A TypedDict without it is open: a subclass value may be passed where it is declared, so the write names an attribute at run time, as for a plain dict.
lru_cache and cache results are protected without a contract. A readonly-returns entry adds the producers that are not cached, such as a function that returns a module-level dict.
🤝 Not breaking a value you borrowed or share¶
A function that receives a list did not make it, and a function that gets a cached result did not make that either. Changing such a value changes it for everyone who holds it. These rules report the change.
A read-only parameter changed by a helper¶
A parameter annotated Sequence, Mapping, or AbstractSet promises that the function only reads it. MUT110 reports a call that hands such a parameter, as itself, to a function that changes it in place. Tier: exact, default (MUT110).
# app/stock.py
from collections.abc import Sequence
def sort_names(names) -> None:
if isinstance(names, list):
names.sort()
def prepare(names: Sequence[str]) -> list[str]:
sort_names(names)
return list(names)
$ heki check
app/stock.py:10:5: MUT110 (readonly-param-mutation) [warning] `prepare` passes its read-only parameter `names` to `app.stock.sort_names`; whenever line 6 of `sort_names` runs, it changes the caller's object in place
|
10 | sort_names(names)
| ^
= related: app/stock.py:10:5: `prepare` calls `app.stock.sort_names`
= related: app/stock.py:6:9: `sort_names` changes the object in place here
= help: pass a copy (`list(names)` / `dict(names)` / `set(names)`), or annotate the helper's parameter with a mutable type
...
MUT110 is a warning, because the bug it reports is rarely harmful, so it does not fail the run. Copy the parameter to a local list, sort the copy, and return it, so the caller's object stays as it was:
def prepare(names: Sequence[str]) -> list[str]:
ordered = list(names)
sort_names(ordered)
return ordered
Or annotate the helper's parameter with a mutable type, so the type checker shows the mismatch at the call.
What a helper changes is the object the call hands it: a container method called on the class (list.append(items, x), dict.update(table, other)) changes its first argument, as items.append(x) does, and setattr(obj, name, value) with a name computed at run time changes obj. The dict **kwargs binds is new at each call, so kwargs.pop("key") or kwargs["key"] = value changes nothing of the caller's; a change of a value it holds (kwargs["items"].append(x)) changes the caller's value.
A cached result changed in place¶
A function under lru_cache or cache, or one that keeps its results in a memo dict it fills itself, hands the same list, dict, or set to every caller with the same arguments. MUT114 reports a change of that object. Tier: exact, default (MUT114).
# app/stock.py
from functools import lru_cache
@lru_cache(maxsize=None)
def default_tags(shop: str) -> list[str]:
return ["new", shop]
def tag(shop: str, item: str) -> list[str]:
tags = default_tags(shop)
tags.append(item)
return tags
$ heki check
app/stock.py:11:5: MUT114 (cached-result-mutation) [error] `tag` changes in place the list that `default_tags` returned; `default_tags` is cached by `functools.lru_cache`
|
11 | tags.append(item)
| ^
= help: change a copy (`list(...)`) instead
...
A function that keeps its results in a memo it fills itself is a cache too: it stores what it computes from its arguments in a dict that outlives the call and is named as a cache or memo (self._cache, _CACHE), and a later call with the same arguments returns what the dict keeps. MUT114 reports a change of that object as well.
# app/settings.py
_CACHE = {}
def load(name: str) -> dict:
if name not in _CACHE:
_CACHE[name] = {"name": name, "debug": False}
return _CACHE[name]
def debug_settings(name: str) -> dict:
settings = load(name)
settings["debug"] = True
return settings
$ heki check
app/settings.py:12:5: MUT114 (cached-result-mutation) [error] `debug_settings` changes in place the dict that `load` returned; `load` keeps it in `_CACHE`
|
12 | settings["debug"] = True
| ^
= help: change a copy (`dict(...)`) instead
...
Such a report is exact when every return of the memo function hands out what a builtin dict holds under one key when it returns, and its return annotation names list, dict, or set. It is heuristic when some return hands out a value the memo may not hold (a new object returned without storing it, a default of .get), removes or changes the entry before it returns, or keeps the memo in a class attribute. A memo that clears or drops the key before every read keeps nothing and gives no report. The name decides because the code does not: a get-or-create registry of the same shape whose dict is named otherwise (_sessions, _BY_NAME) is not taken for a cache.
An exact MUT114 report is an error, so it fails the run; a heuristic one fails it only under fail-on = "warning", a contract with fail = true, or MUT114 in the lint settings' fail list. A producer that is not cached, such as a function that returns a module-level dict it never fills itself, is covered by a readonly-returns contract, which also takes precedence over a memo. A function that returns such a producer's value on some paths and a cached value on others hands out both, and a change of it is reported under each rule.
A shared return value changed¶
For a value that is shared without a cache, name its producers in a readonly-returns contract, with deep = true to cover what is below the result. MUT202 reports a change of what a producer returned, directly or through the functions that return it. Tier: heuristic, contract (MUT202).
# app/m.py
from registry import schema
def schema_of(name: str) -> dict[str, list[str]]:
return schema(name)
def extend(name: str) -> None:
s = schema_of(name)
s["fields"].append("extra")
heki reports: extend mutates the shared object returned by read-only producer registry.schema through app.m.schema_of. Change a copy.deepcopy of the value instead.
A shallow copy shares its items¶
dict(x), x.copy(), and copy.copy(x) copy one level. The inner lists are the same objects, so a change below the copy changes the original too. MUT115 reports it when the original is state that outlives the call. Tier: heuristic, opt-in (MUT115).
def with_base(options: dict[str, list[str]], base: str) -> dict[str, list[str]]:
result = options.copy()
result["bases"].append(base) # changes the list `options["bases"]` holds
return result
heki reports: with_base changes in place an object that its shallow copy of options (.copy(), line 2) shares with options. Rebuild the inner value instead: result["bases"] = [*result["bases"], base].
A container changed while it is iterated¶
A dict, set, or deque that a loop is iterating raises RuntimeError at the next step once something changes it. MUT113 reports a method that suspends at a yield inside such a loop while another method changes the container. Tier: heuristic, experimental, opt-in (MUT113).
class Subscriptions:
def __init__(self) -> None:
self.by_node: dict[str, object] = {}
self._cursor = self._cycle()
def _cycle(self):
while self.by_node:
yield from self.by_node.values() # MUT113
def next_subscriber(self) -> object | None:
subscriber = next(self._cursor, None)
if subscriber is None:
self._cursor = self._cycle() # no subscribers: return None, start over next time
return subscriber
def drop(self, node: str) -> None:
self.by_node.pop(node, None)
heki reports: Subscriptions._cycle suspends at a yield while iterating self.by_node, which Subscriptions.drop changes in place, so resuming it after that change raises RuntimeError. Iterate over a snapshot: yield from list(self.by_node.values()).
Other rules about values that change under you¶
| Rule | Reports | Tier |
|---|---|---|
MUT119 |
an in-place change of a new container that nothing keeps, such as list(xs).sort(): the change is lost |
exact, default |
MUT118 |
copy.deepcopy of an instance whose object() sentinel a function tests by identity |
heuristic, runs with --heuristic |
MUT108 |
an in-place change of a field that a standard type declares read-only: a frozen dataclass, a NamedTuple, Final and ReadOnly attributes, ReadOnly TypedDict keys |
heuristic, opt-in |
MUT107 |
a method other than an initializer that changes an attribute the class's __hash__ reads |
heuristic, opt-in |
MUT120 |
a change of a value held through a read-only view (a protocol of properties without setters) that a type checker does not see | heuristic, experimental, opt-in |
MUT112 |
an in-place change of a value that is new on one path and borrowed state on another | heuristic, experimental, opt-in |
MUT101, MUT111 |
an attribute that a @cached_property reads, changed without invalidating the cache, and a read of such a stale property |
exact, experimental, opt-in |
A write is a plain store, one premise of an exact MUT101 or MUT111 report, only when every base of the class is a project class, a plain one, or a library base the ecosystem catalog lists as keeping what its subclasses' code stores in the instance. Django's models.Model is one: neither it nor its metaclass defines attribute hooks, so a write of an attribute a model's code sets (in __init__ or a method) is a plain store, and a stale @cached_property over it is an exact report once the rules are switched on. A class-body field (expires = models.DateTimeField(), which Django replaces with a descriptor) and pk (a property of Model) keep the report heuristic.
🔂 Single-use values used twice or not at all¶
An iterator or a generator gives each item once. Reading it a second time sees nothing, and a generator that nobody iterates never runs its body.
An iterator consumed twice¶
rows below is a generator expression. sum uses it up, and the list comprehension after it sees no items. MUT106 reports the second pass. Tier: heuristic, opt-in (MUT106).
def summarize(lines: list[str]) -> tuple[int, list[str]]:
rows = (line.strip() for line in lines)
count = sum(1 for _ in rows)
kept = [r for r in rows if r] # MUT106: `rows` was consumed by `sum`
return count, kept
heki reports: rows is a single-pass iterator already consumed at line 3, so this pass sees no items. Build a list when the value is read more than once.
A generator that is never iterated¶
Calling a generator function only creates the generator. Its body runs when something iterates it. EFF116 reports a call whose generator is dropped: a statement, a value only tested for truth, a function handed to a thread, an atexit handler, or a filter predicate. Tier: exact, default, a warning; its heuristic reports, where a call may run another implementation, appear with --heuristic (EFF116).
def parse_extra(text: str):
for line in text.splitlines():
yield line.strip()
def check(text: str) -> None:
if not parse_extra(text): # EFF116
print("nothing to parse")
heki reports: only a truth test reads what parse_extra returns, and parse_extra is a generator function: a generator is always true, and the body does not run. Iterate it, as in if not list(parse_extra(text)).
👉 Next steps¶
- Configuration: contracts lists every key of the five contracts.
- Adopting heki in a code base shows how to start with a baseline.
- Adoption recipes have contracts to copy for a web service and a layered application.
- Coding agents shows how the same contracts keep an agent out of the files it must not change.
heki inspect app.cart.remove(experimental) shows what one function changes, keeps, and returns.
🦀 If you know Rust¶
Nothing here is enforced when Python compiles the code. heki reports the places where the code breaks the expectation that a Rust compiler would hold it to. What Python leaves open puts these next to the effect and immutability checks.
| Rust | Closest in heki | How Python differs |
|---|---|---|
| Ownership: one owner decides who may change a value | write-owners, read-owners, call-owners (MUT201, MUT203, MUT204) |
Any code can reach any object. A contract names the owners, and heki reports the code outside them. |
Shared borrow &T |
Sequence, Mapping, AbstractSet, and ReadOnly parameters (MUT110); shared return values (MUT114, MUT202) |
The annotation promises nothing at run time. heki reports the helper that changes the value anyway. |
Exclusive borrow &mut T, no change while borrowed |
a container changed while it is iterated (MUT113); a shallow copy that shares its items (MUT115) |
Nothing stops the change, and it shows up later as a RuntimeError or a surprising value. |
| Move: a moved value cannot be used again | a single-use iterator or generator (MUT106, EFF116) |
The name stays usable after the value is used up, and the second use sees nothing. |
| Lifetime: a reference does not outlive what it points to | cleanup that cancellation cuts short (EFF118) |
A Python object stays alive while a name refers to it, so nothing dangles. What can end early is the work around a resource. |