diff --git a/README.md b/README.md index 1b37ab2e..20734952 100644 --- a/README.md +++ b/README.md @@ -187,15 +187,15 @@ Checks valid only for odoo <= 13.0 * external-request-timeout - - https://github.com/OCA/pylint-odoo/blob/v9.1.4/testing/resources/test_repo/broken_module/models/broken_model.py#L685 Use of external request method `requests.delete` without timeout. It could wait for a long time - - https://github.com/OCA/pylint-odoo/blob/v9.1.4/testing/resources/test_repo/broken_module/models/broken_model.py#L686 Use of external request method `requests.get` without timeout. It could wait for a long time - - https://github.com/OCA/pylint-odoo/blob/v9.1.4/testing/resources/test_repo/broken_module/models/broken_model.py#L687 Use of external request method `requests.head` without timeout. It could wait for a long time + - https://github.com/OCA/pylint-odoo/blob/v9.1.4/testing/resources/test_repo/broken_module/models/broken_model.py#L690 Use of external request method `requests.delete` without timeout. It could wait for a long time + - https://github.com/OCA/pylint-odoo/blob/v9.1.4/testing/resources/test_repo/broken_module/models/broken_model.py#L691 Use of external request method `requests.get` without timeout. It could wait for a long time + - https://github.com/OCA/pylint-odoo/blob/v9.1.4/testing/resources/test_repo/broken_module/models/broken_model.py#L692 Use of external request method `requests.head` without timeout. It could wait for a long time * invalid-commit - - https://github.com/OCA/pylint-odoo/blob/v9.1.4/testing/resources/test_repo/broken_module/models/broken_model.py#L404 Use of cr.commit() directly - More info https://github.com/OCA/odoo-community.org/blob/master/website/Contribution/CONTRIBUTING.rst#never-commit-the-transaction - - https://github.com/OCA/pylint-odoo/blob/v9.1.4/testing/resources/test_repo/broken_module/models/broken_model.py#L405 Use of cr.commit() directly - More info https://github.com/OCA/odoo-community.org/blob/master/website/Contribution/CONTRIBUTING.rst#never-commit-the-transaction - - https://github.com/OCA/pylint-odoo/blob/v9.1.4/testing/resources/test_repo/broken_module/models/broken_model.py#L406 Use of cr.commit() directly - More info https://github.com/OCA/odoo-community.org/blob/master/website/Contribution/CONTRIBUTING.rst#never-commit-the-transaction + - https://github.com/OCA/pylint-odoo/blob/v9.1.4/testing/resources/test_repo/broken_module/models/broken_model.py#L409 Use of cr.commit() directly - More info https://github.com/OCA/odoo-community.org/blob/master/website/Contribution/CONTRIBUTING.rst#never-commit-the-transaction + - https://github.com/OCA/pylint-odoo/blob/v9.1.4/testing/resources/test_repo/broken_module/models/broken_model.py#L410 Use of cr.commit() directly - More info https://github.com/OCA/odoo-community.org/blob/master/website/Contribution/CONTRIBUTING.rst#never-commit-the-transaction + - https://github.com/OCA/pylint-odoo/blob/v9.1.4/testing/resources/test_repo/broken_module/models/broken_model.py#L411 Use of cr.commit() directly - More info https://github.com/OCA/odoo-community.org/blob/master/website/Contribution/CONTRIBUTING.rst#never-commit-the-transaction * license-allowed @@ -270,7 +270,7 @@ Checks valid only for odoo <= 13.0 * no-wizard-in-models - - https://github.com/OCA/pylint-odoo/blob/v9.1.4/testing/resources/test_repo/broken_module/models/broken_model.py#L824 No wizard class for model directory. See the complete structure https://github.com/OCA/odoo-community.org/blob/master/website/Contribution/CONTRIBUTING.rst#complete-structure + - https://github.com/OCA/pylint-odoo/blob/v9.1.4/testing/resources/test_repo/broken_module/models/broken_model.py#L829 No wizard class for model directory. See the complete structure https://github.com/OCA/odoo-community.org/blob/master/website/Contribution/CONTRIBUTING.rst#complete-structure * no-write-in-compute @@ -307,9 +307,9 @@ Checks valid only for odoo <= 13.0 * sql-injection - - https://github.com/OCA/pylint-odoo/blob/v9.1.4/testing/resources/test_repo/broken_module/models/broken_model.py#L571 SQL injection risk. Use parameters if you can. - More info https://github.com/OCA/odoo-community.org/blob/master/website/Contribution/CONTRIBUTING.rst#no-sql-injection - - https://github.com/OCA/pylint-odoo/blob/v9.1.4/testing/resources/test_repo/broken_module/models/broken_model.py#L573 SQL injection risk. Use parameters if you can. - More info https://github.com/OCA/odoo-community.org/blob/master/website/Contribution/CONTRIBUTING.rst#no-sql-injection - - https://github.com/OCA/pylint-odoo/blob/v9.1.4/testing/resources/test_repo/broken_module/models/broken_model.py#L575 SQL injection risk. Use parameters if you can. - More info https://github.com/OCA/odoo-community.org/blob/master/website/Contribution/CONTRIBUTING.rst#no-sql-injection + - https://github.com/OCA/pylint-odoo/blob/v9.1.4/testing/resources/test_repo/broken_module/models/broken_model.py#L576 SQL injection risk. Use parameters if you can. - More info https://github.com/OCA/odoo-community.org/blob/master/website/Contribution/CONTRIBUTING.rst#no-sql-injection + - https://github.com/OCA/pylint-odoo/blob/v9.1.4/testing/resources/test_repo/broken_module/models/broken_model.py#L578 SQL injection risk. Use parameters if you can. - More info https://github.com/OCA/odoo-community.org/blob/master/website/Contribution/CONTRIBUTING.rst#no-sql-injection + - https://github.com/OCA/pylint-odoo/blob/v9.1.4/testing/resources/test_repo/broken_module/models/broken_model.py#L580 SQL injection risk. Use parameters if you can. - More info https://github.com/OCA/odoo-community.org/blob/master/website/Contribution/CONTRIBUTING.rst#no-sql-injection * test-folder-imported @@ -332,15 +332,15 @@ Checks valid only for odoo <= 13.0 - https://github.com/OCA/pylint-odoo/blob/v9.1.4/testing/resources/test_repo/broken_module/models/broken_model.py#L380 Use lazy % or .format() or % formatting in odoo._ functions - https://github.com/OCA/pylint-odoo/blob/v9.1.4/testing/resources/test_repo/broken_module/models/broken_model.py#L381 Use lazy % or .format() or % formatting in odoo._ functions - - https://github.com/OCA/pylint-odoo/blob/v9.1.4/testing/resources/test_repo/broken_module/models/broken_model.py#L468 Use lazy % or .format() or % formatting in odoo._ functions + - https://github.com/OCA/pylint-odoo/blob/v9.1.4/testing/resources/test_repo/broken_module/models/broken_model.py#L473 Use lazy % or .format() or % formatting in odoo._ functions * translation-format-truncated - - https://github.com/OCA/pylint-odoo/blob/v9.1.4/testing/resources/test_repo/broken_module/models/broken_model.py#L479 Logging format string ends in middle of conversion specifier + - https://github.com/OCA/pylint-odoo/blob/v9.1.4/testing/resources/test_repo/broken_module/models/broken_model.py#L484 Logging format string ends in middle of conversion specifier * translation-fstring-interpolation - - https://github.com/OCA/pylint-odoo/blob/v9.1.4/testing/resources/test_repo/broken_module/models/broken_model.py#L477 Use lazy % or .format() or % formatting in odoo._ functions + - https://github.com/OCA/pylint-odoo/blob/v9.1.4/testing/resources/test_repo/broken_module/models/broken_model.py#L482 Use lazy % or .format() or % formatting in odoo._ functions * translation-not-lazy @@ -362,15 +362,15 @@ Checks valid only for odoo <= 13.0 * translation-too-few-args - - https://github.com/OCA/pylint-odoo/blob/v9.1.4/testing/resources/test_repo/broken_module/models/broken_model.py#L475 Not enough arguments for odoo._ format string + - https://github.com/OCA/pylint-odoo/blob/v9.1.4/testing/resources/test_repo/broken_module/models/broken_model.py#L480 Not enough arguments for odoo._ format string * translation-too-many-args - - https://github.com/OCA/pylint-odoo/blob/v9.1.4/testing/resources/test_repo/broken_module/models/broken_model.py#L480 Too many arguments for odoo._ format string + - https://github.com/OCA/pylint-odoo/blob/v9.1.4/testing/resources/test_repo/broken_module/models/broken_model.py#L485 Too many arguments for odoo._ format string * translation-unsupported-format - - https://github.com/OCA/pylint-odoo/blob/v9.1.4/testing/resources/test_repo/broken_module/models/broken_model.py#L478 Unsupported odoo._ format character 'y' (0x79) at index 30 + - https://github.com/OCA/pylint-odoo/blob/v9.1.4/testing/resources/test_repo/broken_module/models/broken_model.py#L483 Unsupported odoo._ format character 'y' (0x79) at index 30 * use-vim-comment diff --git a/src/pylint_odoo/checkers/custom_logging.py b/src/pylint_odoo/checkers/custom_logging.py index a0a793a4..9eb32350 100644 --- a/src/pylint_odoo/checkers/custom_logging.py +++ b/src/pylint_odoo/checkers/custom_logging.py @@ -2,9 +2,10 @@ from contextlib import contextmanager from unittest.mock import patch -from astroid import builder, nodes +from astroid import builder, exceptions as astroid_exceptions, nodes from pylint.checkers import logging +from .odoo_addons import OdooAddons from .odoo_base_checker import OdooBaseChecker @@ -79,16 +80,21 @@ def transform_binop2call(self, node): _("lazy detectable: %s" % es_err.error) """ new_code = f"{node.left.as_string()[:-1]} {node.op} {node.right.as_string()})" - new_node = builder.extract_node(new_code) + try: + new_node = builder.extract_node(new_code) + except astroid_exceptions.AstroidSyntaxError: + return node_attrs = ["lineno", "col_offset", "parent", "end_lineno", "end_col_offset", "position", "fromlineno"] for node_attr in node_attrs: setattr(new_node, node_attr, getattr(node, node_attr, None)) return new_node def visit_binop(self, node): - if not isinstance(node.left, nodes.Call) or node.op != "%": + if not isinstance(node.left, nodes.Call) or node.op != "%" or OdooAddons.get_func_name(node.left.func) != "_": return - self.visit_call(self.transform_binop2call(node)) + new_node = self.transform_binop2call(node) + if new_node: + self.visit_call(new_node) def _check_log_method(self, *args, **kwargs): with patch("pylint.checkers.logging.CHECKED_CONVENIENCE_FUNCTIONS", {"_"}): diff --git a/src/pylint_odoo/checkers/odoo_addons.py b/src/pylint_odoo/checkers/odoo_addons.py index 61f5bef4..04c0beb6 100644 --- a/src/pylint_odoo/checkers/odoo_addons.py +++ b/src/pylint_odoo/checkers/odoo_addons.py @@ -1298,7 +1298,8 @@ def visit_assign(self, node): if manifest_path: self._odoo_inherit_items[(manifest_path, odoo_class_inherit)].add(node) - def get_func_name(self, node): + @staticmethod + def get_func_name(node): func_name = ( isinstance(node, nodes.Name) and node.name or isinstance(node, nodes.Attribute) and node.attrname or "" ) diff --git a/testing/resources/test_repo/broken_module/models/broken_model.py b/testing/resources/test_repo/broken_module/models/broken_model.py index 411df08c..c781ab15 100644 --- a/testing/resources/test_repo/broken_module/models/broken_model.py +++ b/testing/resources/test_repo/broken_module/models/broken_model.py @@ -395,6 +395,11 @@ def my_method1(self, variable1): _('%d') % 3 _('{}').format('hello') _('{}').format(3) + + # It raised exception but it was already fixed + msg = "Invalid not _ method %s".lstrip() % "value" + # It should emit message but binop.left is showing "lstrip" only instead of "_" + self.message_post(_('Double method _ and lstrtip %s').lstrip() % (variable1,)) # TODO: Emit message for this case return error_msg def my_method2(self, variable2):