Skip to content

Commit

Permalink
Adding CI checks with git hooks and pre-commit (pasqal-io#331)
Browse files Browse the repository at this point in the history
* add pre-commit hooks

* update contributing guidelines with pre-commit usage

* fix a typo

* Remove superflous args from isort hooks

Running isort hook with --show-config arg and the verbose: true
flag shows that the pyproject.toml configuration is read and used
by the hook:

```terminal
...
{
    "profile": "black",
    "line_length": 79,
    "source": "/Users/manta/Desktop/Pulser/pyproject.toml"
}
...
```
  • Loading branch information
lvignoli authored Feb 18, 2022
1 parent 7976013 commit 81a4312
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .mypy_script
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env bash
mypy --config-file .mypy.ini
26 changes: 26 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
repos:
- repo: https://github.com/psf/black
rev: 22.1.0
hooks:
- id: black-jupyter

- repo: https://github.com/PyCQA/flake8
rev: 4.0.1
hooks:
- id: flake8

- repo: https://github.com/pycqa/isort
rev: 5.10.1
hooks:
- id: isort
name: isort (python)

# Calling mypy from a bash script, as I cannot figure out how to pass the
# .mypy.ini config file when using the hook at
# https://github.com/pre-commit/mirrors-mypy
- repo: local
hooks:
- id: mypy
name: mypy
entry: ./.mypy_script
language: script
20 changes: 20 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,23 @@ annotations and pass the type checks from running:

**Note**: Type hints for `numpy` have only been added in version 1.20. Make sure you have `numpy >= 1.20`
installed before running the type checks.

### Use `pre-commit` to automate CI checks

[`pre-commit`](https://pre-commit.com/) is a tool to easily setup and manage [git hooks](https://git-scm.com/docs/githooks).

Run

```bash
pre-commit install --hook-type pre-push
```

to install `black`, `isort`, `flake8` and `mypy` hooks in your local repository (at `.git/hooks/` by defaults)
and run them automatically before any push to a remote git repository.
If an issue is found by these tools, the git hook will abort the push. `black` and `isort` hooks may reformat guilty files.

Disable the hooks with

```bash
pre-commit uninstall --hook-type pre-push
```
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ mypy == 0.921
pytest
pytest-cov

# CI
pre-commit

# tutorials
notebook
python-igraph
Expand Down

0 comments on commit 81a4312

Please sign in to comment.