Skip to content

Instantly share code, notes, and snippets.

@churnikov
Created April 11, 2022 10:18
Show Gist options
  • Save churnikov/7a52a080fb8c42013155be0e2e0690fa to your computer and use it in GitHub Desktop.
Save churnikov/7a52a080fb8c42013155be0e2e0690fa to your computer and use it in GitHub Desktop.
How to setup pre-commit hooks
[flake8]
max-line-length = 120
inline-quotes = "
multiline-quotes = "
ignore = E203,W503
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.0.1
hooks:
- id: check-yaml
- id: check-toml
- id: debug-statements
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: https://github.com/PyCQA/isort
rev: 5.9.3
hooks:
- id: isort
args: [ "--profile", "black", "--filter-files" ]
- repo: https://github.com/psf/black
rev: 20.8b1
hooks:
- id: black
- repo: https://gitlab.com/PyCQA/flake8
rev: 3.9.2
hooks:
- id: flake8
args: [ "--config", ".flake8" ]
additional_dependencies: [ "flake8-quotes" ]
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.812
hooks:
- id: mypy

How to setup pre-commit hooks

  1. Take all the files from this gist and add them to the root of your project.
  2. Make the following changes:
  3. Change in pyproject.toml on lines 20 and 23 {{ service_name }} to your main module
  4. Add to your project pre-commit via pip install pre-commit
  5. Run pre-commit install

Now you can use pre-commit hooks

Pre-commit hooks help with creation of code with cosistent style between developers. They run before every commit. But they could be run manually:

$ git add .
$ pre-commit run --all-files
[mypy]
python_version = 3.8
no_strict_optional = True
ignore_missing_imports = True
[tool.black]
line-length = 120
target_version = ["py38"]
exclude = '''
/(
\.git
| \.hg
| \.mypy_cache
| \.tox
| \.venv
| _build
| build
| dist
)/
'''
[tool.isort]
profile = "black"
line_length = 120
src_paths = ["{{ service_name }}", "tests"]
skip_gitignore = true
filter_files = true
known_first_party = ["{{ service_name }}"]
known_third_party = [
"loguru",
"pytest",
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment