Skip to content

Commit

Permalink
[ADD] prefer-env-translation: Add new check for odoo v18.0
Browse files Browse the repository at this point in the history
Related to odoo/odoo#174844
  • Loading branch information
trisdoan authored and moylop260 committed Jan 13, 2025
1 parent 2a40df0 commit 7995e59
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 34 deletions.
48 changes: 32 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ no-wizard-in-models | No wizard class for model directory. See the complete stru
no-write-in-compute | Compute method calling `write`. Use `update` instead. | E8135
odoo-addons-relative-import | Same Odoo module absolute import. You should use relative import with "." instead of "odoo.addons.%s" | W8150
odoo-exception-warning | `odoo.exceptions.Warning` is a deprecated alias to `odoo.exceptions.UserError` use `from odoo.exceptions import UserError` | R8101
prefer-env-translation | Better using self.env._ More info at https://github.com/odoo/odoo/pull/174844 | W8161
print-used | Print used. Use `logger` instead. | W8116
prohibited-method-override | Prohibited override of "%s" method. | W8107
renamed-field-parameter | Field parameter "%s" is no longer supported. Use "%s" instead. | W8111
Expand Down Expand Up @@ -122,6 +123,10 @@ To know what version of odoo are you running pylint needs the parameter
with particular odoo version e.g. `"16.0"`
Check valid only for odoo >= 18.0
prefer-env-translation
Checks valid only for odoo >= 14.0
translation-format-interpolation
Expand Down Expand Up @@ -187,15 +192,15 @@ Checks valid only for odoo <= 13.0

* external-request-timeout

- https://github.com/OCA/pylint-odoo/blob/v9.3.0/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.3.0/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.3.0/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
- https://github.com/OCA/pylint-odoo/blob/v9.3.0/testing/resources/test_repo/broken_module/models/broken_model.py#L786 Use of external request method `requests.delete` without timeout. It could wait for a long time
- https://github.com/OCA/pylint-odoo/blob/v9.3.0/testing/resources/test_repo/broken_module/models/broken_model.py#L787 Use of external request method `requests.get` without timeout. It could wait for a long time
- https://github.com/OCA/pylint-odoo/blob/v9.3.0/testing/resources/test_repo/broken_module/models/broken_model.py#L788 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.3.0/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.3.0/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.3.0/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
- https://github.com/OCA/pylint-odoo/blob/v9.3.0/testing/resources/test_repo/broken_module/models/broken_model.py#L473 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.3.0/testing/resources/test_repo/broken_module/models/broken_model.py#L474 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.3.0/testing/resources/test_repo/broken_module/models/broken_model.py#L475 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

Expand Down Expand Up @@ -270,7 +275,7 @@ Checks valid only for odoo <= 13.0

* no-wizard-in-models

- https://github.com/OCA/pylint-odoo/blob/v9.3.0/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
- https://github.com/OCA/pylint-odoo/blob/v9.3.0/testing/resources/test_repo/broken_module/models/broken_model.py#L925 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

Expand All @@ -290,6 +295,12 @@ Checks valid only for odoo <= 13.0
- https://github.com/OCA/pylint-odoo/blob/v9.3.0/testing/resources/test_repo/broken_module/pylint_oca_broken.py#L11 `odoo.exceptions.Warning` is a deprecated alias to `odoo.exceptions.UserError` use `from odoo.exceptions import UserError`
- https://github.com/OCA/pylint-odoo/blob/v9.3.0/testing/resources/test_repo/broken_module/pylint_oca_broken.py#L8 `odoo.exceptions.Warning` is a deprecated alias to `odoo.exceptions.UserError` use `from odoo.exceptions import UserError`

* prefer-env-translation

- https://github.com/OCA/pylint-odoo/blob/v9.3.0/testing/resources/test_repo/broken_module/models/broken_model.py#L143 Better using self.env._ More info at https://github.com/odoo/odoo/pull/174844
- https://github.com/OCA/pylint-odoo/blob/v9.3.0/testing/resources/test_repo/broken_module/models/broken_model.py#L153 Better using self.env._ More info at https://github.com/odoo/odoo/pull/174844
- https://github.com/OCA/pylint-odoo/blob/v9.3.0/testing/resources/test_repo/broken_module/models/broken_model.py#L319 Better using self.env._ More info at https://github.com/odoo/odoo/pull/174844

* print-used

- https://github.com/OCA/pylint-odoo/blob/v9.3.0/testing/resources/test_repo/test_module/except_pass.py#L20 Print used. Use `logger` instead.
Expand All @@ -307,9 +318,9 @@ Checks valid only for odoo <= 13.0

* sql-injection

- https://github.com/OCA/pylint-odoo/blob/v9.3.0/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.3.0/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.3.0/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
- https://github.com/OCA/pylint-odoo/blob/v9.3.0/testing/resources/test_repo/broken_module/models/broken_model.py#L672 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.3.0/testing/resources/test_repo/broken_module/models/broken_model.py#L674 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.3.0/testing/resources/test_repo/broken_module/models/broken_model.py#L676 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

Expand All @@ -332,15 +343,17 @@ Checks valid only for odoo <= 13.0

- https://github.com/OCA/pylint-odoo/blob/v9.3.0/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.3.0/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.3.0/testing/resources/test_repo/broken_module/models/broken_model.py#L473 Use lazy % or .format() or % formatting in odoo._ functions
- https://github.com/OCA/pylint-odoo/blob/v9.3.0/testing/resources/test_repo/broken_module/models/broken_model.py#L444 Use lazy % or .format() or % formatting in odoo._ functions

