Skip to main content

odoo-pre-commit-hooks to use in pre-commit-config.yml files

Project description

Build Status codecov version wheel supported-versions commits-since code-style-black

odoo-pre-commit-hooks

OCA's custom pre-commit hooks for Odoo modules

Installation

You don't need to install it directly only configure your ".pre-commit-config.yaml" file

You even can install it directly:

  • Installing from pypi:

    • pip install -U oca-odoo-pre-commit-hooks
  • Installing from github:

    • pip install --force-reinstall -U git+https://github.com/OCA/odoo-pre-commit-hooks.git@main

Usage pre-commit-config.yaml

Add to your ".pre-commit-config.yaml" configuration file the following input

    - repo: https://github.com/OCA/odoo-pre-commit-hooks
        rev: v0.2.20
        hooks:
        - id: oca-checks-odoo-module
        - id: oca-checks-po
          args: ["--fix"]

Usage directly the entry points

If you install directly the package use the entry point:

oca-checks-odoo-module --help
oca-checks-po --help

Skip one xml-check for only one file

If you need to skip one check in one particular XML file you can use the follow comment

<?xml version="1.0" encoding="utf-8"?>
<!-- oca-hooks:disable=xml-check-to-skip -->
<odoo>
...
</odoo>
<?xml version="1.0" encoding="utf-8"?>
<!-- oca-hooks:disable=xml-check-to-skip,
                       xml-check-to-skip2 -->
<odoo>
...
</odoo>

The position of the comment it is not relative to the line that throw the check

It disable the entire file

Configuration

Behavior can be configured through several methods and as of now only consists of enabling/disabling checks.

Enabling or Disabling Checks

Each available hook consists of multiple checks which can be enabled/disabled using any of the following methods (ordered by priority):

  1. As an argument e.g., oca-checks-odoo --enable=check-to-enable --disable=check-to-disable1,check-to-disable2
  2. Using environment variables OCA_HOOKS_ENABLE or OCA_HOOKS_DISABLE e.g., export OCA_HOOKS_ENABLE=check1,check2
  3. A configuration file. The path to it can be specified with the argument --config. Alternatively a file named .oca_hooks.cfg will be looked for (by default) in the following locations (in order):
    1. Current working directory
    2. Repo's root
    3. User's home

Using a Configuration File

To enable or disable checks using a configuration file, add a disable or enable key under the MESSAGES_CONTROL section. For example:

[MESSAGES_CONTROL]
enable=check-enable1,check-enable2
disable=check-to-disable

As stated before, each source has a certain priority. This means that if the environment variable OCA_HOOKS_ENABLE=check1 exists, the configuration file above would not have any effect when it comes to enabling checks, and the only enabled check will be check1.

However, if OCA_HOOKS_DISABLE is not set, the configuration file will still have an effect and check-to-disable will be disabled.

Checks

  • Check weblate-component-too-long Check if the component is too long Weblate have 100 characters limit for repository name + Odoo Version + module name e.g. The following module: https://github.com/OCA/account-financial-reporting/tree/18.0/account_tax_balance Generates the following component: account-financial-reporting-18.0-account_tax_balance

  • Check file-not-used Check if there is a file created but not referenced from manifest.py

  • Check manifest-syntax-error Check if the manifest file has syntax error

  • Check prefer-readme-rst Check if the module has README.md file to prefer README.rst file

  • Check use-header-comments Check if the py file has comments '# comment' only in the header of python files Except valid comments e.g. pylint, flake8, shebang or comments in the middle (not header)

  • Check csv-duplicate-record-id duplicate CSV "id" AKA xmlid but for CSV files

  • Check csv-syntax-error Check syntax error for CSV files declared in the manifest

  • Check xml-deprecated-data-node Deprecated node inside xml node

  • Check xml-deprecated-oe-chatter

