From 097c8f1e563e6ab3c2b510e850a572d90658d9b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Bidoul?= Date: Sat, 28 Oct 2023 18:27:30 +0200 Subject: [PATCH 1/3] Use ruff instead of black, isort, flake8, autoflake, pyupgrade --- .gitignore | 1 + copier.yml | 12 ++++++ src/.pre-commit-config.yaml.jinja | 16 ++++++++ ... and not use_ruff %}.isort.cfg{% endif %}} | 0 ... 13 and not use_ruff %}.flake8{% endif %}} | 0 ...% if use_ruff %}.ruff.toml{% endif%}.jinja | 37 +++++++++++++++++++ 6 files changed, 66 insertions(+) rename src/{{% if odoo_version > 12 %}.isort.cfg{% endif %} => {% if odoo_version > 12 and not use_ruff %}.isort.cfg{% endif %}} (100%) rename src/{{% if odoo_version >= 13 %}.flake8{% endif %} => {% if odoo_version >= 13 and not use_ruff %}.flake8{% endif %}} (100%) create mode 100644 src/{% if use_ruff %}.ruff.toml{% endif%}.jinja diff --git a/.gitignore b/.gitignore index 9c283fd..0090721 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,7 @@ __pycache__/ *.py[cod] /.venv /.pytest_cache +/.ruff_cache # C extensions *.so diff --git a/copier.yml b/copier.yml index b1b3f6e..944eadf 100644 --- a/copier.yml +++ b/copier.yml @@ -126,6 +126,18 @@ generate_requirements_txt: Generate requirements.txt from addons manifests and optional overrides in setup.py files. +use_ruff: + default: no + type: bool + help: Use ruff and ruff-format instead of flake8, autoflake, pyupgrade, isort, black. + when: "{{ odoo_version >= 14.0 }}" + +additional_ruff_rules: + type: yaml + default: [] + help: List of additional ruff rules to enable. + when: "{{ use_ruff }}" + rebel_module_groups: type: yaml default: [] diff --git a/src/.pre-commit-config.yaml.jinja b/src/.pre-commit-config.yaml.jinja index 9062656..2af295e 100644 --- a/src/.pre-commit-config.yaml.jinja +++ b/src/.pre-commit-config.yaml.jinja @@ -120,6 +120,7 @@ repos: hooks: - id: oca-checks-odoo-module - id: oca-checks-po + {%- if not use_ruff %} - repo: https://github.com/myint/autoflake rev: {{ repo_rev.autoflake }} hooks: @@ -135,6 +136,7 @@ repos: rev: {{ repo_rev.black }} hooks: - id: black + {%- endif %} - repo: https://github.com/pre-commit/mirrors-prettier rev: v{{ repo_rev.prettier }} hooks: @@ -176,11 +178,14 @@ repos: - id: check-xml - id: mixed-line-ending args: ["--fix=lf"] + {%- if not use_ruff or odoo_version < 15.0 %} # ruff doesn't support python 3.6 - repo: https://github.com/asottile/pyupgrade rev: {{ repo_rev.pyupgrade }} hooks: - id: pyupgrade args: ["--keep-percent-format"] + {%- endif %} + {%- if not use_ruff %} - repo: https://github.com/PyCQA/isort rev: {{ repo_rev.isort }} hooks: @@ -189,6 +194,7 @@ repos: args: - --settings=. exclude: /__init__\.py$ + {%- endif %} - repo: https://github.com/acsone/setuptools-odoo rev: {{ repo_rev.setuptools_odoo }} hooks: @@ -201,12 +207,22 @@ repos: - --header - "# generated from manifests external_dependencies" {%- endif %} + {%- if not use_ruff %} - repo: https://github.com/PyCQA/flake8 rev: {{ repo_rev.flake8 }} hooks: - id: flake8 name: flake8 additional_dependencies: ["flake8-bugbear=={{ repo_rev.flake8_bugbear }}"] + {%- endif %} + {%- if use_ruff %} + - repo: https://github.com/astral-sh/ruff-pre-commit + rev: v0.1.3 + hooks: + - id: ruff + args: [ --fix, --exit-non-zero-on-fix ] + - id: ruff-format + {%- endif %} - repo: https://github.com/OCA/pylint-odoo rev: {{ repo_rev.pylint_odoo }} hooks: diff --git a/src/{% if odoo_version > 12 %}.isort.cfg{% endif %} b/src/{% if odoo_version > 12 and not use_ruff %}.isort.cfg{% endif %} similarity index 100% rename from src/{% if odoo_version > 12 %}.isort.cfg{% endif %} rename to src/{% if odoo_version > 12 and not use_ruff %}.isort.cfg{% endif %} diff --git a/src/{% if odoo_version >= 13 %}.flake8{% endif %} b/src/{% if odoo_version >= 13 and not use_ruff %}.flake8{% endif %} similarity index 100% rename from src/{% if odoo_version >= 13 %}.flake8{% endif %} rename to src/{% if odoo_version >= 13 and not use_ruff %}.flake8{% endif %} diff --git a/src/{% if use_ruff %}.ruff.toml{% endif%}.jinja b/src/{% if use_ruff %}.ruff.toml{% endif%}.jinja new file mode 100644 index 0000000..8aef225 --- /dev/null +++ b/src/{% if use_ruff %}.ruff.toml{% endif%}.jinja @@ -0,0 +1,37 @@ +{%- if odoo_version >= 16.0 %} +target-version = "py310" +{%- elif odoo_version >= 15.0 %} +target-version = "py38" +{%- endif %} +fix = true + +[lint] +extend-select = [ + "B", + "C90", + "I", # isort + {%- if odoo_version >= 15.0 %} + "UP", # pyupgrade + {%- endif %} + {%- for rule in additional_ruff_rules %} + "{{ rule }}", + {%- endfor %} +] +exclude = ["setup/*"] + +[format] +exclude = ["setup/*"] + +[per-file-ignores] +"__init__.py" = ["F401", "I001"] # ignore unused and unsorted imports in __init__.py +"__manifest__.py" = ["B018"] # useless expression + +[isort] +section-order = ["future", "standard-library", "third-party", "odoo", "odoo-addons", "first-party", "local-folder"] + +[isort.sections] +"odoo" = ["odoo"] +"odoo-addons" = ["odoo.addons"] + +[mccabe] +max-complexity = 16 From a25d24d321e7bacc9122968a86cd6cb1699db898 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Bidoul?= Date: Mon, 30 Oct 2023 20:11:28 +0100 Subject: [PATCH 2/3] Smarter default for user_ruff question --- copier.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/copier.yml b/copier.yml index 944eadf..9e15d18 100644 --- a/copier.yml +++ b/copier.yml @@ -127,7 +127,7 @@ generate_requirements_txt: files. use_ruff: - default: no + default: "{% if odoo_version < 17.0 -%}no{% else %}yes{% endif %}" type: bool help: Use ruff and ruff-format instead of flake8, autoflake, pyupgrade, isort, black. when: "{{ odoo_version >= 14.0 }}" From 92c376524500c637167102b708b9df47ee401561 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Bidoul?= Date: Mon, 30 Oct 2023 20:17:45 +0100 Subject: [PATCH 3/3] Tweak jinja condition --- src/.pre-commit-config.yaml.jinja | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/.pre-commit-config.yaml.jinja b/src/.pre-commit-config.yaml.jinja index 2af295e..63f4764 100644 --- a/src/.pre-commit-config.yaml.jinja +++ b/src/.pre-commit-config.yaml.jinja @@ -214,8 +214,7 @@ repos: - id: flake8 name: flake8 additional_dependencies: ["flake8-bugbear=={{ repo_rev.flake8_bugbear }}"] - {%- endif %} - {%- if use_ruff %} + {%- else %} - repo: https://github.com/astral-sh/ruff-pre-commit rev: v0.1.3 hooks: