Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ADD] prefer-env-translation: Add new check for translation #514

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/pylint_odoo/checkers/odoo_addons.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,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 @@ -569,6 +574,7 @@ class OdooAddons(OdooBaseChecker, BaseChecker):
"odoo_maxversion": "13.0",
},
"no-raise-unlink": {"odoo_minversion": "15.0"},
"prefer-env-translation": {"odoo_minversion": "18.0"},
}

def __init__(self, linter: PyLinter):
Expand Down Expand Up @@ -796,6 +802,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 @@ -973,6 +980,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 not isinstance(node.parent, nodes.Assign)
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 @@ -1008,6 +1026,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 @@ -490,6 +490,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()
Loading