pre-commit hooks for python projects using pyproject.toml.
Project description
pyproject-pre-commit
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.8.1
- Poetry (For development)
Usage
Install pyproject-pre-commit
If your project uses poetry, do:
$ poetry add --group dev pyproject-pre-commit
Otherwise, install the package in your working environment.
If you use pip, do:
$ pip install pyproject-pre-commit
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.1.1
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
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 afterpoetry 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.
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 afterpoetry shell
.
Available ids
You can find ids in .pre-commit-hooks.yaml.
There are ids for following tools:
- For Python
- black-diff: Just show Black result.
- black: Black: The uncompromising Python code formatter.
- blacken-docs: Run
black
on python code blocks in documentation files. - autoflake-diff: Just show autoflake result.
- autoflake: autoflake removes unused imports and unused variables from Python code.
- autopep8-diff: Just show autopep8.
- autopep8: autopep8 automatically formats Python code to conform to the PEP 8 style guide.
- isort-diff: Just show isort result.
- isort: isort your imports, so you don't have to.
- flake8:
flake8
is a command-line utility for enforcing style consistency across Python projects. - bandit: Bandit is a tool for finding common security issues in Python code.
- mypy: Mypy is a static type checker for Python.
- For Shell script
- shellcheck: ShellCheck - A shell script static analysis tool
- For Markdown
- mdformat-check: Just show mdformat result.
- mdformat: CommonMark compliant Markdown formatter.
- with following plugins:
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
You may want to modify after adding these configurations.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file pyproject_pre_commit-0.2.3.tar.gz
.
File metadata
- Download URL: pyproject_pre_commit-0.2.3.tar.gz
- Upload date:
- Size: 9.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.2 CPython/3.12.2 Darwin/23.6.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5c2dafa4f0e614e3782c6314254654a2f99081bd87c8a145477a8711bc8c533c |
|
MD5 | b37d36d5e58143d537cf946d594f27f9 |
|
BLAKE2b-256 | 473c147e6eee423faec03ae1f761726dcb87db3d4578b7ed2b9070424e1062c8 |
File details
Details for the file pyproject_pre_commit-0.2.3-py3-none-any.whl
.
File metadata
- Download URL: pyproject_pre_commit-0.2.3-py3-none-any.whl
- Upload date:
- Size: 9.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.2 CPython/3.12.2 Darwin/23.6.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0909bb2430238e8d7ee6e2e04cb6f468b21bc7ef3528ec3b3cd65c0dc11c769b |
|
MD5 | a3e86bec2970de30f1f6ec7ce2f4855d |
|
BLAKE2b-256 | 64dd9eea27d6e06646cc243488b88565741a2661be028ade5d6bd2c9f15f06b0 |