From 2a3d54ffaa3c7bceb6925035b2ed51991227d990 Mon Sep 17 00:00:00 2001 From: Andrew Halberstadt Date: Tue, 6 Aug 2024 16:50:41 -0400 Subject: [PATCH] feat(python): switch from black to ruff-format for formatting --- .pre-commit-config.yaml | 5 +- STANDARD.md | 53 +++++++------------ pyproject.toml | 7 +-- reps/hooks.py | 12 ++--- .../pyproject.toml | 10 ++-- 5 files changed, 30 insertions(+), 57 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index d400259..821a884 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -20,15 +20,12 @@ repos: - id: yamllint repo: https://github.com/adrienverge/yamllint.git rev: v1.35.1 - - hooks: - - id: black - repo: https://github.com/psf/black - rev: 24.3.0 - hooks: - args: - --fix - --exit-non-zero-on-fix id: ruff + - id: ruff-format repo: https://github.com/charliermarsh/ruff-pre-commit rev: v0.3.5 exclude: | diff --git a/STANDARD.md b/STANDARD.md index ce9793f..006df12 100644 --- a/STANDARD.md +++ b/STANDARD.md @@ -367,39 +367,40 @@ commands = coverage erase [tox]: https://tox.wiki/ -### Formatting +### Formatting and Linting -Projects should use [black] for formatting. The default configuration is -recommended, but if extra configuration is necessary (e.g to exclude files), it -should go in the top-level `pyproject.toml` file: +Projects should use [ruff] for formatting and linting. The default +configuration is recommended, but if extra configuration is necessary (e.g to +exclude files), it should go in the top-level `pyproject.toml` file: ```toml -[tool.black] -extend-exclude = """(\ - test/data\ - src/foo)\ - """ +[tool.ruff] +extend-exclude = [ + "test/data", + "src/foo", +] ``` -Additionally, black should be configured to run in the pre-commit hook by +Additionally, `ruff` should be configured to run in the pre-commit hook by adding the following to the top-level `.pre-commit-config.yaml` file: ```yaml repos: - - repo: https://github.com/psf/black - rev: 23.3.0 + - repo: https://github.com/astral-sh/ruff-pre-commit + rev: v0.5.6 hooks: - - id: black + - id: ruff + args: ["--fix"] + - id: ruff-format ``` Be sure to run `pre-commit autoupdate` to pick up the latest version. -[black]: https://pypi.org/project/black/ +[ruff]: https://pypi.org/project/ruff/ -### Linting +#### Lint Rules -Projects should use [ruff] for linting. Ruff should be configured to run the -following rules: +Ruff should be configured to run the following lint rules: * pycodestyle * pyflakes @@ -421,28 +422,12 @@ select = [ "TCH", # flake8-type-checking ] ignore = [ - "E501", # let black handle line-length + "E501", # let ruff-format handle line-length ] # change to your minimum supported Python version target-version = "py38" ``` -Additionally ruff should be configured to run in the `pre-commit` hook by adding -the following to the top-level `.pre-commit-config.yaml` file: - -```yaml -repos: - - repo: https://github.com/astral-sh/ruff-pre-commit - rev: 'v0.1.5' - hooks: - - id: ruff - args: [--fix, --exit-non-zero-on-fix] -``` - -Be sure to run `pre-commit autoupdate` to pick up the latest version. - -[ruff]: https://pypi.org/project/ruff/ - ### Type Checking Projects should use [Pyright] for type checking. Pyright has advanced type diff --git a/pyproject.toml b/pyproject.toml index 45af7bc..46dda54 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -28,11 +28,6 @@ requests = "^2.31.0" [tool.poetry.group.type.dependencies] pyright = "^1.1.336" -[tool.black] -extend-exclude = """(\ - reps/templates)\ - """ - [tool.coverage.run] omit = ["reps/templates/*"] @@ -40,7 +35,7 @@ omit = ["reps/templates/*"] testpaths = ["test"] [tool.ruff] -exclude = ["reps/templates"] +extend-exclude = ["reps/templates"] [tool.pyright] include = ["reps"] diff --git a/reps/hooks.py b/reps/hooks.py index 99531c5..4b2e22e 100644 --- a/reps/hooks.py +++ b/reps/hooks.py @@ -69,17 +69,13 @@ def merge_pre_commit(items: CookiecutterContext): pre_commit_config["repos"].extend( [ { - "repo": "https://github.com/psf/black", - "rev": "23.3.0", + "repo": "https://github.com/astral-sh/ruff-pre-commit", + "rev": "v0.5.6", "hooks": [ - {"id": "black"}, + {"id": "ruff", "args": ["--fix", "--exit-non-zero-on-fix"]}, + {"id": "ruff-format"}, ], }, - { - "repo": "https://github.com/astral-sh/ruff-pre-commit", - "rev": "v0.1.1", - "hooks": [{"id": "ruff", "args": ["--fix", "--exit-non-zero-on-fix"]}], - }, ] ) diff --git a/reps/templates/python/{{cookiecutter.__project_slug}}/pyproject.toml b/reps/templates/python/{{cookiecutter.__project_slug}}/pyproject.toml index bb29ad5..adcafcb 100644 --- a/reps/templates/python/{{cookiecutter.__project_slug}}/pyproject.toml +++ b/reps/templates/python/{{cookiecutter.__project_slug}}/pyproject.toml @@ -9,9 +9,6 @@ readme = "README.md" [tool.poetry.dependencies] python = "^{{cookiecutter.min_python_version}}" -[tool.black] -line-length = 88 - [tool.pytest.ini_options] xfail_strict = true @@ -21,6 +18,10 @@ branch = true source = ["src/{{cookiecutter.__package_name}}/"] [tool.ruff] +line-length = 88 +target-version = "py{{cookiecutter.__min_tox_python_version}}" + +[tool.ruff.lint] select = [ "E", "W", # pycodestyle "F", # pyflakes @@ -30,9 +31,8 @@ select = [ "TCH", # flake8-type-checking ] ignore = [ - "E501", # let black handle line-length + "E501", # let ruff-format handle line-length ] -target-version = "py{{cookiecutter.__min_tox_python_version}}" [tool.ruff.isort] known-first-party = ["{{cookiecutter.__package_name}}"]