From 6e54c27ccea6070f0113a5129b6c642d4cd9f4d2 Mon Sep 17 00:00:00 2001 From: SDNick484 Date: Tue, 10 Nov 2026 10:18:21 -0800 Subject: [PATCH] Add pre-commit: ruff, plus the rendered test templates linted as a sibling would (F11) The templates hold __PKG__, so ruff excludes templates/; their rendered copies in tests/ are gitignored here, so `ruff check .` skips those too. CI lints them with --no-respect-gitignore since #25, but only after a push. pre-commit now catches it at commit time: - render-shared-tests (local): sync_shared.py --source . --package mcp_shared, when templates/, shared.toml, the sync script or pyproject.toml change. - ruff-check / ruff-format on the staged files, and the same two hooks again (aliases *-rendered-tests) with --no-respect-gitignore tests. Reusing ruff-pre-commit's hooks pins ruff once; a local hook would need its own ruff in additional_dependencies, which Dependabot doesn't update. - pre-commit-hooks: check-toml, check-yaml, check-merge-conflict, end-of-file-fixer, trailing-whitespace (the tree already passes all five). Revs are commit SHAs with "# frozen: vX" comments. Dependabot gains the pre-commit ecosystem (GA 2026-03), which bumps SHA and comment together and skips local hooks. pre-commit joins the dev extra; README and CLAUDE.md say `pre-commit install`. Checked: a 130-column line added to templates/tests/test_tasks.py passes the plain ruff hook and fails both rendered-tests hooks (E501), the #25 failure. Refs #16 Co-Authored-By: Claude Opus 5.5 --- .github/dependabot.yml | 13 +++++++++++- .pre-commit-config.yaml | 46 +++++++++++++++++++++++++++++++++++++++++ CLAUDE.md | 1 + README.md | 2 ++ pyproject.toml | 2 +- 5 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 .pre-commit-config.yaml diff --git a/.github/dependabot.yml b/.github/dependabot.yml index afb0aec..61daf6b 100644 --- a/.github/dependabot.yml +++ b/.github/dependabot.yml @@ -1,5 +1,5 @@ # Dependabot keeps the pinned action SHAs in .github/workflows/ and the dev/runtime bounds in -# pyproject.toml current. Weekly, one PR per ecosystem at most, so a Monday brings at most two +# pyproject.toml current. Weekly, one PR per ecosystem at most, so a Monday brings at most three # PRs to look at. The shared modules' runtime bounds (mcp, pyjwt, uvicorn) are mirrored in the # siblings' pyproject.toml files: a bump here is a reminder to bump them there. version: 2 @@ -12,6 +12,17 @@ updates: groups: actions: patterns: ["*"] + # The hook revs in .pre-commit-config.yaml (SHA + "# frozen: vX" comment, both bumped). + # Local hooks are skipped, which is why the rendered-tests lint reuses ruff-pre-commit's hooks + # instead of pinning a second ruff in a local hook's additional_dependencies. + - package-ecosystem: pre-commit + directory: / + schedule: + interval: weekly + day: monday + groups: + hooks: + patterns: ["*"] - package-ecosystem: pip directory: / schedule: diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..84c56f5 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,46 @@ +# pre-commit install (once per clone; then every `git commit` runs these on the staged files) +# pre-commit run --all-files (the whole tree, as CI does) +# +# Revs are commit SHAs, not tags (a tag can be moved); the comment is the tag the SHA was taken +# from. Dependabot (.github/dependabot.yml, ecosystem pre-commit) bumps both together. +repos: + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: 3e8a8703264a2f4a69428a0aa4dcb512790b2c8c # frozen: v6.0.0 + hooks: + - id: check-toml + - id: check-yaml + - id: check-merge-conflict + - id: end-of-file-fixer + - id: trailing-whitespace + + # Before the ruff hooks below that lint tests/: hooks run in the order listed here. + - repo: local + hooks: + - id: render-shared-tests + name: render the shared tests into tests/ + # The rendered copies are gitignored here, so writing them never dirties the commit. + entry: python scripts/sync_shared.py --source . --package mcp_shared + language: python + pass_filenames: false + files: ^(templates/|shared\.toml$|scripts/sync_shared\.py$|pyproject\.toml$) + + - repo: https://github.com/astral-sh/ruff-pre-commit + rev: c0f278f1254ccc8fb09676bcdb8d847e7db493e5 # frozen: v0.17.4 + hooks: + - id: ruff-check + - id: ruff-format + # The rendered tests, as every sibling lints them. `ruff .` skips them (gitignored here), + # which is how a long template line once failed all nine siblings' CI (#25). Same ruff, + # same rev as above: these are the same hooks with different arguments. + - id: ruff-check + alias: ruff-check-rendered-tests + name: ruff check (rendered tests) + args: [--no-respect-gitignore, tests] + pass_filenames: false + files: ^(templates/|shared\.toml$|scripts/sync_shared\.py$|pyproject\.toml$) + - id: ruff-format + alias: ruff-format-rendered-tests + name: ruff format --check (rendered tests) + args: [--check, --no-respect-gitignore, tests] + pass_filenames: false + files: ^(templates/|shared\.toml$|scripts/sync_shared\.py$|pyproject\.toml$) diff --git a/CLAUDE.md b/CLAUDE.md index 798102d..2bc87ea 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -32,6 +32,7 @@ software/systems engineer: be technical and concrete, with specific examples. - `ruff check .`: lint - `ruff format --check .`: check formatting - `mypy`: type-check +- `pre-commit install`: once per clone; commits then render and lint the templates as a sibling would ## Workflows diff --git a/README.md b/README.md index ce99cc3..f5f7888 100644 --- a/README.md +++ b/README.md @@ -163,6 +163,8 @@ Choices worth knowing about, and why: ## Workflows +**Pre-commit:** run `pre-commit install` once per clone (it comes with `pip install -e ".[dev]"`); each commit then renders the test templates and lints them as the siblings will, which plain `ruff check .` can't (the rendered copies are gitignored here). + ### Upgrading from format 1 If a sibling has a format 1 lockfile: diff --git a/pyproject.toml b/pyproject.toml index eb1b646..480b945 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -22,7 +22,7 @@ dependencies = [ Homepage = "https://github.com/SDNick484/mcp-server-shared" [project.optional-dependencies] -dev = ["pytest>=8", "pytest-cov>=5", "anyio>=4", "hypothesis>=6", "ruff>=0.6", "mypy>=1.11", "httpx2"] +dev = ["pytest>=8", "pytest-cov>=5", "anyio>=4", "hypothesis>=6", "ruff>=0.6", "mypy>=1.11", "httpx2", "pre-commit>=4"] [tool.setuptools.packages.find] where = ["src"]