mirror of
https://github.com/PyCQA/flake8.git
synced 2026-03-29 10:36:53 +00:00
made complexity an option (deactivated by default)
This commit is contained in:
parent
d0dc42f37a
commit
a6e2297838
4 changed files with 69 additions and 19 deletions
64
README
64
README
|
|
@ -18,15 +18,40 @@ It also adds a few features:
|
|||
|
||||
# flake8: noqa
|
||||
|
||||
- lines that contains a "# NOQA" comment at the end will not issue a warning
|
||||
- a Mercurial hook
|
||||
- more things to come..
|
||||
- lines that contains a "# NOQA" comment at the end will not issue a warning.
|
||||
- a Mercurial hook.
|
||||
- a McCabe complexity checker.
|
||||
|
||||
Original projects:
|
||||
QuickStart
|
||||
==========
|
||||
|
||||
- pep8: http://github.com/jcrocholl/pep8/
|
||||
- PyFlakes: http://divmod.org/trac/wiki/DivmodPyflakes
|
||||
- McCabe: http://nedbatchelder.com/blog/200803/python_code_complexity_microtool.html
|
||||
To run flake8 just invoke it against any directory or Python module::
|
||||
|
||||
$ flake8 coolproject
|
||||
coolproject/mod.py:1027: local variable 'errors' is assigned to but never used
|
||||
coolproject/mod.py:97: 'shutil' imported but unused
|
||||
coolproject/mod.py:729: redefinition of function 'readlines' from line 723
|
||||
coolproject/mod.py:1028: local variable 'errors' is assigned to but never used
|
||||
coolproject/mod.py:625:17: E225 missing whitespace around operato
|
||||
|
||||
The output of PyFlakes *and* pep8 is merged and returned.
|
||||
|
||||
flake8 offers an extra option: --max-complexity, which will emit a warning if the
|
||||
McCabe complexityu of a function is higher that the value. By default it's
|
||||
deactivated::
|
||||
|
||||
$ bin/flake8 --max-complexity 12 flake8
|
||||
coolproject/mod.py:97: 'shutil' imported but unused
|
||||
coolproject/mod.py:729: redefinition of function 'readlines' from line 723
|
||||
coolproject/mod.py:1028: local variable 'errors' is assigned to but never used
|
||||
coolproject/mod.py:625:17: E225 missing whitespace around operator
|
||||
coolproject/mod.py:452:1: 'missing_whitespace_around_operator' is too complex (18)
|
||||
coolproject/mod.py:939:1: 'Checker.check_all' is too complex (12)
|
||||
coolproject/mod.py:1204:1: '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.
|
||||
|
||||
|
||||
Mercurial hook
|
||||
|
|
@ -41,18 +66,37 @@ like this::
|
|||
|
||||
[flake8]
|
||||
strict = 0
|
||||
complexity = 12
|
||||
|
||||
|
||||
If *strict* option is set to **1**, any warning will block the commit. When
|
||||
*strict* is set to **0**, warnings are just displayed in the standard output.
|
||||
|
||||
*complexity* defines the maximum McCabe complexity allowed before a warning
|
||||
is emited. If you don't specify it it's just ignored. If specified, must
|
||||
be a positive value. 12 is usually a good value.
|
||||
|
||||
|
||||
Original projects
|
||||
=================
|
||||
|
||||
Flake8 is just a glue project, all the merits go to the creators of the original
|
||||
projects:
|
||||
|
||||
- pep8: http://github.com/jcrocholl/pep8/
|
||||
- PyFlakes: http://divmod.org/trac/wiki/DivmodPyflakes
|
||||
- McCabe: http://nedbatchelder.com/blog/200803/python_code_complexity_microtool.html
|
||||
|
||||
|
||||
CHANGES
|
||||
=======
|
||||
|
||||
1.0 - ?
|
||||
-------
|
||||
1.0 - 2011-11-29
|
||||
----------------
|
||||
|
||||
- Deactivates by default the complexity checker
|
||||
- Introduces the complexity option in the HG hook and the command line.
|
||||
|
||||
??
|
||||
|
||||
0.9 - 2011-11-09
|
||||
----------------
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
#!/home/tarek/dev/bitbucket.org/flk8/bin/python
|
||||
#!/home/tarek/dev/bitbucket.org/flake8-clean/bin/python
|
||||
from flake8.run import main
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
|
|
|||
|
|
@ -1264,6 +1264,8 @@ def process_options(arglist=None):
|
|||
global options, args
|
||||
parser = OptionParser(version=__version__,
|
||||
usage="%prog [options] input ...")
|
||||
parser.add_option('--max-complexity', default=-1, action='store',
|
||||
type='int', help="McCabe complexity treshold")
|
||||
parser.add_option('-v', '--verbose', default=0, action='count',
|
||||
help="print status messages, or debug with -vv")
|
||||
parser.add_option('-q', '--quiet', default=0, action='count',
|
||||
|
|
|
|||
|
|
@ -12,16 +12,18 @@ from flake8 import pyflakes
|
|||
from flake8 import mccabe
|
||||
|
||||
|
||||
def check_file(path, complexity=10):
|
||||
def check_file(path, complexity=-1):
|
||||
warnings = pyflakes.checkPath(path)
|
||||
warnings += pep8.input_file(path)
|
||||
warnings += mccabe.get_module_complexity(path, complexity)
|
||||
if complexity > -1:
|
||||
warnings += mccabe.get_module_complexity(path, complexity)
|
||||
return warnings
|
||||
|
||||
|
||||
def check_code(code, complexity=10):
|
||||
def check_code(code, complexity=-1):
|
||||
warnings = pyflakes.check(code, '<stdin>')
|
||||
warnings += mccabe.get_code_complexity(code, complexity)
|
||||
if complexity > -1:
|
||||
warnings += mccabe.get_code_complexity(code, complexity)
|
||||
return warnings
|
||||
|
||||
|
||||
|
|
@ -43,13 +45,14 @@ def _get_python_files(paths):
|
|||
|
||||
def main():
|
||||
options, args = pep8.process_options()
|
||||
complexity = options.max_complexity
|
||||
warnings = 0
|
||||
if args:
|
||||
for path in _get_python_files(args):
|
||||
warnings += check_file(path)
|
||||
warnings += check_file(path, complexity)
|
||||
else:
|
||||
stdin = sys.stdin.read()
|
||||
warnings += check_code(stdin)
|
||||
warnings += check_code(stdin, complexity)
|
||||
|
||||
raise SystemExit(warnings > 0)
|
||||
|
||||
|
|
@ -84,10 +87,11 @@ def hg_hook(ui, repo, **kwargs):
|
|||
pep8.options.physical_checks = pep8.find_checks('physical_line')
|
||||
pep8.options.logical_checks = pep8.find_checks('logical_line')
|
||||
pep8.args = []
|
||||
|
||||
complexity = ui.configint('flake8', 'complexity', default=-1)
|
||||
warnings = 0
|
||||
|
||||
for file_ in _get_files(repo, **kwargs):
|
||||
warnings += check_file(file_)
|
||||
warnings += check_file(file_, complexity)
|
||||
|
||||
strict = ui.configbool('flake8', 'strict', default=True)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue