Skip to content
Snippets Groups Projects
Commit 241f06d8 authored by Martin Trigaux's avatar Martin Trigaux
Browse files

[ADD] test_lint: add test to detect bad usages of _

More often than it should, translations calls are made using
  _("Foo %s" % bar)

instead of
  _("Foo %s") % bar

or the even better (but more recent)
  _("Foo %s", bar)

The issue of using the first writing is that the translation lookup
won't work at the execution (looking for ir.translation for src "Foo
Alice", "Foo Bob",... instead of "Foo %s").

Add a pylint test to ensure this won't happen anymore
parent 110263df
Branches
Tags
No related merge requests found
import os
import astroid
from pylint import checkers, interfaces
class OdooBaseChecker(checkers.BaseChecker):
__implements__ = interfaces.IAstroidChecker
name = 'odoo'
msgs = {
'E8502': (
'Bad usage of _, _lt function.',
'gettext-variable',
'See https://www.odoo.com/documentation/14.0/reference/translations.html#variables'
)
}
@checkers.utils.check_messages('gettext-variable')
def visit_call(self, node):
if isinstance(node.func, astroid.Name) and node.func.name in ('_', '_lt'):
first_arg = node.args[0]
if not (isinstance(first_arg, astroid.Const) and isinstance(first_arg.value, str)):
self.add_message('gettext-variable', node=node)
def register(linter):
linter.register_checker(OdooBaseChecker(linter))
......@@ -31,6 +31,7 @@ class TestPyLint(TransactionCase):
# custom checkers
'sql-injection',
'gettext-variable',
]
BAD_FUNCTIONS = [
......@@ -68,7 +69,7 @@ class TestPyLint(TransactionCase):
'--enable=%s' % ','.join(self.ENABLED_CODES),
'--reports=n',
"--msg-template='{msg} ({msg_id}) at {path}:{line}'",
'--load-plugins=pylint.extensions.bad_builtin,_odoo_checker_sql_injection',
'--load-plugins=pylint.extensions.bad_builtin,_odoo_checker_sql_injection,_odoo_checker_gettext',
'--bad-functions=%s' % ','.join(self.BAD_FUNCTIONS),
'--deprecated-modules=%s' % ','.join(self.BAD_MODULES)
]
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment