Add pre-commit documentation to README and GitHub workflow

Sets up a pre-commit workflow to automate code linting and formatting.

This ensures code quality and consistency by running checks before code is committed.
This commit is contained in:
Eyal Frishman 2025-11-25 17:27:54 +02:00
parent 449494c8b6
commit 59ed9392ed
2 changed files with 60 additions and 0 deletions

44
.github/workflows/pre-commit.yml vendored Normal file
View File

@ -0,0 +1,44 @@
name: Pre-commit
on:
push:
branches:
- main
- "release/**"
pull_request:
workflow_dispatch:
permissions:
contents: read
jobs:
run-pre-commit:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.10"
- name: Install uv
uses: astral-sh/setup-uv@v7
- name: Cache uv & pre-commit
uses: actions/cache@v4
with:
path: |
.venv
~/.cache/uv
~/.cache/pre-commit
key: ${{ runner.os }}-uv-${{ hashFiles('uv.lock', '.pre-commit-config.yaml') }}
restore-keys: |
${{ runner.os }}-uv-
- name: Install dev dependencies
run: uv sync --group dev
- name: Run pre-commit
run: uv run pre-commit run --all-files

View File

@ -123,6 +123,22 @@ I haven't invested too much here but some tests exist, especially for the tokeni
python -m pytest tests/test_rustbpe.py -v -s python -m pytest tests/test_rustbpe.py -v -s
``` ```
## Pre-commit hooks
Linting and formatting are enforced with [pre-commit](https://pre-commit.com/) both locally and in CI via GitHub Actions. To match the checks that run in PRs:
- Make sure the dev extras are installed (`uv sync --group dev`).
- Run the suite on demand: `uv run pre-commit run --all-files`.
- (optional) Install the git hook once (for automation during `git commit`): `uv run pre-commit install`.
Hook coverage (auto-fixes most issues; review and stage the changes afterward):
- [`ruff`](https://github.com/astral-sh/ruff): a fast Rust-based linter and formatter that replaces multiple tools:
- **Linting** (`ruff-check`): removes unused imports (like autoflake), upgrades syntax (like pyupgrade), and sorts imports (like isort).
- **Formatting** (`ruff-format`): applies consistent code formatting (like black), with quote style preserved.
- [`pre-commit-hooks`](https://github.com/pre-commit/pre-commit-hooks): repo hygiene (trim trailing whitespace, enforce LF endings/newlines, detect merge conflicts, block oversized files).
- [`codespell`](https://github.com/codespell-project/codespell): catches common spelling mistakes in code and docs (add false positives to `[tool.codespell].ignore-words-list` in `pyproject.toml`).
## File structure ## File structure
``` ```