Skip to content

Commit a5a16d8

Browse files
authored
Add pre-commit hooks (keras-team#2111)
* Add pre-commit hooks * Edit CONTRIBUTING.md * Add dummy unformatted file * Delete dummy changes * Small change
1 parent 2aff100 commit a5a16d8

File tree

7 files changed

+60
-48
lines changed

7 files changed

+60
-48
lines changed

.github/workflows/actions.yml

+4-11
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,7 @@ jobs:
9595
run: |
9696
pip install -r requirements.txt --progress-bar off
9797
pip install --no-deps -e "." --progress-bar off
98-
- name: Lint
99-
run: bash shell/lint.sh
100-
- name: Check for API changes
101-
run: |
102-
bash shell/api_gen.sh
103-
git status
104-
clean=$(git status | grep "nothing to commit")
105-
if [ -z "$clean" ]; then
106-
echo "Please run shell/api_gen.sh to generate API."
107-
exit 1
108-
fi
98+
- name: Install pre-commit
99+
run: pip install pre-commit && pre-commit install
100+
- name: Run pre-commit
101+
run: pre-commit run --all-files --hook-stage manual

.pre-commit-config.yaml

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
repos:
2+
- repo: local
3+
hooks:
4+
- id: api-gen
5+
name: api_gen
6+
entry: |
7+
bash shell/api_gen.sh
8+
git status
9+
clean=$(git status | grep "nothing to commit")
10+
if [ -z "$clean" ]; then
11+
echo "Please run shell/api_gen.sh to generate API."
12+
exit 1
13+
fi
14+
language: system
15+
stages: [pre-commit, manual]
16+
require_serial: true
17+
- repo: https://github.com/astral-sh/ruff-pre-commit
18+
rev: v0.9.2
19+
hooks:
20+
- id: ruff
21+
args: [--config, pyproject.toml, --fix, .]
22+
stages: [pre-commit]
23+
- id: ruff-format
24+
args: [--config, pyproject.toml, .]
25+
stages: [pre-commit]
26+
- id: ruff
27+
args: [--config, pyproject.toml, .]
28+
stages: [manual]
29+
- id: ruff-format
30+
args: ["--check", --config, pyproject.toml, .]
31+
stages: [manual]

CONTRIBUTING.md

+19-16
Original file line numberDiff line numberDiff line change
@@ -158,15 +158,6 @@ This is automatically done if you clone using git inside WSL.
158158
Note that will not support Windows Shell/PowerShell for any scripts in this
159159
repository.
160160

161-
## Update Public API
162-
163-
Run API generation script when creating PRs that update `keras_hub_export`
164-
public APIs. Add the files changed in `keras_hub/api` to the same PR.
165-
166-
```
167-
./shell/api_gen.sh
168-
```
169-
170161
## Testing changes
171162

172163
KerasHub is tested using [PyTest](https://docs.pytest.org/en/6.2.x/).
@@ -214,13 +205,25 @@ pytest --run_extra_large
214205
When running "extra_large" tests, we recommend also specify a specific test file
215206
so you aren't waiting around forever!
216207

217-
## Formatting Code
208+
## Generating public API and formatting the code
218209

219-
KerasHub uses [Ruff](https://docs.astral.sh/ruff/) to format the code. You can
220-
run `the following commands manually every time you want to format your code:
210+
For the first time you are setting up the repo, please run `pre-commit install`.
211+
Note that this needs to be done only once at the beginning.
221212

222-
- Run `shell/format.sh` to format your code
223-
- Run `shell/lint.sh` to check the result.
213+
Now, whenever you run `git commit -m "<message>"`, three things are
214+
automatically done:
215+
216+
- Public API generation
217+
- Code formatting
218+
- Code linting
219+
220+
If there's any error, the commit will not go through. Please fix the error (
221+
most of the times, the error is fixed automatically by the formatter/linter) and
222+
re-run the following:
223+
224+
```
225+
git add .
226+
git commit -m "<message>" # This will not get logged as a duplicate commit.
227+
```
224228

225-
If after running these the CI flow is still failing, try updating `ruff`
226-
with `pip install --upgrade ruff`.
229+
KerasHub uses [Ruff](https://docs.astral.sh/ruff/) to format the code.

requirements.txt

+3
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,7 @@ torchvision>=0.16.0
1111
# Jax.
1212
jax[cpu]
1313

14+
# pre-commit checks (formatting, linting, etc.)
15+
pre-commit
16+
1417
-r requirements-common.txt

shell/api_gen.sh

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ echo "Generating api directory with public APIs..."
77
# Generate API Files
88
python3 "${base_dir}"/api_gen.py
99

10+
# Format code because `api_gen.py` might order
11+
# imports differently.
1012
echo "Formatting api directory..."
11-
# Format API Files
12-
bash "${base_dir}"/shell/format.sh
13+
(SKIP=api-gen pre-commit run --files $(find "${base_dir}"/keras_hub/api -type f) --hook-stage pre-commit || true) > /dev/null

shell/format.sh

-8
This file was deleted.

shell/lint.sh

-11
This file was deleted.

0 commit comments

Comments
 (0)