prefer-env-translation (W8161)#

_() is used instead of self.env._().

Applies to Odoo 18.0 and later.

What it does#

Reports calls to the global _() function from Odoo 18.0.

Why is this bad?#

self.env._() translates in the language of the environment directly. The global _() has to inspect the call stack to find it, which is slower and fails outside methods. See odoo/odoo#174844.

Fix safety#

Safe: _(...) becomes self.env._(...) inside methods of Odoo models where self is the record. Elsewhere (static methods, functions, a lambda with its own self) there is no fix. Remove the unused _ import afterwards (Ruff’s F401 does so).