Using Version Control Hooks¶
Usage with the pre-commit git hooks framework¶
Flake8 can be included as a hook for pre-commit. The easiest way to get
started is to add this configuration to your
- repo: https://gitlab.com/pycqa/flake8 rev: '' # pick a git hash / tag to point to hooks: - id: flake8
See the pre-commit docs for how to customize this configuration.
Checked-in python files will be passed as positional arguments.
will always lint explicitly passed arguments (
flake8 --exclude has
no effect). Instead use
exclude: ... regex to exclude
pre-commit won’t ever pass untracked files to
.tox / etc. is unnecessary.
- id: flake8 exclude: ^testing/(data|examples)/
pre-commit creates an isolated environment for hooks. To use
plugins, use the
- id: flake8 additional_dependencies: [flake8-docstrings]
Built-in Hook Integration¶
It is strongly suggested to use Flake8 via pre-commit over the
built-in hook mechanisms.
pre-commit smooths out many of the rough
git and is much more battle-tested than the Flake8
Flake8 can be integrated into your development workflow in many ways. A
default installation of Flake8 can install pre-commit hooks for both
Git and Mercurial. To install a built-in hook, you can use the
flake8 --install-hook command-line option. For example, you can
install a git pre-commit hook by running:
flake8 --install-hook git
This will install the pre-commit hook into
you can install the mercurial commit hook by running
flake8 --install-hook mercurial
By default, Flake8 does not prevent you from creating a commit with these hooks. Both hooks can be configured to be strict easily.
Both our Git and Mercurial hooks check for the presence of
in each VCS’ config. For example, you might configure this like so:
git config --bool flake8.strict true hg config flake8.strict true
Checking All Modified Files Currently Tracked¶
Mercurial does not have the concept of an index or “stage” as best as I understand.
Flake8 aims to make smart choices that keep things fast for users where
possible. As a result, the Flake8 Git pre-commit will default to only
checking files that have been staged (i.e., added to the index). If, however,
you are keen to be lazy and not independently add files to your git index, you
true (similar to how you would set
flake8.strict above) and this will check all tracked files.
This is to support users who often find themselves doing things like:
git commit -a
If you have files you have not yet added to the index, Flake8 will not
see these and will not check them for you. You must