Skip to content

Commit

Permalink
Ruff config & pre-commit hook
Browse files Browse the repository at this point in the history
[Ruff](https://docs.astral.sh/ruff/):
An extremely fast Python linter and code formatter, written in Rust.

Replaces existing Black hook. Runs on pre-commit (contributors should
run `pre-commit install` in the repo root to set this up.)
  • Loading branch information
craigds committed Oct 10, 2024
1 parent 6383a38 commit 72d4de5
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 6 deletions.
17 changes: 17 additions & 0 deletions .hooks/kx-ruff.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/env python3
import subprocess
import sys

filez = sys.argv[1:]

print("Running ruff linter")
# TODO: should we enable `--unsafe-fixes` here?
# (Most of the 'unsafe' fixes appear to be "it might move a comment slightly" level of unsafe)
subprocess.check_call(["ruff", "check", "--fix", *filez])

print("Running ruff formatter")
subprocess.check_call(["ruff", "format", *filez])

# pre-commit doesn't add changed files to the index. Normally changed files fail the hook.
# however, just calling git add sneakily works around that.
subprocess.check_call(["git", "add"] + filez)
9 changes: 6 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ repos:
- id: check-yaml
- id: check-added-large-files
- id: check-json
- repo: https://github.com/psf/black
rev: 22.3.0
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.6.1
hooks:
- id: black
- id: ruff
# Run both the linter and formatter (and commit the changes automatically)
entry: ".hooks/kx-ruff.py"
- repo: https://github.com/sirosen/check-jsonschema
rev: 0.6.0
hooks:
Expand Down
22 changes: 19 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
[tool.black]
target-version = ['py37']

[tool.pyright]
exclude = ["**/__pycache__", "**/build", "venv"]


[tool.ruff]
line-length = 88
target-version = "py310"
force-exclude = true

[tool.ruff.lint]
preview = true
select = [
"F402",
"F404",
"F823",
"F821",
"F822",
"E112",
"E113",
"E902",
]

0 comments on commit 72d4de5

Please sign in to comment.