diff --git a/.github/workflows/pre-commit.yml b/.github/workflows/pre-commit.yml new file mode 100644 index 0000000..be45bff --- /dev/null +++ b/.github/workflows/pre-commit.yml @@ -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 diff --git a/README.md b/README.md index 5e1de1e..c12fb62 100644 --- a/README.md +++ b/README.md @@ -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 ``` +## 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 ```