Flake8¶
Flake8 is a wrapper around these tools:
- PyFlakes
- pep8
- Ned Batchelder’s McCabe script
Flake8 runs all the tools by launching the single flake8
script.
It displays the warnings in a per-file, merged output.
It also adds a few features:
files that contain this line are skipped:
# flake8: noqa
lines that contain a
# noqa
comment at the end will not issue warnings.a Git and a Mercurial hook.
a McCabe complexity checker.
extendable through
flake8.extension
entry points.
QuickStart¶
pip install flake8
To run flake8 just invoke it against any directory or Python module:
$ flake8 coolproject
coolproject/mod.py:97:1: F401 'shutil' imported but unused
coolproject/mod.py:625:17: E225 missing whitespace around operato
coolproject/mod.py:729:1: F811 redefinition of function 'readlines' from line 723
coolproject/mod.py:1028:1: F841 local variable 'errors' is assigned to but never used
The outputs of PyFlakes and pep8 (and the optional plugins) are merged and returned.
flake8 offers an extra option: –max-complexity, which will emit a warning if the McCabe complexity of a function is higher than the value. By default it’s deactivated:
$ flake8 --max-complexity 12 coolproject
coolproject/mod.py:97:1: F401 'shutil' imported but unused
coolproject/mod.py:625:17: E225 missing whitespace around operator
coolproject/mod.py:729:1: F811 redefinition of unused 'readlines' from line 723
coolproject/mod.py:939:1: C901 'Checker.check_all' is too complex (12)
coolproject/mod.py:1028:1: F841 local variable 'errors' is assigned to but never used
coolproject/mod.py:1204:1: C901 'selftest' is too complex (14)
This feature is quite useful to detect over-complex code. According to McCabe, anything that goes beyond 10 is too complex. See https://en.wikipedia.org/wiki/Cyclomatic_complexity.
Frequently Asked Questions¶
Why does flake8 pin the version of pep8?¶
Version 1.6 of pep8 doesn’t work properly with flake8. Until pep8 releases a version that works, flake8 pins the version of pep8 so that flake8 will work as a whole.
Is flake8 broken?¶
Flake8 combines two other projects that are significant on their own: pep8 and PyFlakes. If flake8 is doing something you don’t like, it is quite likely that the problem lies in one of those other projects. You can run them separately to see if they are the cause of your difficulties. We greatly appreciate your efforts to diagnose the source of the problem before reporting bugs against flake8.
Questions or Feedback¶
If you have questions you’d like to ask the developers, or feedback you’d like to provide, feel free to use the mailing list: code-quality@python.org We would love to hear from you. Additionally, if you have a feature you’d like to suggest, the mailing list would be the best place for it.
Documentation¶
- Configuration
- Warning / Error codes
- VCS Hooks
- Buildout integration
- Setuptools integration
- Flake8 API
- Writing an Extension for Flake8
- Existing Extensions
- Changes
- 2.6.0 - 2016-06-15
- 2.5.5 - 2016-06-14
- 2.5.4 - 2016-02-11
- 2.5.3 - 2016-02-11
- 2.5.2 - 2016-01-30
- 2.5.1 - 2015-12-08
- 2.5.0 - 2015-10-26
- 2.4.1 - 2015-05-18
- 2.4.0 - 2015-03-07
- 2.3.0 - 2015-01-04
- 2.2.5 - 2014-10-19
- 2.2.4 - 2014-10-09
- 2.2.3 - 2014-08-25
- 2.2.2 - 2014-07-04
- 2.2.1 - 2014-06-30
- 2.2.0 - 2014-06-22
- 2.1.0 - 2013-10-26
- 2.0.0 - 2013-02-23
- 1.7.0 - 2012-12-21
- 1.6.2 - 2012-11-25
- 1.6.1 - 2012-11-24
- 1.6 - 2012-11-16
- 1.5 - 2012-10-13
- 1.4 - 2012-07-12
- 1.3.1 - 2012-05-19
- 1.3 - 2012-03-12
- 1.2 - 2012-02-12
- 1.1 - 2012-02-14
- 1.0 - 2011-11-29
- 0.9 - 2011-11-09
- 0.8 - 2011-02-27
- 0.7 - 2010-02-18
- 0.6 - 2010-02-15
Original Projects¶
Flake8 is just a glue project, all the merits go to the creators and maintainers of the original projects: