Add pre-commit hooks for more standardized development
See original GitHub issue🚀 Feature Add pre-commit hooks through the “pre-commit” framework. https://pre-commit.com/
Motivation
Basically it is a very nice way to reduce code issues: formatting, old code style, mutable parameters, etc. There are literally hundreds of checks out there, and the installation is trivial. Every developer on their computer needs two extra lines: pip install pre-commit (enter the torchio git directory) pre-commit install
The code styles, checks, etc., are defined in the .pre-commit-config.yaml, so it is project-wise standardized.
Pitch
It is good.
Alternatives Like without CI: good-old non-testing.
Additional context
An example what i use elsewhere, but it shows how to configure the project. I will open a PR with a simple one which doesn’t use auto-formating, only other issues. Autopep8 or black or others could automatically enforce style, but you need to define your own, and then we can put it into the config file.
repos:
- repo: local
hooks:
- id: exclude-unwanted
name: exclude unwanted files, e.g. training data
entry: exclude unwanted files, e.g. training data
language: fail
files: '.*png|.*xlsx'
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.3.0
hooks:
- id: trailing-whitespace
- id: check-added-large-files
- id: check-case-conflict
- id: check-ast
- id: check-executables-have-shebangs
- id: check-json
- id: check-merge-conflict
- id: check-symlinks
- id: check-toml
- id: check-xml
- id: check-vcs-permalinks
- id: check-xml
- id: detect-private-key
- id: end-of-file-fixer
- id: pretty-format-json
- id: requirements-txt-fixer
- id: sort-simple-yaml
- id: double-quote-string-fixer
- id: mixed-line-ending
- id: debug-statements
- id: pretty-format-json
args: ['--autofix']
- id: flake8
additional_dependencies: [
flake8-bandit,
flake8-blind-except,
flake8-breakpoint,
flake8-broken-line,
flake8-bugbear,
flake8-builtins,
flake8-class-newline,
flake8-comprehensions,
flake8-debugger,
flake8-eradicate,
flake8-logging-format,
flake8-pep3101,
flake8-polyfill,
flake8-pytest,
flake8-pytest-style,
flake8-quotes,
flake8-string-format,
flake8-type-annotations,
flake8-use-fstring,
flake8-fixme,
flake8-markdown,
flake8-2020,
flake8-print,
pep8-naming]
args: ['--max-line-length=160', '--ignore=E801,E501,E402,E116,E265,E203,W503,N806,E800,N812,N816']
- repo: https://github.com/pre-commit/pygrep-hooks
rev: v1.4.1
hooks:
- id: python-check-blanket-noqa
- id: python-no-log-warn
- id: rst-backticks
- repo: https://github.com/asottile/pyupgrade
rev: v1.23.0
hooks:
- id: pyupgrade
args: ['--py36-plus']
- repo: https://github.com/pre-commit/mirrors-autopep8
rev: v1.4.4
hooks:
- id: autopep8
args: ['--ignore=E402,E265,E112,E115,E116,E261,E262', '--select=E226', '-aaa', '--max-line-lengt=160', '--in-place']
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:7 (7 by maintainers)
Top GitHub Comments
Advantage: a million things. 😃 But mostly: tox is for setting up a standardized virtual environment where tests and linters can run. pre-commit is for defining linters. And many people call pre-commit from tox. 😃 Here is a quick intro video: https://www.youtube.com/watch?v=KKJL8bM4cis
But if you only run tox in CI (not with precommit hook), then having a smaller but fast pre-commit is really helpful. If you run tox anyway, then adding pre-commit for tox is nice.
Some features: E.g. you can define that a given file cannot be committed, e.g. because it contains an AWS key, or SSH key. Or because it is a dicom. You can add pretty advanced checks, e.g. checking png files, if they can be compressed before you commit, or if they are valid. These pre-made checks are just wonderful and there are very many.
But there is also a mechanism for auto-formatting yaml, json, python, etc. It is work in progress, but the same config file will be used (if enabled) for a pre-commit github bot, (see pre-commit.ci) where the idea is: if the incoming pull request is bad, then detect it. Bad: if possible, fix it, e.g. it can reformat the pull request when it arrives. But if fixing is not possible, it will just give you an error message.
One upselling: among others the tox developers also use pre-commit. 😃 https://github.com/tox-dev/tox/blob/master/.pre-commit-config.yaml
Of course, you could add these linters and rules to tox, but it becomes quite complicated and verbose. E.g. when you need a few dependencies for a given linter, but it collides with a package from another linter. pre-commit handles these, because they all go into different virtual environments. The rules/tools can be automatically updated with
pre-commit autoupdate
, etc.Check out the video and/or a few example, pre-commit is just great, and a nice github integration is coming. 😃
Yes I have already been tinkering a bit with the default config 😃