Skip to content

Commit 4588058

Browse files
authored
Merge pull request #1266 from effigies/mnt/spellcheck
MNT: Add spellchecking to tox, CI and pre-commit
2 parents 79c8b4b + 12b99f9 commit 4588058

File tree

8 files changed

+79
-29
lines changed

8 files changed

+79
-29
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ jobs:
172172
continue-on-error: true
173173
strategy:
174174
matrix:
175-
check: ['style', 'doctest', 'typecheck']
175+
check: ['style', 'doctest', 'typecheck', 'spellcheck']
176176

177177
steps:
178178
- uses: actions/checkout@v4

.pre-commit-config.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,9 @@ repos:
4040
- importlib_resources
4141
args: ["nibabel"]
4242
pass_filenames: false
43+
- repo: https://github.com/codespell-project/codespell
44+
rev: v2.2.6
45+
hooks:
46+
- id: codespell
47+
additional_dependencies:
48+
- tomli

doc/source/devel/devguide.rst

Lines changed: 53 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -95,20 +95,6 @@ advise that you enable merge summaries within git:
9595

9696
See :ref:`configure-git` for more detail.
9797

98-
Pre-commit hooks
99-
----------------
100-
101-
NiBabel uses pre-commit_ to help committers validate their changes
102-
before committing. To enable these, you can use pipx_::
103-
104-
pipx run pre-commit install
105-
106-
Or install and run::
107-
108-
python -m pip install pre-commit
109-
pre-commit install
110-
111-
11298
Testing
11399
=======
114100

@@ -139,6 +125,55 @@ full set of optional dependencies that are available for 64-bit
139125
interpreters. If you are using 32-bit Python, replace ``-x64`` with ``-x86``.
140126

141127

128+
Style guide
129+
===========
130+
131+
To ensure code consistency and readability, NiBabel has adopted the following
132+
tools:
133+
134+
* blue_ - An auto-formatter that aims to reduce diffs to relevant lines
135+
* isort_ - An import sorter that groups stdlib, third-party and local imports.
136+
* flake8_ - A style checker that can catch (but generally not fix) common
137+
errors in code.
138+
* codespell_ - A spell checker targeted at source code.
139+
* pre-commit_ - A pre-commit hook manager that runs the above and various
140+
other checks/fixes.
141+
142+
While some amount of personal preference is involved in selecting and
143+
configuring auto-formatters, their value lies in largely eliminating the
144+
need to think or argue about style.
145+
With pre-commit turned on, you can write in the style that works for you,
146+
and the NiBabel style will be adopted prior to the commit.
147+
148+
To apply our style checks uniformly, simply run::
149+
150+
tox -e style,spellcheck
151+
152+
To fix any issues found::
153+
154+
tox -e style-fix
155+
tox -e spellcheck -- -w
156+
157+
Occasionally, codespell has a false positive. To ignore the suggestion, add
158+
the intended word to ``tool.codespell.ignore-words-list`` in ``pyproject.toml``.
159+
However, the ignore list is a blunt instrument and could cause a legitimate
160+
misspelling to be missed. Consider choosing a word that does not trigger
161+
codespell before adding it to the ignore list.
162+
163+
Pre-commit hooks
164+
----------------
165+
166+
NiBabel uses pre-commit_ to help committers validate their changes
167+
before committing. To enable these, you can use pipx_::
168+
169+
pipx run pre-commit install
170+
171+
Or install and run::
172+
173+
python -m pip install pre-commit
174+
pre-commit install
175+
176+
142177
Changelog
143178
=========
144179

@@ -168,6 +203,9 @@ Please see `our community guidelines
168203
<https://github.com/nipy/nibabel/blob/master/.github/CODE_OF_CONDUCT.md>`_.
169204
Other projects call these guidelines the "code of conduct".
170205

171-
.. _tox: https://tox.wiki
206+
.. _blue: https://blue.readthedocs.io/
207+
.. _codespell: https://github.com/codespell-project/codespell
208+
.. _flake8: https://flake8.pycqa.org/
172209
.. _pipx: https://pypa.github.io/pipx/
173210
.. _precommit: https://pre-commit.com/
211+
.. _tox: https://tox.wiki/

doc/source/gitwash/development_workflow.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ Rewriting commit history
334334

335335
Do this only for your own feature branches.
336336

337-
There's an embarassing typo in a commit you made? Or perhaps the you
337+
There's an embarrassing typo in a commit you made? Or perhaps the you
338338
made several false starts you would like the posterity not to see.
339339

340340
This can be done via *interactive rebasing*.

nibabel/freesurfer/tests/test_io.py

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import warnings
88
from os.path import isdir
99
from os.path import join as pjoin
10+
from pathlib import Path
1011

1112
import numpy as np
1213
import pytest
@@ -46,14 +47,6 @@
4647
)
4748

4849

49-
def _hash_file_content(fname):
50-
hasher = hashlib.md5()
51-
with open(fname, 'rb') as afile:
52-
buf = afile.read()
53-
hasher.update(buf)
54-
return hasher.hexdigest()
55-
56-
5750
@freesurfer_test
5851
def test_geometry():
5952
"""Test IO of .surf"""
@@ -179,7 +172,6 @@ def test_annot():
179172
annots = ['aparc', 'aparc.a2005s']
180173
for a in annots:
181174
annot_path = pjoin(data_path, 'label', f'lh.{a}.annot')
182-
hash_ = _hash_file_content(annot_path)
183175

184176
labels, ctab, names = read_annot(annot_path)
185177
assert labels.shape == (163842,)
@@ -190,9 +182,10 @@ def test_annot():
190182
labels_orig, _, _ = read_annot(annot_path, orig_ids=True)
191183
np.testing.assert_array_equal(labels == -1, labels_orig == 0)
192184
# Handle different version of fsaverage
193-
if hash_ == 'bf0b488994657435cdddac5f107d21e8':
185+
content_hash = hashlib.md5(Path(annot_path).read_bytes()).hexdigest()
186+
if content_hash == 'bf0b488994657435cdddac5f107d21e8':
194187
assert np.sum(labels_orig == 0) == 13887
195-
elif hash_ == 'd4f5b7cbc2ed363ac6fcf89e19353504':
188+
elif content_hash == 'd4f5b7cbc2ed363ac6fcf89e19353504':
196189
assert np.sum(labels_orig == 1639705) == 13327
197190
else:
198191
raise RuntimeError(

nibabel/info.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@
7777
pip install nibabel[test]
7878
pytest --pyargs nibabel
7979
80-
For more inforation, consult the `developer guidelines`_.
80+
For more information, consult the `developer guidelines`_.
8181
8282
.. _tox: https://tox.wiki
8383
.. _pytest: https://docs.pytest.org

pyproject.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,3 +129,7 @@ python_version = "3.11"
129129
exclude = [
130130
"/tests",
131131
]
132+
133+
[tool.codespell]
134+
skip = "*/data/*,./nibabel-data"
135+
ignore-words-list = "ans,te,ue,ist,nin,nd,ccompiler,ser"

tox.ini

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,15 @@ commands =
154154
blue nibabel
155155
isort nibabel
156156

157+
[testenv:spellcheck]
158+
description = Check spelling
159+
labels = check
160+
deps =
161+
codespell[toml]
162+
skip_install = true
163+
commands =
164+
codespell . {posargs}
165+
157166
[testenv:typecheck]
158167
description = Check type consistency
159168
labels = check

0 commit comments

Comments
 (0)