diff --git a/.hooks/kx-ruff.py b/.hooks/kx-ruff.py new file mode 100755 index 00000000..5e5b591a --- /dev/null +++ b/.hooks/kx-ruff.py @@ -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) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 8988fea3..516e7180 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -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: diff --git a/pyproject.toml b/pyproject.toml index dd70488f..e09f4a7a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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", +]