From a48fae02c58180ce700ce3af2b101bacbc704eb8 Mon Sep 17 00:00:00 2001 From: Aniket Singh Yadav Date: Sun, 22 Feb 2026 12:06:32 +0530 Subject: [PATCH] add pre-commit run --- .pre-commit-config.yaml | 88 +++++++++++++++++++++++++++++++++++++++++ CONTRIBUTING.md | 44 +++++++++++++++++++++ requirements.txt | 1 + 3 files changed, 133 insertions(+) create mode 100644 .pre-commit-config.yaml diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 000000000..c2e9648fe --- /dev/null +++ b/.pre-commit-config.yaml @@ -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' diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 3d5b67e7e..86535361a 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -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 diff --git a/requirements.txt b/requirements.txt index 9fe2d0446..5045caa02 100644 --- a/requirements.txt +++ b/requirements.txt @@ -6,3 +6,4 @@ nbformat==5.1.3 pylint==3.3.3 yapf==0.43.0 tensorflow==2.15.0 +pre-commit==3.7.1