inheritable-method-string (ODE8147)
Preview (since 0.16.2.2) · Related issues · View source
Derived from the odoo linter.
Fix is always available.
This rule is unstable and in preview. The --preview flag is required for use.
What it does
Checks for compute=/search=/inverse= field arguments that pass a direct method
reference instead of the method's name as a string.
Why is this bad?
A direct reference hardcodes the field to that exact function object at class-definition time. When another module inherits the model and overrides the method, the field keeps calling the original function and the override is silently ignored — the classic Odoo inheritance mechanism simply does not apply. Passing the name as a string makes Odoo resolve the method on the record at runtime, so overrides are honored.
The direct reference also forces the method to be defined above the field definition (otherwise the name is unbound), which is against the convention of declaring fields first and methods after them.
Example
Use instead:
Fix safety
The fix replaces the reference with the method's name as a string. The rule only fires when the enclosing class defines a method with that name, so the string resolves to a method that exists. The fix is still marked as unsafe because dispatch changes from the bound function object to a name lookup on the record: a subclass override starts being honored (the point of the rule), and if the reference actually pointed at a same-named object from an outer scope — for example an imported function shadowed by a method defined further down the class — the class method replaces it.
References
- OCA/odoo-pre-commit-hooks#126,
the proposal this rule and
inheritable-method-lambdaimplement, including which field attributes accept a string and which require a callable.