Skip to content

Update pre-commit file and configuration #3412

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jun 17, 2025
Merged
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
2 changes: 2 additions & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@

# Run pre-commit to fix end-of-file-fixer and trailing-whitespace (#3418)
fe5a29b6942829ff2870181b6c1a68ebd2af9add
# Fix code style (#3423)
f9e8ca106a0d3f386f26f3a9481fd2eb341999f0
6 changes: 2 additions & 4 deletions .github/workflows/code-style.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@ on:
- "setup.cfg"
- "requirements-dev.txt"
- "pyproject.toml"
- "tests/run_code_style.sh"
- ".github/workflows/code-style.yml"
- "!assets/**"
- "!docker/**"
- "!docs/**"
- "!conda.recipe"


jobs:
code-style:
runs-on: ubuntu-latest
Expand All @@ -25,8 +23,8 @@ jobs:
with:
python-version: "3.11"
- run: |
bash ./tests/run_code_style.sh install
bash ./tests/run_code_style.sh fmt
pip install -r requirements-dev.txt
pre-commit run -a

- name: Commit and push changes
uses: stefanzweifel/git-auto-commit-action@v6
Expand Down
1 change: 0 additions & 1 deletion .github/workflows/gpu-hvd-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ on:
- "ignite/**"
- "tests/ignite/**"
- "tests/run_gpu_tests.sh"
- "tests/run_code_style.sh"
- "examples/**.py"
- "requirements-dev.txt"
- ".github/workflows/gpu-hvd-tests.yml"
Expand Down
1 change: 0 additions & 1 deletion .github/workflows/gpu-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ on:
- "ignite/**"
- "tests/ignite/**"
- "tests/run_gpu_tests.sh"
- "tests/run_code_style.sh"
- "examples/**.py"
- "requirements-dev.txt"
- ".github/workflows/gpu-tests.yml"
Expand Down
2 changes: 0 additions & 2 deletions .github/workflows/mps-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@ on:
paths:
- "ignite/**"
- "tests/ignite/**"
- "tests/run_code_style.sh"
- "examples/**.py"
- "requirements-dev.txt"
- ".github/workflows/mps-tests.yml"
pull_request:
paths:
- "ignite/**"
- "tests/ignite/**"
- "tests/run_code_style.sh"
- "examples/**.py"
- "requirements-dev.txt"
- ".github/workflows/mps-tests.yml"
Expand Down
2 changes: 0 additions & 2 deletions .github/workflows/tpu-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,13 @@ on:
- "ignite/**"
- "tests/ignite/**"
- "tests/run_tpu_tests.sh"
- "tests/run_code_style.sh"
- "requirements-dev.txt"
- ".github/workflows/tpu-tests.yml"
pull_request:
paths:
- "ignite/**"
- "tests/ignite/**"
- "tests/run_tpu_tests.sh"
- "tests/run_code_style.sh"
- "requirements-dev.txt"
- ".github/workflows/tpu-tests.yml"
workflow_dispatch:
Expand Down
5 changes: 2 additions & 3 deletions .github/workflows/unit-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,14 @@ jobs:

- name: Check code formatting
run: |
bash ./tests/run_code_style.sh install
bash ./tests/run_code_style.sh lint
pre-commit run -a

- name: Run Mypy
# https://github.com/pytorch/ignite/pull/2780
#
if: ${{ matrix.os == 'ubuntu-latest' }}
run: |
bash ./tests/run_code_style.sh mypy
mypy

# Download MNIST: https://github.com/pytorch/ignite/issues/1737
# to "/tmp" for unit tests
Expand Down
26 changes: 14 additions & 12 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
exclude: "^conda.recipe"
exclude: "^conda.recipe|assets|docs"
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v3.4.0
rev: v5.0.0
hooks:
- id: check-toml
- id: check-yaml
Expand All @@ -14,16 +14,18 @@ repos:
- id: prettier
exclude_types: ["python", "jupyter", "shell", "gitignore"]

- repo: https://github.com/omnilib/ufmt
rev: v2.7.3
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.11.13
hooks:
- id: ufmt
additional_dependencies:
- black == 24.10.0
- usort == 1.0.8.post1
# Run the linter
- id: ruff-check
types_or: [python, pyi]
# TODO: Enable when black and ruff format converge
# Run the formatter
# - id: ruff-format
# types_or: [python, pyi]

