Skip to content

cached-method

Pythonsuspiciousno fixdefault: warnpython/ruff

Reports a method decorated with functools.lru_cache or functools.cache. Static methods are not reported.

The cache belongs to the function and is shared by all instances. It stores self as part of each key, so an instance that called the method stays alive until its entry is evicted, and with no size limit (functools.cache, maxsize=None) for as long as the cache does.

To fix, keep the cache per instance (create it in __init__), use functools.cached_property, or move the work to a function that takes only the values it needs.

Reported
...
import functools
...
@functools.cache
def f(self):
pass
...
python:
lint:
rules:
cached-method: warn # off, warn (default), error

Enabled by the presets: python/ruff.

Suppress one line with sudsy-disable-next-line cached-method in a comment of the file’s language.

The same check in other tools; Sudsy reports what each upstream rule reports.

Tool Rule Match
ruff B019 full