* translation-format-truncated

- https://github.com/OCA/pylint-odoo/blob/v9.3.0/testing/resources/test_repo/broken_module/models/broken_model.py#L484 Logging format string ends in middle of conversion specifier
- https://github.com/OCA/pylint-odoo/blob/v9.3.0/testing/resources/test_repo/broken_module/models/broken_model.py#L565 Logging format string ends in middle of conversion specifier
- https://github.com/OCA/pylint-odoo/blob/v9.3.0/testing/resources/test_repo/broken_module/models/broken_model.py#L580 Logging format string ends in middle of conversion specifier

* translation-fstring-interpolation

- https://github.com/OCA/pylint-odoo/blob/v9.3.0/testing/resources/test_repo/broken_module/models/broken_model.py#L482 Use lazy % or .format() or % formatting in odoo._ functions
- https://github.com/OCA/pylint-odoo/blob/v9.3.0/testing/resources/test_repo/broken_module/models/broken_model.py#L563 Use lazy % or .format() or % formatting in odoo._ functions
- https://github.com/OCA/pylint-odoo/blob/v9.3.0/testing/resources/test_repo/broken_module/models/broken_model.py#L578 Use lazy % or .format() or % formatting in odoo._ functions

* translation-not-lazy

Expand All @@ -362,15 +375,18 @@ Checks valid only for odoo <= 13.0

* translation-too-few-args

- https://github.com/OCA/pylint-odoo/blob/v9.3.0/testing/resources/test_repo/broken_module/models/broken_model.py#L480 Not enough arguments for odoo._ format string
- https://github.com/OCA/pylint-odoo/blob/v9.3.0/testing/resources/test_repo/broken_module/models/broken_model.py#L561 Not enough arguments for odoo._ format string
- https://github.com/OCA/pylint-odoo/blob/v9.3.0/testing/resources/test_repo/broken_module/models/broken_model.py#L576 Not enough arguments for odoo._ format string

* translation-too-many-args

- https://github.com/OCA/pylint-odoo/blob/v9.3.0/testing/resources/test_repo/broken_module/models/broken_model.py#L485 Too many arguments for odoo._ format string
- https://github.com/OCA/pylint-odoo/blob/v9.3.0/testing/resources/test_repo/broken_module/models/broken_model.py#L566 Too many arguments for odoo._ format string
- https://github.com/OCA/pylint-odoo/blob/v9.3.0/testing/resources/test_repo/broken_module/models/broken_model.py#L581 Too many arguments for odoo._ format string

* translation-unsupported-format

- https://github.com/OCA/pylint-odoo/blob/v9.3.0/testing/resources/test_repo/broken_module/models/broken_model.py#L483 Unsupported odoo._ format character 'y' (0x79) at index 30
- https://github.com/OCA/pylint-odoo/blob/v9.3.0/testing/resources/test_repo/broken_module/models/broken_model.py#L564 Unsupported odoo._ format character 'y' (0x79) at index 30
- https://github.com/OCA/pylint-odoo/blob/v9.3.0/testing/resources/test_repo/broken_module/models/broken_model.py#L579 Unsupported odoo._ format character 'y' (0x79) at index 30

* use-vim-comment

Expand Down
4 changes: 2 additions & 2 deletions src/pylint_odoo/checkers/custom_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ def add_message(self, msgid, *args, **kwargs):
return super().add_message(msgid, *args, **kwargs)

def visit_call(self, node):
if not isinstance(node.func, nodes.Name):
name = OdooAddons.get_func_name(node.func)
if name != "_":
return
name = node.func.name
with config_logging_modules(self.linter, ("odoo",)):
self._check_log_method(node, name)

Expand Down
20 changes: 14 additions & 6 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._ 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 @@ -789,6 +795,7 @@ def _get_assignation_nodes(self, node):
"method-inverse",
"method-search",
"no-write-in-compute",
"prefer-env-translation",
"print-used",
"renamed-field-parameter",
"sql-injection",
Expand Down Expand Up @@ -868,11 +875,7 @@ def visit_call(self, node):
)
if method_name and self.class_odoo_models:
self.odoo_computes.add(method_name)
if (
isinstance(argument_aux, nodes.Call)
and isinstance(argument_aux.func, nodes.Name)
and argument_aux.func.name == "_"
):
if isinstance(argument_aux, nodes.Call) and self.get_func_name(argument_aux.func) == "_":
self.add_message("translation-field", node=argument_aux)
index += 1
# Check cr.commit()
Expand Down Expand Up @@ -942,7 +945,12 @@ def visit_call(self, node):
self.add_message("translation-required", node=node, args=("message_post", keyword, as_string))

# Call _(...) with variables into the term to be translated
if isinstance(node.func, nodes.Name) and node.func.name == "_" and node.args:
if self.get_func_name(node.func) == "_" and node.args:
# "_" -> isinstance(node.func, nodes.Name)
# "self.env._" -> isinstance(node.func, nodes.Attribute)
if isinstance(node.func, nodes.Name) and node.func.as_string() == "_":
self.add_message("prefer-env-translation", node=node)

wrong = ""
right = ""
arg = node.args[0]
Expand Down
Loading

0 comments on commit 7995e59

Please sign in to comment.