Skip to main content

pre-commit hooks for python projects using pyproject.toml.

Project description

pyproject-pre-commit

test test coverage

pre-commit hooks for python projects.

.pre-commit-hooks.yaml provides pre-defined ids which you just need to add these ids to your .pre-commit-config.yaml.

By installing pyproject-pre-commit package, all necessary tools are installed as dependencies.

Requirement

  • Python >= 3.9.0

Usage

Install pyproject-pre-commit

If your project uses poetry, do:

$ poetry add --group dev pyproject-pre-commit[ruff]

or uv, do:

$ poetry add --dev pyproject-pre-commit[ruff]

You can choose ruff or black as main linter/formatter by option.

If you want to use black, do:

$ poetry add --dev pyproject-pre-commit[black]

For black case, autoflake, autopep8, isort, flake8 and bandit are also installed.

If you wish to install all, do:

$ poetry add --dev pyproject-pre-commit[all]

If you use pip, do:

$ pip install pyproject-pre-commit[ruff]

This will install tools for pre-commit hooks in your working environment, so that you can use these tools, such as black, directly.

Prepare .pre-commit-config.yaml

Add https://github.com/rcmdnk/pyproject-pre-commit to your .pre-commit-config.yaml, like:

repos:
  - repo: https://github.com/rcmdnk/pyproject-pre-commit
    rev: v0.3.0
    hooks:
      - id: black-diff
      - id: black
      - id: blacken-docs
      - id: autoflake-diff
      - id: autoflake
      - id: autopep8-diff
      - id: autopep8
      - id: isort-diff
      - id: isort
      - id: flake8
      - id: bandit
      - id: mypy
      - id: shellcheck
      - id: mdformat-check
      - id: mdformat
      - id: actionlint
      - id: validate-pyproject

By using pyproject-pre-commit, you can simplify your .pre-commit-config.yaml that you need only repo of https://github.com/rcmdnk/pyproject-pre-commit.

These hooks uses local installation of tools, so pre-commit will use tools installed in your working environment.

This can be made by ppc command:

$ ppc --pre-commit > .pre-commit-config.yaml

[!NOTE] If you are using poetry, run poetry run ppc ... or run after poetry shell.

If you already have it, add hooks w/o repos: by

$ ppc --pre-commit |grep -v "^repos:" >> .pre-commit-config.yaml

You may want to modify after adding these configurations.

To use ruff instead of such as black, flake8, isort, add following hooks:

repos:
  - repo: https://github.com/rcmdnk/pyproject-pre-commit
    rev: v0.3.0
    hooks:
      - id: ruff-lint-diff
      - id: ruff-lint
      - id: ruff-format-diff
      - id: ruff-format
      - id: mypy
      - id: shellcheck
      - id: mdformat-check
      - id: mdformat
      - id: actionlint
      - id: validate-pyproject

This can be made by ppc command:

$ ppc --pre-commit --ruff > .pre-commit-config.yaml

Run pre-commit

pre-commit command is installed as dependencies of pyproject-pre-commit package.

After installing pyproject-pre-commit package, you can run pre-commit command.

First, install pre-commit hooks by:

$ pre-commit install

then you can run pre-commit by:

$ pre-commit run --all-files

[!NOTE] If you are using poetry, run poetry run pre-commit ... or run after poetry shell.

Available ids

You can find ids in .pre-commit-hooks.yaml.

There are ids for following tools:

All tools are installed as dependencies of pyproject-pre-commit package.

shellcheck and mdformat are given in addition to python tools as they can be managed by pip and most projects have README.md and some have shell scripts.

For tools which can format files, there are additional ids with -diff or --check which show the results before modifying files. You can see the differences after formatting if you place these ids before ids w/o --diff or --check.

Options for tools

You can set options in pyproject.toml for all tools above.

For flake8, flake8-pyproject allows to read options from pyproject.toml

About bandit, there is a plugin for the flake8, but plugin version does not read options from pyproject.toml even with pyproject.toml. Therefore, use bandit directly and give -c pyproject.toml option in the hooks.

pyproject.toml

You can set options in pyproject.toml for all tools.

Example options can be made by ppc command:

$ ppc --pyproject >> pyproject.toml

or

$ ppc --pyproject --ruff >> pyproject.toml

You may want to modify after adding these configurations.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyproject_pre_commit-0.5.2.tar.gz (123.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyproject_pre_commit-0.5.2-py3-none-any.whl (11.2 kB view details)

Uploaded Python 3

File details

Details for the file pyproject_pre_commit-0.5.2.tar.gz.

File metadata

  • Download URL: pyproject_pre_commit-0.5.2.tar.gz
  • Upload date:
  • Size: 123.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.3.1 CPython/3.14.2 Linux/6.11.0-1018-azure

File hashes

Hashes for pyproject_pre_commit-0.5.2.tar.gz
Algorithm Hash digest
SHA256 ec71bd6579d81a0068f8f1d578a9806da07c61820e1e9c1603bfe0f65a961b1f
MD5 e16184402614c1e7c300692b4324ade8
BLAKE2b-256 eca611d82e14025acc32047d0a103743688d0c98912075e4c15e9b6bb54a0449

See more details on using hashes here.

File details

Details for the file pyproject_pre_commit-0.5.2-py3-none-any.whl.

File metadata

  • Download URL: pyproject_pre_commit-0.5.2-py3-none-any.whl
  • Upload date:
  • Size: 11.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.3.1 CPython/3.14.2 Linux/6.11.0-1018-azure

File hashes

Hashes for pyproject_pre_commit-0.5.2-py3-none-any.whl
Algorithm Hash digest
SHA256 0df7ed1efdaa10ad860513c0278a3115666db96087086ba76d561274f9b095b8
MD5 81920306aa5fc2e564cdf87c9e21335e
BLAKE2b-256 ab93afc36a99b90d8c0d104a90112cd693d4508677deb66bff5bf62d88dab6fc

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page