- repo: https://github.com/pycqa/flake8
rev: 7.1.1
- repo: https://github.com/psf/black
rev: 24.10.0
hooks:
- id: flake8
args: ["--config", "setup.cfg"]
- id: black
59 changes: 25 additions & 34 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ into the following categories:
- [Installation](#installation)
- [Code development](#code-development)
- [Codebase structure](#codebase-structure)
- [Formatting Code](#formatting-code)
- [Formatting without pre-commit](#formatting-without-pre-commit)
- [Formatting with pre-commit](#formatting-with-pre-commit)
- [Checking Code Style](#checking-code-style)
- [Formatting the code](#formatting-the-code)
- [Formatting the code with a pre-commit hook](#formatting-the-code-with-a-pre-commit-hook)
- [Run tests](#run-tests)
- [Run distributed tests only on CPU](#run-distributed-tests-only-on-cpu)
- [Run Mypy checks](#run-mypy-checks)
Expand Down Expand Up @@ -80,31 +80,33 @@ conda activate pytorch-ignite-dev

### Installation

1) Make a fork of the repository on the GitHub (see [here](https://github.com/firstcontributions/first-contributions#fork-this-repository) for details).
As a result, for example your username is `happy-ignite-developer`, then you should be able to see your fork on the GitHub, e.g https://github.com/happy-ignite-developer/ignite.git
1. Make a fork of the repository on the GitHub (see [here](https://github.com/firstcontributions/first-contributions#fork-this-repository) for details).
As a result, for example your username is `happy-ignite-developer`, then you should be able to see your fork on the GitHub, e.g https://github.com/happy-ignite-developer/ignite.git

2) Clone your fork locally and setup `upstream`. Assuming your username is `happy-ignite-developer`:
2. Clone your fork locally and setup `upstream`. Assuming your username is `happy-ignite-developer`:

```bash
git clone https://github.com/happy-ignite-developer/ignite.git
cd ignite
git remote add upstream https://github.com/pytorch/ignite.git
git remote -v
```

You might see the following output:

```
origin https://github.com/happy-ignite-developer/ignite.git (fetch)
origin https://github.com/happy-ignite-developer/ignite.git (push)
upstream https://github.com/pytorch/ignite (fetch)
upstream https://github.com/pytorch/ignite (push)
```
3) Sync and install all necessary dependencies:

3. Sync and install all necessary dependencies:

```bash
git pull upstream master
pip install -e .
pip install -r requirements-dev.txt
bash ./tests/run_code_style.sh install
```

### Code development
Expand Down Expand Up @@ -136,48 +138,39 @@ If you modify the code, you will most probably also need to code some tests to e
New code should be compatible with Python 3.X versions. Once you finish implementing a feature or bugfix and tests,
please run lint checking and tests:

#### Formatting Code
#### Checking Code Style

To ensure the codebase complies with a style guide, we use [flake8](https://flake8.pycqa.org/en/latest/)
and [ufmt](https://ufmt.omnilib.dev/) ([black](https://black.readthedocs.io/en/stable/) and
[usort](https://usort.readthedocs.io/en/stable/)) to format and check codebase for compliance with PEP8.
To ensure the codebase complies with the PEP8 style guide, we use [ruff](https://docs.astral.sh/ruff/)
and [black](https://black.readthedocs.io/en/stable/) to lint and format the codebase respectively.

##### Formatting without pre-commit
##### Formatting the code

If you choose not to use pre-commit, you can take advantage of IDE extensions configured to black format or invoke
black manually to format files and commit them.

To install `flake8`, `ufmt` and `mypy`, please run

```bash
bash ./tests/run_code_style.sh install
```
To automate the process, we have configured the repo with [pre-commit](https://pre-commit.com/).

To format files and commit changes:

```bash
# This should autoformat the files
bash ./tests/run_code_style.sh fmt
# This should lint and autoformat the files
pre-commit -a
# If everything is OK, then commit
git add .
git commit -m "Added awesome feature"
```

##### Formatting with pre-commit
##### Formatting the code with a pre-commit hook

To automate the process, we have configured the repo with [pre-commit hooks](https://pre-commit.com/) to use µfmt to autoformat the staged files to ensure every commit complies with a style guide. This requires some setup, which is described below:
To enable the `pre-commit` hooks follow the steps described below:

1. Install pre-commit in your python environment.
2. Run pre-commit install that configures a virtual environment to invoke ufmt and flake8 on commits.
1. Run `pre-commit install` to configures a virtual environment to invoke linters and formatters on commits.

```bash
pip install pre-commit
pre-commit install
```

3. When files are committed:
- If the stages files are not compliant with black or µsort, µfmt will autoformat the staged files. If this were to happen, files should be staged and committed again. See example code below.
- If the staged files are not compliant with flake8, errors will be raised. These errors should be fixed and the files should be committed again. See example code below.
2. When files are committed:
- If the stages files are not compliant, the tools autoformat the staged files. If this were to happen, files should be staged and committed again. See example code below.
- If the staged files are not compliant errors will be raised. These errors should be fixed and the files should be committed again. See example code below.

```bash
git add .
Expand Down Expand Up @@ -226,7 +219,7 @@ CUDA_VISIBLE_DEVICES="" pytest --dist=each --tx $WORLD_SIZE*popen//python=python
To run mypy to check the optional static type:

```bash
bash ./tests/run_code_style.sh mypy
mypy
```

To change any config for specif folder, please see the file mypy.ini
Expand Down Expand Up @@ -292,9 +285,7 @@ for formatting docstrings, specially from an example of `Google style with Pytho
- [`.. versionchanged::`] directive for adding new arguments, changing internal behaviours, fixing bugs and
- [`.. deprecated::`] directive for deprecations.

Examples: ``versionadded`` usage [link](https://github.com/pytorch/ignite/blob/52c69251dd9d97c32da1df0477ec3854e5702029/ignite/handlers/state_param_scheduler.py#L24), ``versionchanged`` usage [link](https://github.com/pytorch/ignite/blob/d2020e4e253ac1455a757c2db895c68ccfd2b958/ignite/metrics/metric.py#L281-L282)


Examples: `versionadded` usage [link](https://github.com/pytorch/ignite/blob/52c69251dd9d97c32da1df0477ec3854e5702029/ignite/handlers/state_param_scheduler.py#L24), `versionchanged` usage [link](https://github.com/pytorch/ignite/blob/d2020e4e253ac1455a757c2db895c68ccfd2b958/ignite/metrics/metric.py#L281-L282)

Length of line inside docstrings block must be limited to 120 characters.

Expand Down
Loading
Loading