Skip to content

Commit

Permalink
Add pre-commit hooks (#387)
Browse files Browse the repository at this point in the history
Co-authored-by: schroedtert <[email protected]>
  • Loading branch information
chraibi and schroedtert authored Oct 12, 2024
1 parent c5bbaf3 commit e18bfce
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 82 deletions.
30 changes: 30 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.6.8
hooks:
- id: ruff
alias: ruff-include-sorting
name: Check include sorting (ruff)
args: ['check', '--select', 'I', '--fix', '.']
- id: ruff-format
name: Check formatting (ruff)
args: ['.']
- id: ruff
alias: ruff-linting
name: Linting (ruff)
files: ^pedpy/

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.11.2 # Use the latest version of mypy available at the time
hooks:
- id: mypy
name: Check type hints (mypy)
additional_dependencies: [
"numpy~=2.1",
"pandas~=2.2.3",
"Shapely~=2.0.6",
"scipy~=1.14",
"matplotlib~=3.9",
"h5py~=3.11"
]
exclude: "(^helper/|^docs/|^scripts/|^tests/)"
36 changes: 27 additions & 9 deletions docs/source/developer_guide.rst
Original file line number Diff line number Diff line change
Expand Up @@ -252,25 +252,43 @@ Only Pull Request with succeeding pipelines will be allowed to be merged into th
Formatting/Linting
------------------

Except from the functional requirements (see :ref:`Tests`) for changes in the code base, we also have some non-functional requirements.
Aside from the functional requirements (see :ref:`Tests`) for changes in the code base, we also have some non-functional requirements.
These will also be checked in our CI process for each Pull Request.
To ensure compliance with these requirements, we have integrated pre-commit hooks into the repository.

1. **Code formatting:**
To ensure that your Pull Request may get accepted, make sure that the code is formatted with ``ruff format``.
We provide a helper script (``scripts/format.sh``) that will format every file in the correct manner.
To test it locally you can use ``scripts/check-format.sh``.
1. **Install Pre-Commit Hooks:**

2. **Type Hints:**
To simplify and automate formatting, linting, and type checking, we use pre-commit hooks. This will ensure that every commit is checked against our formatting, linting, and type hinting standards before being accepted.

To set up the pre-commit hooks locally, run the following command:

```bash
pip install pre-commit
pre-commit install
```
Now, every time you commit changes, the hooks will automatically run, checking for issues related to formatting, linting, and type hints.

.. note ::
These pre-commit hooks may change your files, i.e., when the code needs some reformatting, make sure to add these new changes also to your commit.
Otherwise you will not be able to commit.
2. **Code formatting:**
To ensure that your Pull Request may get accepted, make sure that the code is formatted with ``ruff``.
The pre-commit hooks, will automatically format your code according to our guide lines.
Alternatively, We provide a helper script (``scripts/format.sh``) that will format every file in the correct manner.

3. **Type Hints:**
We decided that every function, parameter, return value, etc. should be annotated with type hints, as they make it clearer for users what to expect and what is needed.
For ensuring that no type hint is forgotten we use ``MyPy``.
This can be checked locally via ``python3 -m mypy --config-file mypy.ini pedpy/``
This can be checked locally via ``python3 -m mypy .``

3. **Linting:**
4. **Linting:**
Linting in Python is an important process that helps ensure that our code is consistent and adheres to best practices.
Linting tools like ``ruff`` analyze our code for potential errors, bad practices, and code smells.
This helps us catch issues early on and prevents them from becoming bigger problems down the line.

4. **Docstring style:** (included in linting)
5. **Docstring style:** (included in linting)
Make sure to check whether every of your new functions has a docstring.
We decided to use Google-docstring style to be used in our project.
You can use `ruff` to check if everything is correct locally.
Expand Down
43 changes: 0 additions & 43 deletions mypy.ini

This file was deleted.

28 changes: 28 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -185,3 +185,31 @@ max-locals = 20
max-statements = 50
## Constant types to ignore when used as "magic values" (see: PLR2004).
allow-magic-value-types = ["int", "str"]

[tool.mypy]
python_version = "3.10"
namespace_packages = true
ignore_missing_imports = false
check_untyped_defs = true
disallow_any_generics = true
disallow_incomplete_defs = true
no_implicit_optional = true
no_implicit_reexport = true
strict_equality = true
warn_redundant_casts = true
warn_unused_ignores = false
plugins = ["numpy.typing.mypy_plugin"]
exclude = "^(helper|docs|scripts|tests)(/|$)"

[[tool.mypy.overrides]]
module = [
"matplotlib.*",
"mpl_toolkits.*",
"pandas.*",
"shapely.*",
"numpy.*",
"scipy.*",
"setuptools.*",
"h5py.*",
]
ignore_missing_imports = true
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ build~=1.1
mypy~=1.11
mypy-extensions==1.0
ruff~=0.6
pre-commit~=3.8
19 changes: 0 additions & 19 deletions scripts/check-format.sh

This file was deleted.

14 changes: 3 additions & 11 deletions scripts/ci.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#! /bin/bash
#!/bin/bash

check_for_failure() {
"$@" || failure=1
Expand All @@ -7,16 +7,8 @@ check_for_failure() {
location="$(cd "$(dirname "${0}")";pwd -P)"
root="$(cd "$(dirname "${location}/../..")";pwd -P)"

echo "Check format"
check_for_failure "${root}"/scripts/check-format.sh
echo "-------------------------------------------------------------------------"

echo "Check typing with mypy"
check_for_failure python3 -m mypy --config-file mypy.ini pedpy/
echo "-------------------------------------------------------------------------"

echo "Linting with ruff"
check_for_failure python3 -m ruff check pedpy
echo "Running pre-commit checks..."
check_for_failure pre-commit run --all-files
echo "-------------------------------------------------------------------------"

if ((failure)); then
Expand Down

0 comments on commit e18bfce

Please sign in to comment.