Add pre-commit checks to a python repository
A total of 5 files must be added to repository. These files and their contents are listed below
Create a
requirements-dev.txtfile and add dev packages on this file
-r requirements.txt
flake8==4.0.1
mypy==0.910
ipdb==0.13.9
ipdbplugin==1.5.0
pip-tools==2.0.2
black==18.9b0
isort==4.3.15
pre-commit==2.16.0
honcho==1.0.1
CODE
2. Add a setup.cfg file → exclude can be customized based on your application
[flake8]
max-line-length=79
max-complexity=18
ignore=E123,E402,W503,E203
exclude= */migrations/*, ./bin/local_variables.py, .collected_static, .git, __init__.py, migrations
CODE
3. Add a pyproject.toml
[tool.black]
line-length = 79
target_version = ['py36']
exclude = '''
(
/(
\.eggs # exclude a few common directories in the
| \.git # root of the project
| \.hg
| \.mypy_cache
| \.tox
| \.venv
| _build
| buck-out
| build
| dist
| migrations
| assets
)/
)
'''
CODE
4. Add a pre-commit.yaml
repos:
- repo: https://github.com/psf/black
rev: 22.3.0
hooks:
- id: black
language_version: python3
- repo: https://github.com/PyCQA/isort
rev: 5.10.1
hooks:
- id: isort
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.1.0
hooks:
- id: trailing-whitespace
args: ['--markdown-linebreak-ext=md']
- id: check-json
- id: check-yaml
- id: mixed-line-ending
args: ['--fix=lf']
- id: end-of-file-fixer
- id: flake8
language_version: python3
- id: debug-statements
- id: check-ast
- id: check-merge-conflict
- repo: https://github.com/Lucas-C/pre-commit-hooks
rev: v1.1.14
hooks:
- id: forbid-crlf
- id: remove-crlf
- id: remove-tabs
CODE
5. Add a file isort.cfg → known_third_party can be customized based on your application
[settings]
multi_line_output=3
include_trailing_comma=True
force_grid_wrap=0
combine_as_imports=True
line_length=79
indent=' '
known_django=django,
known_third_party=rest_framework,djchoices,allauth,django_filters,blockcypher,celery,test_plus,factory,faker,pandas,pytz,requests,mock,dateutil,rest_framework_simplejwt,django_rest_passwordreset,infi,mixpanel,cashaddress,google,iso8601,decouple,locust,kombu
skip=migrations,pre-commit
sections=STDLIB,DJANGO,THIRDPARTY,FIRSTPARTY,LOCALFOLDER
CODE