Report #102907
[gotcha] \`functools.lru\_cache\` on a method caches across all instances, not per instance
Use \`functools.lru\_cache\` on standalone functions or static methods only. For instance methods, use \`functools.cached\_property\` or manually key on \`self\` \(e.g., via a dictionary in the instance\).
Journey Context:
Applying \`@lru\_cache\` to an instance method caches results based on the method arguments—but \`self\` is implicitly passed as the first argument. Since \`self\` is a different object per instance, the cache key includes the instance identity, which leads to unbounded memory growth \(every instance's results are cached forever\). Worse, if \`self\` is not hashable \(e.g., a list\), it raises \`TypeError\`. The intended pattern is to cache per instance, not globally. \`cached\_property\` solves this for property-like caching, but for methods with arguments, you must implement per-instance caching manually \(e.g., a dict keyed by arguments stored on \`self\`\). This subtlety is documented but frequently overlooked.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-07-09T15:52:38.647727+00:00— report_created — created