Skip to content

Commit fc0f2f5

Browse files
committed
feat: introduce the Ruff pre-commit hook to replace a bunch of other code formatters and linter hooks
1 parent b3bbbd4 commit fc0f2f5

4 files changed

Lines changed: 54 additions & 67 deletions

File tree

.pre-commit-config.yaml

Lines changed: 6 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -21,50 +21,14 @@ repos:
2121
name: Check conventional commit message
2222
stages: [commit-msg]
2323

24-
# Sort imports.
25-
- repo: https://github.com/pycqa/isort
26-
rev: dac090ce4d9ee313d086e2e89ab1acb8c2664fa1 # frozen: 9.0.0a3
24+
# Ruff formats and lints code.
25+
- repo: https://github.com/astral-sh/ruff-pre-commit
26+
rev: 3b3f7c3f57fe9925356faf5fe6230835138be230 # frozen: v0.15.17
2727
hooks:
28-
- id: isort
29-
name: Sort import statements
30-
args: [--settings-path, pyproject.toml]
31-
stages: [pre-commit]
32-
33-
# Add Black code formatters.
34-
- repo: https://github.com/ambv/black
35-
rev: c6755bb741b6481d6b3d3bb563c83fa060db96c9 # frozen: 26.3.1
36-
hooks:
37-
- id: black
38-
name: Format code
28+
- id: ruff-format
3929
args: [--config, pyproject.toml]
40-
- repo: https://github.com/asottile/blacken-docs
41-
rev: dda8db18cfc68df532abf33b185ecd12d5b7b326 # frozen: 1.20.0
42-
hooks:
43-
- id: blacken-docs
44-
name: Format code in docstrings
45-
args: [--line-length, '120']
46-
additional_dependencies: [black==26.3.1]
47-
48-
# Upgrade and rewrite Python idioms.
49-
- repo: https://github.com/asottile/pyupgrade
50-
rev: 75992aaa40730136014f34227e0135f63fc951b4 # frozen: v3.21.2
51-
hooks:
52-
- id: pyupgrade
53-
name: Upgrade code idioms
54-
files: ^src/package/|^tests/
55-
args: [--py310-plus]
56-
57-
# Similar to pylint, with a few more/different checks. For more available
58-
# extensions: https://github.com/DmytroLitvinov/awesome-flake8-extensions
59-
- repo: https://github.com/pycqa/flake8
60-
rev: d93590f5be797aabb60e3b09f2f52dddb02f349f # frozen: 7.3.0
61-
hooks:
62-
- id: flake8
63-
name: Check flake8 issues
64-
files: ^src/package/|^tests/
65-
types: [text, python]
66-
additional_dependencies: [flake8-bugbear==25.11.29, flake8-builtins==3.1.0, flake8-comprehensions==3.17.0, flake8-docstrings==1.7.0, flake8-logging==1.8.0, flake8-mutable==1.2.0, flake8-noqa==1.4.0, flake8-print==5.0.0, flake8-pyi==25.5.0, flake8-pytest-style==2.2.0, flake8-rst-docstrings==0.4.0, pep8-naming==0.15.1]
67-
args: [--config, .flake8]
30+
- id: ruff-check
31+
args: [--config, pyproject.toml, --fix, --exit-non-zero-on-fix]
6832

6933
# Run Pylint from the local repo to make sure venv packages
7034
# specified in pyproject.toml are available.
@@ -89,17 +53,6 @@ repos:
8953
types: [text, python]
9054
args: [--explicit-package-bases, --config-file, pyproject.toml]
9155

92-
# Check for potential security issues.
93-
- repo: https://github.com/PyCQA/bandit
94-
rev: 92ae8b82fb422a639f0ed8d99e96cea769594e08 # frozen: 1.9.4
95-
hooks:
96-
- id: bandit
97-
name: Check for security issues
98-
args: [--configfile, pyproject.toml]
99-
files: ^src/package/|^tests/
100-
types: [text, python]
101-
additional_dependencies: ['bandit[toml]']
102-
10356
# Enable a whole bunch of useful helper hooks, too.
10457
# See https://pre-commit.com/hooks.html for more hooks.
10558
- repo: https://github.com/pre-commit/pre-commit-hooks
@@ -146,8 +99,6 @@ repos:
14699
- repo: https://github.com/macisamuele/language-formatters-pre-commit-hooks
147100
rev: 4380fbb73a154b5f5624794c1c78d9719ccc860f # frozen: v2.16.0
148101
hooks:
149-
- id: pretty-format-ini
150-
args: [--autofix]
151102
- id: pretty-format-yaml
152103
args: [--autofix]
153104
# Commenting this out because https://github.com/pappasam/toml-sort/issues/11

