KEMBAR78
Helpful pre commit hooks for Python and Django | PDF
Thomas Aglassinger
https://roskakori.at
Helpful pre-commit
hooks for Python and
Django
What is pre-commit?
●
A framework to manage git pre-commit hooks.
●
To perform quality check on code before accepting a
commit.
●
Many handy hooks available through it.
●
Helps you catch many (often minor issues) before they make
it into the repository.
How to get pre-commit
●
Visit https://pre-commit.com/
●
Various methods to install:
– Plain Python: pip install pre-commit
– Anaconda: conda install -c conda-forge pre-commit
– Ubuntu: sudo apt-get install pre-commit
– MacOS: brew install pre-commit
Configuration
●
Add a file “.pre-commit-config”
●
Add the github repositories and hooks you want to run before each commit.
●
Example:
●
Run “pre-commit install”
●
Hooks are checked on
“git commit ...” and commit in IDE
●
Manually run hooks using
“pre-commit run --all-files”
exclude: "^.idea"
repos:
- repo: git://github.com/pre-commit/pre-commit-hooks
rev: v3.2.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: mixed-line-ending
args: ["--fix=auto"]
Configuration
●
Add a file “.pre-commit-config”
●
Add the github repositories and hooks you want to run
before each commit.
●
Example: exclude: "^.idea"
repos:
- repo: git://github.com/pre-commit/pre-commit-hooks
rev: v3.2.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: mixed-line-ending
args: ["--fix=auto"]
Regex for files and
folders not to check
Source code repository
and version of hooks
to use
Hooks to use
Parameters for a
specific hook
Standard hooks – Text files
●
- repo: git://github.com/pre-commit/pre-commit-hooks
rev: v3.2.0
●
trailing-whitespace: remove white space at end of line in
text files
●
mixed-line-ending: detect or fix mixed line endings (e.g
developers on Windows and Unix working on same code)
●
end-of-file-fixer: Ensures that text files end with a newline
character.
Example hooks
Standard hooks – Unwanted files
●
check-byte-order-marker: rejects text files starting with
UTF-8 BOM (as created by various Microsoft tools like
Notepad, causing many Unix tools to fail)
●
check-added-large-files: rejects large files; possibly specify
what is large: args: ["--maxkb=768"]
●
Check-merge-conflict: refuse to commit before all merge
conflicts are resolved
Standard hooks - Python
●
check-ast: check Python syntax
●
requirements-txt-fixer: sorts entries in requirements.txt
●
debug-statements: refuse to commit calls to breakpoints()
and imports of debugger
Standard hooks – Syntax checks
●
check-json
●
check-toml
●
check-xml
●
check-yaml
Pretty print Python
●
Using black (opinionated): https://github.com/psf/black
●
Configure using pyproject.toml
- repo: https://github.com/ambv/black
rev: stable
hooks:
- id: black
[tool.black]
line-length = 120
include = '.pyi?$'
exclude = '''
/(
.eggs
| .git
| venv
| build
| dist
)/
'''
Sort imports consistently
●
Using isort:
https://github.com/timothycrosley/isort
●
Configure using setup.cfg
●
Can be tricky to work with black
→ see example to the right
- repo: https://github.com/timothycrosley/isort
rev: 5.4.2
hooks:
- id: isort
[tool:isort]
# Use project line length.
line_length = 120
# Packages from own projetc
default_section = THIRDPARTY
known_first_party = mytool, niftysite
# Use same style as black.
force_grid_wrap=0
include_trailing_comma = True
multi_line_output=3
use_parentheses=True
# Skip virtual environments.
skip_glob = venv
Flake8 – Semantic Python check
●
Find unused imports, shadowed variables, string formatting,
unreachable code, …
●
Configure using setup.cfg
- repo: https://gitlab.com/pycqa/flake8
rev: 3.8.3
hooks:
- id: flake8
[flake8]
max-line-length = 120
ignore = E203, W503
exclude =
.git
build
dist
venv
docs/conf.py
Prettier – Pretty print various formats
for web development
●
Supports JavaScript, CSS, Markdown, HTML, GraphQL, YAML
and others by means of plugins
●
Does not work with Django HTML templates → exclude using
“.prettierignore” (uses same format as .gitignore)
- repo: https://github.com/prettier/prettier
rev: 2.0.5
hooks:
- id: prettier
**/coverage.xml
**/templates/**/*.html
Summary
●
Pre-commit is easy to install and configure.
●
Solves minor minor issues arising when multiple developers
work on the same code.
●
Plenty of available hooks.

