PY-CACH0001 · instance_independent_cached_property
Avoid storing an instance cache for a computation independent of that instance.
This is a deterministic rule for python. Read its implementation.
Definition
Section titled “Definition”Inspect direct synchronous and asynchronous class methods decorated with cached_property.
Report a property whose executable body is one statement that never reads the receiver the
method was handed. Nothing about the value depends on the instance, yet it is computed and
stored again in every owner. Prefer a direct module function for stateless work. For expensive
shared initialization, expose a module function backed by functools.cache or use an explicit
singleton contract when identity is part of the design.
Evidence
Section titled “Evidence”Each finding identifies the owner, property, source range, and the proven absence of any read of the receiver in the executable body. The Boolean result identifies one property caching an instance independent value.
Exceptions
Section titled “Exceptions”A property whose body reads self or cls anywhere is accepted, as is one whose body does
more than a single statement, since the storage may then be paying for real work the receiver
took part in. A static method holds no receiver to read and never matches. Properties injected
by frameworks, generated code, and vendored code can be excluded by path.
Examples
Section titled “Examples”@cached_propertydef prose_collector(self) -> ProseCollector: return ProseCollector()@cached_propertydef prose_collector(self) -> ProseCollector: return ProseCollector(self.width)
@cachedef model() -> Model: return Model.load()References
Section titled “References”- Cites “The Python Standard Library”, functools.cached_property. Open reference
- Cites “The Python Standard Library”, functools.cache. Open reference