Odoo 18 introduced a new XML tag <chatter/> which replaces the old way to declare chatters on form views. For more information, see: https://github.com/odoo/odoo/pull/156463

  • Check xml-deprecated-openerp-node deprecated xml node

  • Check xml-deprecated-qweb-directive for use of deprecated QWeb directives t-*-options

  • Check xml-deprecated-qweb-directive-15 t-esc and t-raw directives are deprecated in Odoo v15.0, use t-out instead. For more details https://github.com/odoo/odoo/commit/01875541b1a8131cb and https://github.com/odoo/odoo/pull/70004

  • Check xml-double-quotes-py Detect XML attributes containing escaped double quotes " -> (") for python expressions Python expressions (e.g. domain, context, eval, t-* or data-* attrs). that originate from Prettier auto-fix and reduce human readability.

When Prettier rewrites values like attr='""' into attr="""" the result is valid XML but harder to read and maintain.

The check:

  • Scans XML files that contain escaped double quotes (")
  • Restricts analysis to attributes known to embed Python code
  • Verifies that both the original and the proposed value are valid Python expressions
  • Suggests replacing double quotes with single quotes when safe

This avoids false positives and prevents introducing syntax errors while improving readability and Prettier compatibility.

  • Check xml-header-missing Generated when the XML file is missing the XML declaration header ''

  • Check xml-header-wrong Generated when the XML file declaration header is different than expected (case sensitive).

  • Check xml-not-valid-char-link The resource in in src/href contains a not valid character.

  • Check xml-oe-structure-missing-id

Ensure all tags with class 'oe_structure' have an ID. For more information on the rationale, see: https://github.com/OCA/odoo-pre-commit-hooks/issues/27

  • Check xml-redundant-module-name

If the module is called "module_a" and the xmlid is <record id="module_a.xmlid_name1" ...

The "module_a." is redundant it could be replaced to only <record id="xmlid_name1" ...

  • Check xml-id-position-first

If the record id is not in the first position <record ... id="xmlid_name1"

It should be the first <record id="xmlid_name1" ...

  • Check xml-field-bool-without-eval

if the record is boolean but without eval attribute <field name="active">True</field> instead of <field name="active" eval="True" />

  • Check xml-field-numeric-without-eval

if the record is integer but without eval attribute <field name="sequence">100</field> instead of <field name="sequence" eval="100" />

  • Check xml-create-user-wo-reset-password records of user without context="{'no_reset_password': True}" This context avoid send email and mail log warning

  • Check xml-view-dangerous-replace-low-priority in ir.ui.view

    ...
  • Check xml-deprecated-tree-attribute The tree-view declaration is using a deprecated attribute.

  • Check xml-record-missing-id Generated when a tag has no id.

  • Check xml-duplicate-record-id

If a module has duplicated record_id AKA xml_ids file1.xml <record id="xmlid_name1" file2.xml <record id="xmlid_name1"

  • Check xml-duplicate-fields in all record nodes <record id="xmlid_name1"... <field name="field_name1"... <field name="field_name1"...

  • Check xml-syntax-error Check syntax of XML files declared in the Odoo manifest

  • Check xml-dangerous-qweb-replace-low-priority Dangerous qweb view defined with low priority

  • Check xml-duplicate-template-id Triggered when two templates share the same ID

  • Check xml-template-prettier-incompatible Indentify nodes incompatible with Prettier XML auto-fix generating possible unexpected text insertion

  • Check xml-xpath-translatable-item check xpath nodes using contains(text(), 'Text translatable') Since that the text could be translated so it is a mutable value. It could raise ValueError exception if the language is changed.

** Special fixit checks

  • Check field-string-redundant Detects and removes the 'string' parameter in odoo.fields if it is redundant (matches the field name in Title Case), ensuring it only applies to Odoo Class definitions (Models).

  • Check manifest-superfluous-key Identifies and removes Identifies from Odoo manifest files (manifest.py) superfluous keys (if they have the same as the default value) should be omitted for simplicity

e.g. 'installable': True True is the default value for installable key

e.g. 'data': [] [] is the default value for 'data' key

  • Check prefer-env-translation Replace _('text') with self.env._('text') only if '' comes from 'odoo.' and only for modules >=18.0

  • Check unused-logger Disallow unused _logger = logging.getLogger(__name__) in Odoo models.

Checks PO

  • Check po-requires-module Translation entry requires comment #. module: MODULE

  • Check po-python-parse-printf Check if msgid is using str variables like %s So translation msgstr must be the same number of variables too

  • Check po-python-parse-format Check if msgid is using str variables like {} So translation msgstr must be the same number of variables too

  • Check po-duplicate-message-definition (message-id) in all entries of PO files

  • Check po-duplicate-model-definition Verify that no entries share the same 'model:' tag

  • Check po-pretty-format Check the following:

  1. Entries sorted alphabetically
  2. Lines are wrapped at 78 columns (same as Odoo)
  3. Clear msgstr when it is the same as msgid
  • Check po-syntax-error Check syntax of PO files from i18n* folders

Help

usage: oca-checks-odoo-module [-h] [--no-verbose] [--no-exit] [--disable DISABLE] [--enable ENABLE] [--config CONFIG] [--list-msgs] [--fix] [files_or_modules ...]

positional arguments:
 files_or_modules Odoo __manifest__.py paths or Odoo module paths.

options:
 -h, --help show this help message and exit
 --no-verbose If enabled so disable verbose mode.
 --no-exit If enabled so it will not call exit.
 --disable, -d DISABLE Disable the checker with the given 'check-name', separated by commas.
 --enable, -e ENABLE Enable the checker with the given 'check-name', separated by commas. Default: All checks are enabled by default
 --config, -c CONFIG Path to a configuration file (default: .oca_hooks.cfg)
 --list-msgs List all currently enabled messages.
 --fix Automatically fix files when possible

Help PO

usage: oca-checks-po [-h] [--no-verbose] [--no-exit] [--disable DISABLE] [--enable ENABLE] [--config CONFIG] [--list-msgs] [--fix] [po_files ...]

positional arguments:
 po_files PO files.

options:
 -h, --help show this help message and exit
 --no-verbose If enabled so disable verbose mode.
 --no-exit If enabled so it will not call exit.
 --disable, -d DISABLE Disable the checker with the given 'check-name', separated by commas.
 --enable, -e ENABLE Enable the checker with the given 'check-name', separated by commas. Default: All checks are enabled by default
 --config, -c CONFIG Path to a configuration file (default: .oca_hooks.cfg)
 --list-msgs List all currently enabled messages.
 --fix Automatically fix files when possible

Examples

Examples PO

Licenses

This repository is licensed under AGPL-3.0.


OCA, or the Odoo Community Association, is a nonprofit organization whose mission is to support the collaborative development of Odoo features and promote its widespread use.

CHANGES

v0.2.20

  • [REF] pre-commit-hooks: Use better description instead of duplicated

v0.2.19

  • [REF] test: Add test for xml-deprecated-qweb-directive-15 check (#179)
  • [IMP] checks_odoo_module_xml: Add NodeContent class and improve the comment lines (#176)
  • [ADD] xml-double-quotes-py: Detect XML attributes containing escaped double quotes " -> (") (#178)

v0.2.18

  • [REF] coveragerc: Relax 'fail under' parameter since that coveralls cover all the tests together insteaad of individual
  • [ADD] weblate-component-too-long: Check if the weblate component name is too long

v0.2.17

  • [ADD] xml-deprecated-qweb-directive-15: t-esc and t-raw directives are deprecated in Odoo v15.0, use t-out instead (#174)

v0.2.16

  • [REF] field-string-redundant: Skip related fields in the check (#173)

v0.2.15

  • [ADD] field-string-redundant: Add old pylint known check but with autofix (#172)
  • [REF] tox, github: Enable tox env 'multi' to run multiple jobs (#171)

v0.2.14

  • [REF] test_profiling: Enable profiling from pytest
  • [REF] checks_odoo_module_fixit: Use pathlib instead of os.path
  • [FIX] fixit: Use the correct disable comment (#168)
  • [REF] tests: Migrating unittest to pytest (#167)
  • [REF] prefer-env-translation: Consider static method case (#165)
  • [REF] prefer-env-translation: Consider new test cases (#164)

v0.2.13

  • [REF] checks_odoo_module_fixit: Use a separated entrypoint to be called from pre_commit with parallels (#163)
  • [IMP] prefer-env-translation: Now supports multiple calls in the same line (#162)

v0.2.12

  • [FIX] prefer-env-translation: Skip check if it is not Odoo's class (#161)

v0.2.11

  • [FIX] use-header-comments: Remove extra newline (#160)
  • [REF] readme: Use whole original message (#159)

v0.2.10

  • [REF] use-header-comments: Skip fully commented files to avoid deletion (#158)

v0.2.9

  • [ADD] use-header-comments: New check to remove comments in the py headers (#157)
  • [REF] fixit: If the environment variable FIXIT_ODOO_VERSION is defined so it is used (#154)
  • [REF] checks_odoo_module_xml: Use filename_short instead of full path (#156)

v0.2.8

  • [REF] requirements: Use fixit from pypi after recent release (#155)

v0.2.7

  • [ADD] unused-logger: Add new check for unused _logger with autofix (#153)

v0.2.6

  • [ADD] xml-template-prettier-incompatible: Add new check for xml template node incompatible with prettier autofixes (#150)
  • [REF] github-actions: Enable py3.14 stable and use to build, lint and update-readme (#152)
  • [IMP] xml-id-position-first: Consider 'template' tag (#151)

v0.2.5

  • [ADD] xml-field-bool-without-eval, xml-field-numeric-without-eval: Check for bool or numeric fields without eval in xml records (#147)

v0.2.4

  • [FIX] file-not-used: Skip check for non-installable modules
  • [ADD] prefer-readme-rst: Add new check to prefer README.rst over README.md

v0.2.3

  • [REF] github: The macOS-13 based runner images are now retired. (#146)
  • [REF] checks_odoo_module_xml: Support auto-fixing multiline attributes (#145)

v0.2.2

  • [REF] manifest-superfluous-key: Consider boolean keys are `False` by default (#144)
  • [REF] checks_odoo_module_xml: Add the tag to be replace at fine grane (#143)

v0.2.1

  • [REF] xml-redundant-module-name: Add autofix
  • [ADD] xml-id-position-first: Add check to detect when xml id is not the first attribute

v0.2.0

  • [ADD] prefer-env-translation, manifest-superfluous-key: Add new checks with autofix using fixit library (#141)

v0.1.8

  • [ADD] xml-header-missing, xml-header-wrong: Add new XML checks with autofix (#140)

v0.1.7

  • [REM] xml-dangerous-filter-wo-user: Remove outdated check for dangerous filters without user (#139)
  • [REF] *: Small fixes (#137)

v0.1.6

  • [REF] requirements.txt: Update polib (#134)

v0.1.5

  • [REF] global_parser: FileType is deprecated for py3.14
  • [REF] github-actions: Use py3.14-dev
  • [REF] github-actions: Add codecov token to fix upload coverage (#131)

v0.1.4

  • [REF] file-not-used: Skip option using "oca_data_manual" in the manifest file (#130)

v0.1.3

  • [FIX] requirements.txt: Add missing 'packaging' requirement (#129)

v0.1.2

  • [ADD] file-not-used: Detects files not used from manifest (#127)
  • [FIX] utils: Fix "TypeError: expected string or bytes-like object, got 'NoneType'" (#128)
  • [ADD] xml-deprecated-oe-chatter: Check for deprecated chatter for odoo v18.0+ (#125)

v0.1.1

  • [REF] po-duplicate-message-definition: Ignore i18n_extra folder (#123)

v0.1.0

  • [REF] checks_odoo_module_po: Remove special char incompatible with windows codecs (#117)
  • [REF] README: Update readme with new py3.13 version
  • [REF] setup.py: Deprecate py3.8-

v0.0.35

  • [FIX] po-pretty-format: Allow original=translated term for "i18n_extra" folder (#113)

v0.0.34

  • [IMP] *: Display the duplicated lines (#111)
  • [REF] tox.ini: Add compatibility with new pyttest (#112)
  • [IMP] csv-duplicate-record-id: show the actual record id
  • [IMP] xml-duplicate-record-id: show the actual record id

v0.0.33

  • [IMP] check_odoo_module_po: extract line number from error message (#108)
  • [IMP] Better error output (#106)

v0.0.32

v0.0.31

  • [REF] setup: Add py3.12 support info (#98)
  • [REF] .github: Update actions version, fix cache and update lints (#97)
  • [REF] tests: Fixing windows po autofix unitest (#96)

v0.0.30

  • CI improvements (#93)
  • [REF] setup: Enable py311 classifier (#87)

v0.0.29

  • IMP] add xml-duplicate-template-id message (#82)
  • [FIX] lints (#86)
  • [ADD] po-duplicate-model-definition check (#85)
  • [REF] Don't filter out messages, move is_message_enabled (#83)
  • [IMP] add xml-record-missing-id (#84)

v0.0.28

  • [FIX] oe-structure-missing-id: change constraints, add docs (#81)

v0.0.27

  • [REF] improve xpath expressions (#80)

v0.0.26

  • [IMP] Improve XPath expressions, compile them only once, fix false negative, add profiling tests (#79)
  • [ADD] pretty-format-po check (#76)
  • [IMP] add xml-oe-structure-missing-id check (#78)
  • [REF] tests: Generate immutable readme (without extra spaces)
  • [IMP] README: Added a section on configuration

v0.0.25

  • [REF] requirements.txt: Update polib to fix issues and improves (#72)

v0.0.24

  • [FIX] pre-commit: Fix isort hook - RuntimeError The Poetry configuration is invalid (#71)

v0.0.23

  • [FIX] Correct documentation (#69)

v0.0.22

  • [IMP] Help msg for "xml-view-dangerous-replace-low-priority" (#68)
  • Wrote documentation for proposed refactoring (#66)
  • [FIX] ci: update flake8 repo URL, tox.ini and gh actions .yml (#65)
  • [REF] hooks: Add support for `` (#62)

v0.0.21

  • [REF] duplicate-xml-fields: Only applies to direct record's children (#58)

v0.0.20

  • [FIX] xml-xpath-translatable-item: Fix false negative calling a method o.amount_to_text() (#57)

v0.0.19

  • [FIX] setup: Add missing "requirements.txt" file to package (#441) (#55)

v0.0.18

  • [FIX] checks_odoo_module: Support glob expression for "qweb" manifest data section (#54)
  • [IMP] oca_pre_commit_hooks: Add --list-msgs option (#53)
  • [REF] oca_pre_commit_hooks: Silent subprocess git error (#52)

v0.0.17

  • [IMP] oca_pre_commit_hooks: Support enable/disable from ENV (#38) (#49)

v0.0.16

  • [ADD] xml-xpath-translatable-item: Add new check to identify mutable item used from xpath (#50)

v0.0.15

  • [FIX] oca_pre_commit_hooks: Fix exit_status false error (#48)

v0.0.14

  • [IMP] oca_pre_commit_hooks: Support for config file (#41)

v0.0.13

  • [REF] pylint_odoo: Remove TODO comments (#47)
  • [REF] requirements: Remove unused libraries and pre-commit hooks (#45)
  • [REF] xml-deprecated-data-node: Use the data node sourceline instead of odoo node (#43)
  • [IMP] oca_pre_commit_hooks: Support disable XML checks from disable comments (#42)
  • [REF] README: Fix name of pypi package (#35)
  • [REF] oca_pre_commit_hooks: cli_po, cli: Add cli_po and cli global (#36)
  • [REF] setup: Match requires-python = ">=3.7" from pre-commit
  • [REF] CI: Enable py3.11

v0.0.12

  • [REF] checks_odoo_module_xml: Allow duplicated fields with 'groups' (#32)
  • [REF] CI: Enable tox with parallels + codecov compatibility (#33)

v0.0.11

  • [REF] codecov: Enable codecov again
  • [REM] missing-readme: Check add to pylint-odoo
  • [REF] TOX: Strip names, remove no-cover, compatible with parallels option (#24)
  • [REF] lint: Adapt lints for py packages instead of odoo module (#23)

v0.0.10

  • [REF] README: Update README and fix mutable results auto-generating it (#22)
  • [REF] checks_odoo_module_po: Run on modified PO files only (#21)
  • [REF] manifest: Skip duplicated data files (#20)

v0.0.9

  • [FIX] README: Fix regex to replace autogenerated content (#18)
  • [REF] README: Detect from CI if README needs to be updated (#17)

v0.0.8

  • [REF] README: Add examples of checks section and script to auto-generate it (#16)
  • [REF] README: Add help section and script to auto-generate it (#15)

v0.0.7

  • [REF] checks: name_checks changed to name-checks (#14)
  • [REF] oca_pre_commit_hooks: Use shorter path in messages (#13)
  • [FIX] pre-commit-hooks.yaml: Run checks even if the manifest was not changed
  • [REF] test: Remove TODO comments already fixed

v0.0.6

  • [FIX] oca_pre_commit_hooks: Fix checks if it has syntax error (#8)

v0.0.5

  • [REF] oca_pre_commit_hooks: Small improvements (#7)

v0.0.4

  • [IMP] oca_pre_commit_hooks: Enable feature to 'enable' checks (#6)
  • [IMP] oca_pre_commit_hooks: Add entrypoint arguments to disable checks (#5)

v0.0.3

  • [REF] README: Remove deprecated CI build

v0.0.2

  • [REF] CI: Re-use tox to run pre-commit in the CI with same caches and packages
  • [ADD] .pre-commit-hooks.yaml: Compatible with pre-commit-hooks to use as hook id directly

v0.0.1

  • [ADD] Migrate pylint-odoo checks (not related to python) to pre-commit hooks (#2)
  • Initialize

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

oca_odoo_pre_commit_hooks-0.2.20.tar.gz (82.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

oca_odoo_pre_commit_hooks-0.2.20-py3-none-any.whl (68.4 kB view details)

Uploaded Python 3

File details

Details for the file oca_odoo_pre_commit_hooks-0.2.20.tar.gz.

File metadata

File hashes

Hashes for oca_odoo_pre_commit_hooks-0.2.20.tar.gz
Algorithm Hash digest
SHA256 172183af16be2a77deb1e7fe8aa0a398804a54bd01f4c41814e55af7e93b797e
MD5 6fc39092ea01e928569d4a0a1f619c92
BLAKE2b-256 e49b1e76ca3fa56568f6b54465d059a814b98d7cb486de664f9defcebc34f06c

See more details on using hashes here.

File details

Details for the file oca_odoo_pre_commit_hooks-0.2.20-py3-none-any.whl.

File metadata

File hashes

Hashes for oca_odoo_pre_commit_hooks-0.2.20-py3-none-any.whl
Algorithm Hash digest
SHA256 5db863977d439ba872539bb7b87859984cbaa96a6add4bdf235d008d19fd01a2
MD5 e49a5abe98f6d7d2d771968c8af4b8fa
BLAKE2b-256 0d3453093c23b37dffbeb7c278f913421ec22a0fdcb9f56e08f1f968a361195f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page