Pre-Commit Hooks

Pre-commit hooks are checks and syntax formatters that run upon every commit. If one of the hooks fails, the commit is aborted and you have to commit again after you resolved the issues raised by the hooks. Pre-commit hooks are defined in the .pre-commit-config.yaml. If you opt for the basic pre-commits, the following checks will be installed into your project:

  • reorder-python-imports: Reorders your python imports according to PEP8 guidelines.

  • check-yaml: Checks whether all .yaml and .yml files wihtin your project are valid yaml files.

  • check-added-large-files: Checks that all committed files do not exceed 100MB in size. This is the maximal file size allowed by Github.

  • check-byte-order-marker: Fails if file has a UTF-8 byte-order marker.

  • check-json: Checks whether all files that end with .json are indeed valid json files.

  • pyupgrade: Converts Python code to make use of newer syntax.

  • pretty-format-json: Reformats your json files to be more readable.

  • trailing-whitespace: Removes trailing whitespaces in all your text files.

  • black: Runs the python code formatter black on all your comitted python files.

  • blacken-docs: Formats python code (according to black’s formatting style) that occurs within documentation files.

If you additionally opt for intrusive pre-commit hooks, then python syntax linter flake8 will be installed as pre-commit hook as well. It is important to note that flake8 is quite strict regarding PEP8 Style Guide adherence and -as opposed to black- it only raises issues but does not automatically resolve them. You have to fix the issues yourself.

Note

If you want to skip the pre-commit hooks for a particular commit, you can run:

$ git commit -am <your commit message> --no-verify

For more advanced usages of pre-commit please consult its website.