Skip to content

Commit 062271c

Browse files
Implement pre-commit hooks
1 parent 9616419 commit 062271c

5 files changed

Lines changed: 169 additions & 1 deletion

File tree

.github/scripts/run_staged_ruff.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
"""Run Ruff against staged Python files for pre-commit hooks."""
2+
3+
from __future__ import annotations
4+
5+
import subprocess
6+
import sys
7+
from pathlib import Path
8+
9+
10+
def staged_python_files() -> list[str]:
11+
result = subprocess.run(
12+
["git", "diff", "--cached", "--name-only", "--diff-filter=ACMR"],
13+
check=True,
14+
capture_output=True,
15+
text=True,
16+
)
17+
files = []
18+
for line in result.stdout.splitlines():
19+
path = Path(line)
20+
if path.suffix in {".py", ".pyi"} and path.exists():
21+
files.append(line)
22+
return files
23+
24+
25+
def main() -> int:
26+
if len(sys.argv) != 2 or sys.argv[1] not in {"check", "format"}:
27+
print("Usage: run_staged_ruff.py {check|format}", file=sys.stderr)
28+
return 2
29+
30+
files = staged_python_files()
31+
if not files:
32+
print("No staged Python files to check.")
33+
return 0
34+
35+
command = ["uv", "run", "ruff", sys.argv[1]]
36+
if sys.argv[1] == "check":
37+
command.append("--fix")
38+
command.extend(files)
39+
return subprocess.run(command).returncode
40+
41+
42+
if __name__ == "__main__":
43+
raise SystemExit(main())

.pre-commit-config.yaml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
repos:
2+
- repo: local
3+
hooks:
4+
- id: ruff-staged-check
5+
name: ruff check staged Python files
6+
entry: python .github/scripts/run_staged_ruff.py check
7+
language: system
8+
pass_filenames: false
9+
types_or: [python, pyi]
10+
- id: ruff-staged-format
11+
name: ruff format staged Python files
12+
entry: python .github/scripts/run_staged_ruff.py format
13+
language: system
14+
pass_filenames: false
15+
types_or: [python, pyi]
16+
17+
- repo: https://github.com/pre-commit/pre-commit-hooks
18+
rev: v5.0.0
19+
hooks:
20+
- id: check-added-large-files
21+
- id: check-toml
22+
- id: check-yaml

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,17 @@ SeQUeNCe includes a comprehensive test suite, this can be ran with the following
7777
uv run pytest tests
7878
```
7979

80+
#### Pre-commit hooks
81+
To run lightweight checks before each commit, install the pre-commit hooks:
82+
```bash
83+
uv run pre-commit install
84+
```
85+
86+
The hooks run Ruff against staged Python files and perform basic file checks. They can also be run manually:
87+
```bash
88+
uv run pre-commit run --all-files
89+
```
90+
8091
## Citation
8192
Please cite us, thank you!
8293
```

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,11 @@ dev = [
6767
"ty>=0.0.16",
6868
"pandas-stubs~=2.3.3",
6969
"scipy-stubs~=1.16.3",
70+
"pre-commit>=4.6.0",
7071
]
7172

7273
[project.urls]
7374
Homepage = "https://github.com/sequence-toolbox/SeQUeNCe"
7475
Documentation = "https://sequence-rtd-tutorial.readthedocs.io/"
7576
Issues = "https://github.com/sequence-toolbox/SeQUeNCe/issues"
76-
Changelog = "https://github.com/sequence-toolbox/SeQUeNCe/blob/master/CHANGELOG.md"
77+
Changelog = "https://github.com/sequence-toolbox/SeQUeNCe/blob/master/CHANGELOG.md"

uv.lock

Lines changed: 91 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)