-
Notifications
You must be signed in to change notification settings - Fork 0
Sanity and pre commit hooks
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.
- Activate your virtual environment
- Install pre-commit
pip install -r requirements-dev.txt
- Install pre-commit hooks configuration
pre-commit install
Hooks are now configured and will run before each commit!
- 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 -uto 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
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
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
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.
blackflake8mypypytest
Wiki page copied from gradient-robomaster