Skip to content

Sanity and pre commit hooks

Qubaef edited this page Mar 19, 2021 · 1 revision

Pre-commit hooks enable us to run code-quality checks before committing files to repository. Their most important feature is that they don't allow you to make a commit unless all files staged for commit are passing checks. Hooks are a pure git-related mechanism, but we will use pre-commit Python module to install them.

Installation

  1. Activate your virtual environment
  2. Install pre-commit
    • pip install -r requirements-dev.txt
  3. Install pre-commit hooks configuration
    • pre-commit install

Hooks are now configured and will run before each commit!

Pre-commit hooks tips

  • By default, hooks will only run for files staged for commit
  • When hooks change the files, they are not automatically updated in files staged for commit, therefore you have to run git add -u to include those changes before trying to commit them again
  • To commit files without running hooks, execute git commit --no-verify (make sure you really want to do it!)
  • To run hooks on all files, execute pre-commit run --all-files

Currently configured hooks

In .pre-commit-config.yaml we have following check enabled:

  • check-docstring-first - checks for a common error of placing code before the docstring
  • check-json - attempts to load all json files to verify syntax
  • check-merge-conflict - check for files that contain merge conflict strings
  • check-yaml - attempts to load all yaml files to verify syntax
  • debug-statements - check for debugger imports and py37+ breakpoint() calls in python source
  • end-of-file-fixer - makes sure files end in a newline and only a newline
  • trailing-whitespace - trims trailing whitespace
  • requirements-txt-fixer - sorts entries in requirements.txt
  • flake8 - code linter
  • mypy - static typing analysis
  • black - code reformatter

Example output

Here is an example of how a successful run of pre-commit hooks looks like.

$ pre-commit run --all-files
black....................................................................Passed
Check docstring is first.................................................Passed
Check JSON...........................................(no files to check)Skipped
Check for merge conflicts................................................Passed
Check Yaml...............................................................Passed
Debug Statements (Python)................................................Passed
Fix End of Files.........................................................Passed
Trim Trailing Whitespace.................................................Passed
Fix requirements.txt.....................................................Passed
flake8...................................................................Passed
pyupgrade................................................................Passed
check for not-real mock methods..........................................Passed
type annotations not comments............................................Passed
mypy.....................................................................Passed

CI sanity checks

Our project has CI sanity checks enabled, meaning that on each push/pull request to main, it will run a set of tests to check if test seems to be of working quality. You will not be able to merge a pull request to main until it passes this tests. You can check your current CI run in Actions tab.

Currently configured CI checks

  • black
  • flake8
  • mypy
  • pytest

Wiki page copied from gradient-robomaster

Clone this wiki locally