Skip to content

Commit 1eced04

Browse files
committed
misc: Add steps for automatically fixing errors to contributing and pre-commit-config
1 parent 4f50954 commit 1eced04

2 files changed

Lines changed: 55 additions & 3 deletions

File tree

.pre-commit-config.yaml

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# See https://pre-commit.com for more information
2-
# See https://pre-commit.com/hooks.html for more hooks
2+
default_stages: [pre-commit]
33
repos:
44
- repo: https://github.com/pre-commit/pre-commit-hooks
55
rev: v3.2.0
@@ -13,23 +13,28 @@ repos:
1313
hooks:
1414
# Run isort to check only (don't modify files)
1515
- id: isort
16+
name: "Check imports are sorted"
1617
args: [--check-only, --filter-files]
18+
stages: [pre-commit]
1719
- repo: https://github.com/astral-sh/ruff-pre-commit
1820
# Ruff version.
1921
rev: v0.14.4
2022
hooks:
2123
# Run the linter to check only (don't modify files)
2224
- id: ruff-check
25+
name: "Check code is linted with ruff"
2326
- repo: https://github.com/PyCQA/flake8
2427
rev: 7.3.0
2528
hooks:
2629
- id: flake8
30+
name: "Check code is linted with flake8"
2731
additional_dependencies: [flake8-pyproject]
2832
- repo: https://github.com/crate-ci/typos
2933
rev: v1.39.1
3034
hooks:
3135
- id: typos
32-
args: []
36+
name: "Check files for typos"
37+
stages: [pre-commit]
3338
- repo: https://github.com/rhysd/actionlint
3439
rev: v1.7.8
3540
hooks:
@@ -39,3 +44,33 @@ repos:
3944
hooks:
4045
- id: hadolint-docker
4146
entry: -e HADOLINT_IGNORE=DL3003,DL3004,DL3005,DL3007,DL3008,DL3009,DL3013,DL3015,DL3042,DL3059,SC2103,SC2046,SC2086 ghcr.io/hadolint/hadolint hadolint
47+
#
48+
# These stages modify the files applying fixes where possible
49+
# Since this may be undesirable they will not run automatically
50+
# These stages can be run with
51+
# pre-commit run --hook-stage manual
52+
#
53+
- repo: https://github.com/pycqa/isort
54+
rev: 5.13.2
55+
hooks:
56+
# Run isort to check only (don't modify files)
57+
- id: isort
58+
name: "Fix imports with isort"
59+
args: [--filter-files]
60+
stages: [manual]
61+
- repo: https://github.com/astral-sh/ruff-pre-commit
62+
# Ruff version.
63+
rev: v0.14.4
64+
hooks:
65+
# Run the linter to check only (don't modify files)
66+
- id: ruff-check
67+
name: "Fix linting errors with ruff check --fix"
68+
args: [--fix]
69+
stages: [manual]
70+
- repo: https://github.com/crate-ci/typos
71+
rev: v1.39.1
72+
hooks:
73+
- id: typos
74+
name: "Fix typos"
75+
args: [-w]
76+
stages: [manual]

CONTRIBUTING.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ These checks will:
110110
* Lint GitHub Actions workflow files using actionlint *
111111
* Lint Dockerfiles using hadolint *
112112

113-
(* these checks will not change the edited files, you must manually fix the files or run an automated tool eg: `ruff check --fix`)
113+
(* these checks will not change the edited files, you must manually fix the files or run an automated tool eg: `ruff check --fix` see below for details)
114114

115115
If you absolutely must push "dirty" code, `pre-commit` can be circumvented using:
116116
```bash
@@ -133,6 +133,23 @@ Some coding rules are "enforced" (and automatically checked by CI), some are "st
133133
- Comment your code, and do not be afraid of being verbose. The first letter must be capitalized. Do not use punctuation (unless the comment consists of multiple sentences).
134134
* We _like_ that blank lines are used to logically split blocks of code implementing different (possibly sequential) tasks.
135135

136+
#### Pre-commit
137+
138+
You can use `pre-commit` to apply automated fixes for line endings, ends of files, import sorting and ruff linting.
139+
All of these steps can be run together on your changes by running:
140+
```bash
141+
pre-commit run --hook-stage manual
142+
```
143+
Adding the `-a` flag runs this on all files in the repository, not just the files that you have changed.
144+
Adding the name of the stage will run just one check.
145+
See the [pre-commit-config](https://github.com/devitocodes/devito/blob/main/.pre-commit.yaml) file for the names of stages.
146+
147+
Some fixes can be automatically applied by the ruff linter, but may change the code in undesirable ways.
148+
This step can only be run manually:
149+
```bash
150+
ruff check --fix --unsafe-fixes
151+
```
152+
136153
### Adding tutorials or examples
137154

138155
We always look forward to extending our [suite of tutorials and examples](https://www.devitoproject.org/devito/tutorials.html) with new Jupyter Notebooks.

0 commit comments

Comments
 (0)