Skip to content

Commit 5d8ddd9

Browse files
committed
Use two hooks for black: to check, and format
This splits the pre-commit hook for black into two hooks, both using the same repo and id but separately aliased: - black-check, which checks but does not modify files. This only runs when the manual stage is specified, and it is used by tox and on CI, with tox.ini and lint.yml modified accordingly. - black-format, which autoformats code. This provides the behavior most users will expect from a pre-commit hook for black. It runs automatically along with other hooks. But tox.ini and lint.yml, in addition to enabling the black-check hook, also explicitly skip the black-format hook. The effect is that in ordinary development the pre-commit hook for black does auto-formatting, but that pre-commit continues to be used effectively for running checks in which code should not be changed.
1 parent 4ba5ad1 commit 5d8ddd9

File tree

4 files changed

+21
-7
lines changed

4 files changed

+21
-7
lines changed

Diff for: .github/workflows/lint.yml

+4
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,7 @@ jobs:
1414
python-version: "3.x"
1515

1616
- uses: pre-commit/[email protected]
17+
with:
18+
extra_args: --hook-stage manual
19+
env:
20+
SKIP: black-format

Diff for: .pre-commit-config.yaml

+8
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,16 @@ repos:
33
rev: 23.9.1
44
hooks:
55
- id: black
6+
alias: black-check
7+
name: black (check)
68
args: [--check, --diff]
79
exclude: ^git/ext/
10+
stages: [manual]
11+
12+
- id: black
13+
alias: black-format
14+
name: black (format)
15+
exclude: ^git/ext/
816

917
- repo: https://github.com/PyCQA/flake8
1018
rev: 6.1.0

Diff for: README.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -142,22 +142,22 @@ To test, run:
142142
pytest
143143
```
144144

145-
To lint, run:
145+
To lint, and apply automatic code formatting, run:
146146

147147
```bash
148148
pre-commit run --all-files
149149
```
150150

151-
To typecheck, run:
151+
Code formatting can also be done by itself by running:
152152

153-
```bash
154-
mypy -p git
153+
```
154+
black .
155155
```
156156

157-
For automatic code formatting, run:
157+
To typecheck, run:
158158

159159
```bash
160-
black .
160+
mypy -p git
161161
```
162162

163163
Configuration for flake8 is in the `./.flake8` file.

Diff for: tox.ini

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ commands = pytest --color=yes {posargs}
1212
[testenv:lint]
1313
description = Lint via pre-commit
1414
base_python = py39
15-
commands = pre-commit run --all-files
15+
set_env =
16+
SKIP = black-format
17+
commands = pre-commit run --all-files --hook-stage manual
1618

1719
[testenv:mypy]
1820
description = Typecheck with mypy

0 commit comments

Comments
 (0)