Skip to content

Commit

Permalink
[ADD] prefer-env-translation: Add new check for translation
Browse files Browse the repository at this point in the history
  • Loading branch information
trisdoan committed Dec 24, 2024
1 parent 6e857c5 commit 05db4ce
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/pylint_odoo/checkers/odoo_addons.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,11 @@
"deprecated-odoo-model-method",
CHECK_DESCRIPTION,
),
"W8161": (
"Better using self.env._ for getting some performance improvement in some cases. More info at https://github.com/odoo/odoo/pull/174844",
"prefer-env-translation",
CHECK_DESCRIPTION,
),
}

DFTL_MANIFEST_REQUIRED_KEYS = ["license"]
Expand Down Expand Up @@ -832,6 +837,7 @@ def _get_assignation_nodes(self, node):
"translation-field",
"translation-positional-used",
"translation-required",
"prefer-env-translation",
)
def visit_call(self, node):
if (
Expand Down Expand Up @@ -1009,6 +1015,17 @@ def visit_call(self, node):
# Check just the following cases "%s %s..."
self.add_message("translation-positional-used", node=node, args=(str2translate,))

# prefer-env-translation: recommend to translate a string (_) with self.env._
if (
isinstance(arg, nodes.Const)
and not isinstance(
node.func, nodes.Attribute
) # ensure it's not already called as attribute, e.g: self.env._()
and "fields" != self.get_func_lib(node.parent.func)
and self._is_instance_method(self.get_enclosing_function(node))
):
self.add_message("prefer-env-translation", node=node)

# SQL Injection
if self._check_sql_injection_risky(node):
self.add_message("sql-injection", node=node)
Expand Down Expand Up @@ -1044,6 +1061,11 @@ def visit_call(self, node):
if infer_node and infer_node.qname() == "itertools.groupby":
self.add_message("bad-builtin-groupby", node=node)

@staticmethod
def _is_instance_method(node: FunctionDef) -> bool:
parent = getattr(node, "parent", False)
return isinstance(parent, ClassDef) and ("_name" in parent.locals or "_inherit" in parent.locals)

@utils.only_required_for_messages(
"development-status-allowed",
"license-allowed",
Expand Down
1 change: 1 addition & 0 deletions testing/resources/test_repo/eighteen_module/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Eighteen module for tests
2 changes: 2 additions & 0 deletions testing/resources/test_repo/eighteen_module/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import models
from . import utils
12 changes: 12 additions & 0 deletions testing/resources/test_repo/eighteen_module/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "Eighteen module for tests",
"license": "AGPL-3",
"author": "Odoo Community Association (OCA)",
"version": "18.0.1.0.0",
"depends": [
"base",
],
"data": [
"security/ir.model.access.csv",
],
}
14 changes: 14 additions & 0 deletions testing/resources/test_repo/eighteen_module/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from odoo import models, _, fields
from odoo.exceptions import UserError


class EighteenModel(models.Model):
_name = "eighteen.model"

name = fields.Char(_("Näme"))

def my_method7(self):
user_id = 1
if user_id != 99:
# Method with translation
raise UserError(_("String with translation"))
9 changes: 9 additions & 0 deletions testing/resources/test_repo/eighteen_module/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from odoo import exceptions, _


def util_method():
raise exceptions.ValidationError(
_(
"The request to the service timed out. Please contact the author of the app.",
)
)
13 changes: 13 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,19 @@ def test_build_docstring(self):
"The README was updated! Don't panic only failing for CI purposes. Run the same test again.",
)

def test_gettext_env(self):
extra_params = ["--disable=all", "--enable=prefer-env-translation"]
test_repo = os.path.join(self.root_path_modules, "eighteen_module")

self.assertDictEqual(
self.run_pylint([test_repo], extra_params).linter.stats.by_msg,
{"prefer-env-translation": 1},
)

# This check is only valid for Odoo 18.0 and upwards
extra_params.append("--valid-odoo-versions=18.0")
self.assertTrue(self.run_pylint([test_repo], extra_params).linter.stats.by_msg)


if __name__ == "__main__":
unittest.main()

0 comments on commit 05db4ce

Please sign in to comment.