McCabe Code Complexity Checker

0.7.0 · active · verified Sat Mar 28

McCabe is a Python library that provides a plugin for flake8, the Python code checker. It's used to analyze the cyclomatic complexity (McCabe complexity) of functions and methods in Python code, helping developers identify potentially over-complex code that might be difficult to understand, test, or maintain. The current version is 0.7.0. Its release cadence is slow, often aligning with updates to supported Python versions.

Warnings

Install

Imports

Quickstart

McCabe is most commonly used as a plugin for `flake8`. After installing both `mccabe` and `flake8`, you enable McCabe checks by running `flake8` with the `--max-complexity` option, specifying the threshold at which a warning (C901) should be emitted. Alternatively, it can be run as a standalone script using `python -m mccabe`.

# To analyze a file using mccabe via flake8:
# 1. Install both libraries
# pip install mccabe flake8

# 2. Run flake8 with the --max-complexity flag
# (e.g., to flag functions with complexity > 10)
# flake8 --max-complexity 10 your_module.py

# Example of a simple Python file to check:
# your_module.py
def high_complexity_function(a, b):
    if a > 0:
        if b > 0:
            print('Both positive')
        else:
            print('A positive, B not')
    elif a < 0:
        if b < 0:
            print('Both negative')
        else:
            print('A negative, B not')
    else:
        print('A is zero')

# To use mccabe as a standalone script (less common):
# python -m mccabe --min 5 your_module.py

view raw JSON →