Skip to content

OD120 ormcache-unknown-name

An ormcache key names something that is not a parameter of the method.

Category Severity Series Needs the index
Correctness Error all no

What it reports

A string given to @ormcache, or to the @ormcache_context of 18.0, that reads a name which is neither a parameter of the decorated method (self included, and *args or **kwargs by their names), a name the key binds itself in a comprehension, a lambda or :=, a Python builtin, nor a name Odoo's cache module gives the key in the series of the module. A module of unknown series may read the names of any series. The finding is at the string, once per name.

A key that does not parse is not checked, and neither are the keys of a decorator given skiparg, which 18.0 caches on instead of them.

Why it matters

Odoo turns the keys into a lambda over the method's parameters, such as lambda self, lang_code: (lang,), and evaluates it apart from the module that defines the method, so not even that module's imports and constants are visible to it. Before 19.0 the lambda sees the globals of Odoo's own cache module instead, which change by series: time from saas~17.2, so from 18.0, and Counter from saas~13.4 to saas~18.2, so 14.0 to 18.0, among others. From 19.0 it sees only method, the decorated function. The model loads, and then each call of the method computes the key before it looks up the cache, and raises NameError. Key on a parameter, or on what self gives, such as self.env.uid.

Example

models/res_partner.py
class ResPartner(models.Model):
    _inherit = "res.partner"

    @tools.ormcache("lang")
    def _greeting_for(self, lang_code):
        return "Bonjour" if lang_code.startswith("fr") else "Hello"
models/res_partner.py
class ResPartner(models.Model):
    _inherit = "res.partner"

    @tools.ormcache("lang_code")
    def _greeting_for(self, lang_code):
        return "Bonjour" if lang_code.startswith("fr") else "Hello"

Suppressing

@tools.ormcache("lang")  # odooscan: ignore[OD120]

See suppressing a finding.