Ned's script to check McCabe complexity.
This module provides a plugin for
flake8, the Python code checker.
You can install, upgrade, or uninstall
mccabe with these commands:
$ pip install mccabe $ pip install --upgrade mccabe $ pip uninstall mccabe
The complexity checker can be used directly:
$ python -m mccabe --min 5 mccabe.py ("185:1: 'PathGraphingAstVisitor.visitIf'", 5) ("71:1: 'PathGraph.to_dot'", 5) ("245:1: 'McCabeChecker.run'", 5) ("283:1: 'main'", 7) ("203:1: 'PathGraphingAstVisitor.visitTryExcept'", 5) ("257:1: 'get_code_complexity'", 5)
flake8 2+ and
mccabe are installed, the plugin is
$ flake8 --version 2.0 (pep8: 1.4.2, pyflakes: 0.6.1, mccabe: 0.2)
By default the plugin is disabled. Use the
--max-complexity switch to
enable it. It will emit a warning if the McCabe complexity of a function is
higher than the provided value:
$ flake8 --max-complexity 10 coolproject ... coolproject/mod.py:1204:1: C901 'CoolFactory.prepare' is too complex (14)
This feature is quite useful for detecting over-complex code. According to McCabe, anything that goes beyond 10 is too complex.
Flake8 has many features that mccabe does not provide. Flake8 allows users to
ignore violations reported by plugins with
# noqa. Read more about this in
To silence violations reported by
mccabe, place your
# noqa: C901 on
the function definition line, where the error is reported for (possibly a
- Feedback and ideas: http://mail.python.org/mailman/listinfo/code-quality
- Cyclomatic complexity: http://en.wikipedia.org/wiki/Cyclomatic_complexity
- Ned Batchelder's script: http://nedbatchelder.com/blog/200803/python_code_complexity_microtool.html
- McCabe complexity: http://en.wikipedia.org/wiki/Cyclomatic_complexity
- Drop support for all versions of Python lower than 3.6
- Add support for Python 3.8, 3.9, and 3.10
- Fix option declaration for Flake8
- Fix signature for
PathGraphingAstVisitor.defaultto match the signature for
- Add support for Python 3.6
- Fix handling for missing statement types
- Report actual column number of violation instead of the start of the line
- When opening files ourselves, make sure we always name the file variable
- Set default maximum complexity to -1 on the class itself
- PyCon 2016 PDX release
- Add support for Flake8 3.0
- Stop testing on Python 3.2
- Add support for async/await keywords on Python 3.5 from PEP 0492
- Always coerce the
max_complexityvalue from Flake8's entry-point to an integer.
- Computation was wrong: the mccabe complexity starts at 1, not 2.
max-complexityvalue is now inclusive. E.g.: if the value is 10 and the reported complexity is 10, then it passes.
- Add tests.
- Do not require
setuptoolsin setup.py. It works around an issue with
pipand Python 3.
- Rename project to
flake8.extensionsetuptools entry point.
max-complexityfrom the configuration file.
- Rename argument
- First release