Makefile

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -161,12 +161,10 @@ audit:
161161
python -m pip_audit --skip-editable --desc on --fix --dry-run
162162

163163
# Run some or all checks over the package code base.
164-
.PHONY: check check-code check-bandit check-flake8 check-lint check-mypy check-actionlint
165-
check-code: check-bandit check-flake8 check-lint check-mypy check-actionlint
166-
check-bandit:
167-
pre-commit run bandit --all-files
168-
check-flake8:
169-
pre-commit run flake8 --all-files
164+
.PHONY: check check-code check-ruff check-lint check-mypy check-actionlint
165+
check-code: check-ruff check-lint check-mypy check-actionlint
166+
check-ruff:
167+
pre-commit run ruff --all-files
170168
check-lint:
171169
pre-commit run pylint --all-files
172170
check-mypy:
@@ -259,7 +257,7 @@ dist-clean:
259257
rm -fr dist/*
260258
rm -f requirements.txt
261259
clean: dist-clean
262-
rm -fr .coverage .hypothesis/ .mypy_cache/ .pytest_cache/
260+
rm -fr .coverage .hypothesis/ .mypy_cache/ .pytest_cache/ .ruff_cache/
263261
rm -fr docs/_build/
264262

265263
# Remove code caches, or the entire virtual environment.

pyproject.toml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ ignore_missing_imports = true
175175

176176

177177
# https://pylint.pycqa.org/en/latest/user_guide/configuration/index.html
178+
# https://github.com/astral-sh/ruff/issues/970
178179
[tool.pylint.main]
179180
fail-under = 10.0
180181
load-plugins = [
@@ -274,3 +275,42 @@ markers = [
274275
"integration: more complex application-level integration tests.",
275276
"performance: performance tests.",
276277
]
278+
279+
280+
# https://docs.astral.sh/ruff/formatter/
281+
# https://docs.astral.sh/ruff/linter/
282+
[tool.ruff]
283+
line-length = 120
284+
285+
[tool.ruff.format]
286+
docstring-code-format = true
287+
docstring-code-line-length = 88
288+
289+
# https://docs.astral.sh/ruff/configuration/
290+
# https://docs.astral.sh/ruff/rules/
291+
[tool.ruff.lint]
292+
exclude = ["docs/*"]
293+
select = [
294+
"E", # pycodestyle
295+
"F", # pyflakes
296+
"I", # isort
297+
"UP", # pyupgrade
298+
"B", # flake8-bugbear
299+
"A", # flake8-builtins
300+
"C4", # flake8-comprehensions
301+
"LOG", # flake8-logging
302+
"T20", # flake8-print
303+
"PYI", # flake8-pyi
304+
"PT", # flake8-pytest-style
305+
"N", # pep8-naming
306+
"S", # flake8-bandit
307+
]
308+
ignore = [
309+
"E203", # E203: whitespace before ‘,’, ‘;’, or ‘:’ (not Black compliant)
310+
"E501", # E501: line too long (managed better by Bugbear's B950)
311+
]
312+
313+
[tool.ruff.lint.per-file-ignores]
314+
"tests/*" = [
315+
"S101", # S101 Use of `assert` detected
316+
]

tests/integration/test_something.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
# Copyright (c) 2021-2026 CODEOWNERS
44
# This code is licensed under MIT license, see LICENSE.md for details.
55

6-
# https://bandit.readthedocs.io/en/latest/blacklists/blacklist_imports.html#b404-import-subprocess
7-
import subprocess # nosec B404
6+
import subprocess
87

98
import pytest
109

@@ -13,7 +12,6 @@
1312
def test_package() -> None:
1413
"""Test the Something command."""
1514
# For testing we disable this warning here:
16-
# https://bandit.readthedocs.io/en/latest/plugins/b603_subprocess_without_shell_equals_true.html
17-
# https://bandit.readthedocs.io/en/latest/plugins/b607_start_process_with_partial_path.html
18-
completed = subprocess.run(["something"], check=True, shell=False) # nosec B603, B607
15+
# https://docs.astral.sh/ruff/rules/start-process-with-partial-path/
16+
completed = subprocess.run(["something"], check=True, shell=False) # noqa: S607
1917
assert completed.returncode == 0

0 commit comments

Comments
 (0)