Helpful pre commit hooks for Python and Django

  • 1.
  • 2.
    What is pre-commit? ● Aframework to manage git pre-commit hooks. ● To perform quality check on code before accepting a commit. ● Many handy hooks available through it. ● Helps you catch many (often minor issues) before they make it into the repository.
  • 3.
    How to getpre-commit ● Visit https://pre-commit.com/ ● Various methods to install: – Plain Python: pip install pre-commit – Anaconda: conda install -c conda-forge pre-commit – Ubuntu: sudo apt-get install pre-commit – MacOS: brew install pre-commit
  • 4.
    Configuration ● Add a file“.pre-commit-config” ● Add the github repositories and hooks you want to run before each commit. ● Example: ● Run “pre-commit install” ● Hooks are checked on “git commit ...” and commit in IDE ● Manually run hooks using “pre-commit run --all-files” exclude: "^.idea" repos: - repo: git://github.com/pre-commit/pre-commit-hooks rev: v3.2.0 hooks: - id: trailing-whitespace - id: end-of-file-fixer - id: mixed-line-ending args: ["--fix=auto"]
  • 5.
    Configuration ● Add a file“.pre-commit-config” ● Add the github repositories and hooks you want to run before each commit. ● Example: exclude: "^.idea" repos: - repo: git://github.com/pre-commit/pre-commit-hooks rev: v3.2.0 hooks: - id: trailing-whitespace - id: end-of-file-fixer - id: mixed-line-ending args: ["--fix=auto"] Regex for files and folders not to check Source code repository and version of hooks to use Hooks to use Parameters for a specific hook
  • 6.
    Standard hooks –Text files ● - repo: git://github.com/pre-commit/pre-commit-hooks rev: v3.2.0 ● trailing-whitespace: remove white space at end of line in text files ● mixed-line-ending: detect or fix mixed line endings (e.g developers on Windows and Unix working on same code) ● end-of-file-fixer: Ensures that text files end with a newline character.
  • 7.
  • 8.
    Standard hooks –Unwanted files ● check-byte-order-marker: rejects text files starting with UTF-8 BOM (as created by various Microsoft tools like Notepad, causing many Unix tools to fail) ● check-added-large-files: rejects large files; possibly specify what is large: args: ["--maxkb=768"] ● Check-merge-conflict: refuse to commit before all merge conflicts are resolved
  • 9.
    Standard hooks -Python ● check-ast: check Python syntax ● requirements-txt-fixer: sorts entries in requirements.txt ● debug-statements: refuse to commit calls to breakpoints() and imports of debugger
  • 10.
    Standard hooks –Syntax checks ● check-json ● check-toml ● check-xml ● check-yaml
  • 11.
    Pretty print Python ● Usingblack (opinionated): https://github.com/psf/black ● Configure using pyproject.toml - repo: https://github.com/ambv/black rev: stable hooks: - id: black [tool.black] line-length = 120 include = '.pyi?$' exclude = ''' /( .eggs | .git | venv | build | dist )/ '''
  • 12.
    Sort imports consistently ● Usingisort: https://github.com/timothycrosley/isort ● Configure using setup.cfg ● Can be tricky to work with black → see example to the right - repo: https://github.com/timothycrosley/isort rev: 5.4.2 hooks: - id: isort [tool:isort] # Use project line length. line_length = 120 # Packages from own projetc default_section = THIRDPARTY known_first_party = mytool, niftysite # Use same style as black. force_grid_wrap=0 include_trailing_comma = True multi_line_output=3 use_parentheses=True # Skip virtual environments. skip_glob = venv
  • 13.
    Flake8 – SemanticPython check ● Find unused imports, shadowed variables, string formatting, unreachable code, … ● Configure using setup.cfg - repo: https://gitlab.com/pycqa/flake8 rev: 3.8.3 hooks: - id: flake8 [flake8] max-line-length = 120 ignore = E203, W503 exclude = .git build dist venv docs/conf.py
  • 14.
    Prettier – Prettyprint various formats for web development ● Supports JavaScript, CSS, Markdown, HTML, GraphQL, YAML and others by means of plugins ● Does not work with Django HTML templates → exclude using “.prettierignore” (uses same format as .gitignore) - repo: https://github.com/prettier/prettier rev: 2.0.5 hooks: - id: prettier **/coverage.xml **/templates/**/*.html
  • 15.
    Summary ● Pre-commit is easyto install and configure. ● Solves minor minor issues arising when multiple developers work on the same code. ● Plenty of available hooks.