Skip to content

Commit c8644a9

Browse files
codexjames-d-mitchell
authored andcommitted
dev: add pre-commit hooks
1 parent 44ed1c3 commit c8644a9

12 files changed

Lines changed: 171 additions & 25 deletions

File tree

.github/workflows/lint.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ jobs:
4444
- name: "Find the names of the Python files that will be linted"
4545
run: echo "PYTHON_FILES=$(uv run ruff check --show-files | grep '\.py$' | tr -s '\n' ' ')" >> $GITHUB_ENV
4646

47-
# Lint with ruff and cpplint
47+
# Fast static checks
4848
- name: "Lint with ruff . . ."
4949
run: uv run ruff check
50-
- name: "Check ruff formatting . . ."
51-
run: uv run ruff format --diff
50+
- name: "Check Ruff formatting . . ."
51+
run: uv run ruff format --check --diff
5252
- name: "Lint with cpplint . . ."
5353
run: uv run cpplint src/*.hpp src/*.cpp
5454

@@ -72,7 +72,7 @@ jobs:
7272
- name: "Install libsemigroups_pybind11 . . ."
7373
run: uv sync --verbose --locked --all-extras --no-dev --group lint
7474

75-
# Lint with ruff and cpplint
75+
# Pylint requires the built extension
7676
- name: "Lint with pylint . . ."
7777
run: uv run pylint $PYTHON_FILES
7878
clang-format:

.pre-commit-config.yaml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
minimum_pre_commit_version: "3.2.0"
2+
default_install_hook_types: [pre-commit, pre-push]
3+
4+
repos:
5+
- repo: https://github.com/astral-sh/ruff-pre-commit
6+
rev: v0.14.2
7+
hooks:
8+
- id: ruff-check
9+
stages: [pre-commit]
10+
- id: ruff-format
11+
stages: [pre-commit]
12+
13+
- repo: local
14+
hooks:
15+
- id: pylint
16+
name: pylint
17+
entry: pylint
18+
language: system
19+
# Keep the local hook static; CI checks the built native extension.
20+
args:
21+
- --jobs=1
22+
- --extension-pkg-allow-list=
23+
- --ignored-modules=_libsemigroups_pybind11
24+
- --disable=missing-class-docstring
25+
types: [python]
26+
exclude: ^(?:build_tools|etc)/
27+
require_serial: true
28+
stages: [pre-push]
29+
30+
- id: cpplint
31+
name: cpplint
32+
entry: cpplint
33+
language: system
34+
files: ^src/
35+
types_or: [c, c++]
36+
stages: [pre-push]

CONTRIBUTING.rst

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,27 @@ where [package_manager] is your favourite conda-like package manager, such as
3030
conda or mamba. The default value is mamba. Note that this DOES NOT *yet* work
3131
with micromamba.
3232

33-
To build libsemigroups (with the above environment active):
33+
Git hooks
34+
---------
35+
36+
Install the repository's commit and push hooks after creating the development
37+
environment:
38+
39+
.. code-block:: console
40+
41+
pre-commit install
42+
43+
Ruff linting and formatting then run when committing, while Pylint and cpplint
44+
run before a push. To check every tracked file without making a commit or push,
45+
run:
46+
47+
.. code-block:: console
48+
49+
pre-commit run --all-files
50+
pre-commit run --all-files --hook-stage pre-push
51+
52+
Building libsemigroups
53+
----------------------
3454

3555
First, clone and prepare the libsemigroups repository:
3656

dev-environment.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ dependencies:
1515
- pip
1616
- pip-tools
1717
- pkgconfig
18+
- pre-commit
1819
- pybind11
1920
- pylint
2021
- pytest

etc/make-lint.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ exit_code=0
1010
echo "Linting with ruff . . ."
1111
ruff check || ((exit_code = 1))
1212

13+
echo "Checking formatting with ruff . . ."
14+
ruff format --check || ((exit_code = 1))
15+
1316
echo "Linting with pylint . . ."
1417
pylint $(ruff check --show-files | grep "\.py$") || ((exit_code = 1))
1518

pyproject.toml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# TODO:
2-
# * Use ruff
32
# * Decide on good configurations for tools
4-
# * Use pre-commit
53
# * Use MyPy
64
[build-system]
75
build-backend = "setuptools.build_meta"
@@ -80,8 +78,14 @@ test = ["pytest>=8.4.2"]
8078
[dependency-groups]
8179
# This list should contain any package used in the development of
8280
# libsemigroups_pybind11.
83-
dev = ["ipython>=9.6.0", { include-group = "lint" }]
84-
lint = ["codespell>=2.4.1", "cpplint>=2.0.2", "pylint>=4.0.2", "ruff>=0.14.2"]
81+
dev = ["ipython>=9.6.0", { include-group = "lint" }]
82+
lint = [
83+
"codespell>=2.4.1",
84+
"cpplint>=2.0.2",
85+
"pre-commit>=4.3.0",
86+
"pylint>=4.0.2",
87+
"ruff>=0.14.2",
88+
]
8589

8690
###################################################################
8791
# Tools

src/libsemigroups_pybind11/action.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,7 @@ def generators(self: _Self) -> Iterator[Element]:
184184

185185
_copy_cxx_mem_fns(_RightActionPPerm1PPerm1, Action)
186186

187-
for _type in (
188-
Action._py_template_params_to_cxx_type.values() # pylint: disable=protected-access
189-
):
187+
for _type in Action._py_template_params_to_cxx_type.values(): # pylint: disable=protected-access
190188
_register_cxx_wrapped_type(_type, Action)
191189

192190
########################################################################

src/libsemigroups_pybind11/adapters.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,7 @@ def __call__(self: _Self, pt: Point, x: Element) -> Point:
106106

107107
_copy_cxx_mem_fns(_ImageRightActionPPerm1PPerm1, ImageRightAction)
108108

109-
for _type in (
110-
ImageRightAction._py_template_params_to_cxx_type.values() # pylint:disable=protected-access
111-
):
109+
for _type in ImageRightAction._py_template_params_to_cxx_type.values(): # pylint:disable=protected-access
112110
_register_cxx_wrapped_type(_type, ImageRightAction)
113111

114112

src/libsemigroups_pybind11/froidure_pin.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -259,9 +259,7 @@ def sorted_elements( # pylint: disable=missing-function-docstring
259259

260260
_copy_cxx_mem_fns(_FroidurePinBMat, FroidurePin)
261261

262-
for _fp_type in (
263-
FroidurePin._py_template_params_to_cxx_type.values() # pylint:disable=protected-access
264-
):
262+
for _fp_type in FroidurePin._py_template_params_to_cxx_type.values(): # pylint:disable=protected-access
265263
_register_cxx_wrapped_type(_fp_type, FroidurePin)
266264

267265

src/libsemigroups_pybind11/konieczny.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -208,14 +208,10 @@ def D_classes(self: _Self) -> _Iterator[DClass]:
208208
_copy_cxx_mem_fns(_KoniecznyBMat8, Konieczny)
209209
_copy_cxx_mem_fns(_KoniecznyBMat8DClass, Konieczny.DClass)
210210

211-
for _type in (
212-
Konieczny._py_template_params_to_cxx_type.values() # pylint: disable=protected-access
213-
):
211+
for _type in Konieczny._py_template_params_to_cxx_type.values(): # pylint: disable=protected-access
214212
_register_cxx_wrapped_type(_type, Konieczny)
215213

216-
for _type in (
217-
Konieczny.DClass._py_template_params_to_cxx_type.values() # pylint: disable=protected-access
218-
):
214+
for _type in Konieczny.DClass._py_template_params_to_cxx_type.values(): # pylint: disable=protected-access
219215
_register_cxx_wrapped_type(_type, Konieczny.DClass)
220216

221217
__all__ = ["Konieczny"]

0 commit comments

Comments
 (0)