Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# Summary: pre-commit hook configuration for TensorFlow Quantum.
# See https://pre-commit.com for documentation.
#
# Install hooks with:
# pip install pre-commit
# pre-commit install
#
# Run all hooks manually with:
# pre-commit run --all-files

default_language_version:
python: python3

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.6.0
hooks:
- id: trailing-whitespace
exclude: '\.ipynb$'
- id: end-of-file-fixer
exclude: '\.ipynb$'
- id: check-yaml
args: [--unsafe]
- id: check-json
exclude: '\.ipynb$'
- id: check-merge-conflict
- id: check-added-large-files
args: ['--maxkb=500']
- id: check-case-conflict
- id: mixed-line-ending
args: ['--fix=lf']
exclude: '\.ipynb$'
- id: no-commit-to-branch
args: ['--branch=master']

- repo: https://github.com/PyCQA/isort
rev: 5.13.2
hooks:
- id: isort
args: [--profile=google, --line-length=80]
files: '^(tensorflow_quantum|benchmarks)/.*\.py$'

- repo: https://github.com/pre-commit/mirrors-yapf
rev: v0.32.0
hooks:
- id: yapf
args: ['--style=google', '--in-place']
files: '^(tensorflow_quantum|benchmarks)/.*\.py$'
additional_dependencies: ['yapf==0.43.0']

- repo: https://github.com/PyCQA/pylint
rev: v3.3.3
hooks:
- id: pylint
args: ['--rcfile=.pylintrc']
files: '^tensorflow_quantum/.*\.py$'
additional_dependencies:
- 'cirq-core==1.3.0'
- 'numpy==1.24.2'
- 'sympy==1.12'

- repo: https://github.com/adrienverge/yamllint
rev: v1.35.1
hooks:
- id: yamllint
args: ['--config-file=.yamllint.yaml', '--strict']
files: '\.(yaml|yml)$'
exclude: '^\.pre-commit-config\.yaml$'

- repo: https://github.com/pre-commit/mirrors-clang-format
rev: v18.1.8
hooks:
- id: clang-format
args: ['-style=google']
files: '\.(cc|h)$'
types_or: [c, c++]

- repo: local
hooks:
- id: format-ipynb
name: format-ipynb
language: python
entry: python scripts/format_ipynb.py
files: '\.ipynb$'
pass_filenames: false
additional_dependencies:
- 'nbformat==5.1.3'
- 'yapf==0.43.0'
44 changes: 44 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,50 @@ to ensure that code has proper formatting and is lint free.
[clang format](https://clang.llvm.org/docs/ClangFormat.html) to ensure we have
consistent formatting everywhere.

### Pre-commit Hooks

This repository ships a [pre-commit](https://pre-commit.com/) configuration
(`.pre-commit-config.yaml`) that automates all of the checks above so they run
automatically before every `git commit`. We strongly recommend installing the
hooks when you set up your local development environment:

```bash
# Install the pre-commit tool (already listed in requirements.txt).
pip install pre-commit

# Register the hooks with your local Git clone – only needed once.
pre-commit install
```

After installation the hooks run automatically on each `git commit`. You can
also run them manually at any time:

```bash
# Run all hooks against every file in the repository.
pre-commit run --all-files

# Run a single hook (e.g. yapf formatting) against all files.
pre-commit run yapf --all-files
```

The hooks cover the following checks:

| Hook | What it does |
|------|-------------|
| `trailing-whitespace` | Removes trailing whitespace from source files |
| `end-of-file-fixer` | Ensures files end with a single newline |
| `check-yaml` / `check-json` | Validates YAML and JSON syntax |
| `check-merge-conflict` | Rejects unresolved merge-conflict markers |
| `check-added-large-files` | Rejects files larger than 500 KB |
| `mixed-line-ending` | Normalises all line endings to LF |
| `no-commit-to-branch` | Prevents direct commits to `master` |
| `isort` | Sorts Python imports (Google profile) |
| `yapf` | Formats Python source with Google style |
| `pylint` | Lints Python source using `.pylintrc` |
| `yamllint` | Lints YAML files using `.yamllint.yaml` |
| `clang-format` | Formats C++ / C source with Google style |
| `format-ipynb` | Formats Jupyter notebook code cells with yapf |

### Adding Modules

If you are adding new modules, be sure to properly expose them to the user
Expand Down
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,8 @@ wheel==0.46.2
wrapt==1.17.3
# via tensorflow
yapf==0.43.0
tensorflow==2.15.0
pre-commit==3.7.1
# via -r requirements.in

# The following packages are considered to be unsafe in a requirements file:
Expand Down
Loading