cached-property-stale-read (MUT111)¶
Family: Mutation & State (MUT) · Exact · Opt-in · Experimental · Severity error (a bug) · No automatic fix
Opt-in: reading a @cached_property that a method of its class family can leave stale (a write MUT101 reports).
Experimental
It reports the readers of the stale writes MUT101 finds, so it shares MUT101's precision. Experimental features can still change in behavior or precision, or be removed, in any release, and each change is listed in the release notes. An opt-in rule that is not experimental is stable and only off by default. See Experimental Features.
What it does¶
Checks for functions that read a @functools.cached_property that a method can leave stale: a
method that changes an attribute the property reads without invalidating the cached value, as
MUT101 reports it. The rule is opt-in: enable it with --extend-select MUT111 or
extend-select = ["MUT111"]. Its exact reports are true claims (census on the Stage 0-A corpus:
60 exact reports, none false; holdout: 2, none false), but at most 2 of the 60 show real harm,
below the 10% share a default rule needs: they read values that the writers behind them leave
stale only in setup or on objects nothing reads again.
A read is one in the function's own body, on any object of the class: self.prop, obj.prop
on a parameter, attribute, or value whose class heki knows (as for MUT203),
getattr(obj, "prop"), and obj.__dict__["prop"] / obj.__dict__.get("prop") (also on
vars(obj)). The object may be an instance of the read's class or of any project subclass of
it. The read is reported when
an instance of one of those classes runs both the cached property and the method that leaves it
stale: the first definition of each along its MRO, or one that the definitions before it call
through super() without invalidating the property themselves. A method of a sibling class, or
one that a subclass replaces without calling it, does not count for that subclass's instances.
A @property getter or a helper that reads the cached property is the function reported; the
functions calling it are not. One new reader of a stale value is one new diagnostic under a
Ratchet baseline. The rule reports one diagnostic per function and property name, at the first
read, and lists every method that can leave the value stale as related locations.
The writes count whatever is selected: MUT101 need not be enabled, and a MUT101 diagnostic
suppressed at the write still counts. It does not report what MUT101 does not: a property
whose writers invalidate it on every path, whose inputs are set only during two-phase
initialization, or that no method changes.
What a report states¶
An exact report states that the function's own body reads the cached property on an object every
class of which runs a write that MUT101 reports exact: the value read may have been computed
before that write and kept after it.
The first five premises are those of that MUT101 report, as its page states them: a value of
a container ABC that the writer uses after its write, for one, counts as one of the classes of its
family (Kept to the return).
Its premises:
- Cached property: the getter's decorator is one the catalog lists, the first definition along each class's MRO. Established.
- Reads on every path: the getter reads the written attribute on every path on which it returns normally. Established.
- Every implementation: each call on the getter's chain has every implementation as its targets. Established.
- Plain write: the write is a plain store or an in-place change of a builtin container no project subclass replaces. Established.
- Kept to the return: nothing from the write, its own instruction included, to the writer's normal return may remove the kept value. Established.
- Reads the property: the function's own body reads the property on the object (
obj.prop, a constantgetattr, an item ofobj.__dict__). Established. - Runs the writer: every class the object may be (R2) gets the property from a
@cached_propertygetter and runs a write of it whoseMUT101report is exact: the first definition of the writing method along its MRO, or one that the definitions before it reach throughsuper()without invalidating the property. Established.
Besides R1-R7, the claim assumes what that MUT101 report assumes: no finalizer (__del__,
which reference counting or the garbage collector runs when it frees an object) and no other
thread runs code between the write and the writer's return.
A heuristic report states that the function reads a @cached_property that a write MUT101
reports can leave stale on the instances the object may be. Open: the premises of the MUT101
report behind it, which are open where its page says so.
Why is this bad?¶
A cached_property computes its value once and keeps it in the instance __dict__. After a
method changes what the value was computed from, every later read returns the old value. A
function that reads the property depends on whether it runs before or after that method, which
nothing at the read shows.
Example¶
from functools import cached_property
class Parser:
def __init__(self, raw: dict[str, str]) -> None:
self.raw = raw
@cached_property
def dialect(self) -> str:
return self.raw.get("$schema", "")
def parse_all(self, docs: list[dict[str, str]]) -> None:
for doc in docs:
self.raw = doc # MUT101: `dialect` keeps the first document's value
self.parse_one()
def parse_one(self) -> bool:
return self.dialect == "draft-07" # MUT111
Use instead:
def parse_all(self, docs: list[dict[str, str]]) -> None:
for doc in docs:
self.raw = doc
self.__dict__.pop("dialect", None)
self.parse_one()
How to fix¶
Invalidate the cached value where it goes stale (self.__dict__.pop("dialect", None), or
del self.dialect once it was computed), compute the value without caching it where its inputs
change, or make sure the reading function only runs before the method that changes them.
Fix safety¶
No automatic fix: whether to invalidate, recompute, or reorder is a design decision.