Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ruff instead of black, isort, flake8, autoflake, pyupgrade #219

Merged
merged 3 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ __pycache__/
*.py[cod]
/.venv
/.pytest_cache
/.ruff_cache

# C extensions
*.so
Expand Down
12 changes: 12 additions & 0 deletions copier.yml
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,18 @@ generate_requirements_txt:
Generate requirements.txt from addons manifests and optional overrides in setup.py
files.

use_ruff:
default: no
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this requires a manual template upgrade and ruff is a better default, I think we should enable it by default.

Suggested change
default: no
default: yes

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would that not enable ruff by default when we apply the template on existing repos?

sbidoul marked this conversation as resolved.
Show resolved Hide resolved
type: bool
help: Use ruff and ruff-format instead of flake8, autoflake, pyupgrade, isort, black.
when: "{{ odoo_version >= 14.0 }}"
yajo marked this conversation as resolved.
Show resolved Hide resolved

additional_ruff_rules:
type: yaml
default: []
help: List of additional ruff rules to enable.
when: "{{ use_ruff }}"

yajo marked this conversation as resolved.
Show resolved Hide resolved
rebel_module_groups:
type: yaml
default: []
Expand Down
16 changes: 16 additions & 0 deletions src/.pre-commit-config.yaml.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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
yajo marked this conversation as resolved.
Show resolved Hide resolved
- repo: https://github.com/asottile/pyupgrade
rev: {{ repo_rev.pyupgrade }}
hooks:
- id: pyupgrade
args: ["--keep-percent-format"]
{%- endif %}
{%- if not use_ruff %}
sbidoul marked this conversation as resolved.
Show resolved Hide resolved
- repo: https://github.com/PyCQA/isort
rev: {{ repo_rev.isort }}
hooks:
Expand All @@ -189,6 +194,7 @@ repos:
args:
- --settings=.
exclude: /__init__\.py$
{%- endif %}
- repo: https://github.com/acsone/setuptools-odoo
rev: {{ repo_rev.setuptools_odoo }}
hooks:
Expand All @@ -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 %}
sbidoul marked this conversation as resolved.
Show resolved Hide resolved
- 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:
Expand Down
37 changes: 37 additions & 0 deletions src/{% if use_ruff %}.ruff.toml{% endif%}.jinja
Original file line number Diff line number Diff line change
@@ -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 %}
sbidoul marked this conversation as resolved.
Show resolved Hide resolved
]
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