McCabe Code Complexity Checker

raw JSON →
0.7.0 verified Tue May 12 auth: no python install: verified quickstart: verified

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.

pip install mccabe flake8
error C901 'function_name' is too complex (X)
cause The McCabe cyclomatic complexity of the function or method exceeds the configured maximum threshold, indicating potentially overly complex code that might be hard to understand or test.
fix
Refactor the function to reduce its complexity, or explicitly set a higher --max-complexity threshold when running flake8, or add # noqa: C901 to the line defining the function to ignore the warning for that specific function. For example, flake8 --max-complexity 10 your_project/
error flake8: error: no such option: --max-complexity
cause This error occurs when the `flake8` command is run with the `--max-complexity` option, but either the `mccabe` plugin is not installed or `flake8` is an older version that does not recognize this option, or `mccabe` is not properly integrated with `flake8`.
fix
Ensure both flake8 and mccabe are installed and up-to-date (pip install --upgrade flake8 mccabe). If the problem persists, try reinstalling them: pip uninstall flake8 mccabe; pip install flake8 mccabe.
error ModuleNotFoundError: No module named 'mccabe'
cause The `mccabe` package is not installed in the Python environment being used, or the Python interpreter cannot find it in its configured paths.
fix
Install the mccabe package using pip: pip install mccabe. If using a virtual environment, ensure it is activated before installation.
breaking Version 0.7.0 and later dropped support for Python versions older than 3.6.
fix Ensure your project runs on Python 3.6 or newer when using mccabe 0.7.0 or later.
gotcha When used with flake8, the mccabe plugin is disabled by default. You must explicitly enable it by setting the maximum complexity threshold.
fix Use the `--max-complexity N` flag when running `flake8` (e.g., `flake8 --max-complexity 10`). Alternatively, configure it in a `setup.cfg`, `.flake8`, or `tox.ini` file under the `[flake8]` section, e.g., `max-complexity = 10`.
gotcha To ignore a specific McCabe complexity violation (C901) for a function, the `# noqa: C901` comment must be placed on the function definition line, not just any line within the function.
fix Place `# noqa: C901` directly on the line where the `def` keyword or the decorator above it appears for the function you want to ignore.
pip install mccabe
python os / libc status wheel install import disk
3.10 alpine (musl) - - 0.03s 17.8M
3.10 alpine (musl) - - 0.03s 19.2M
3.10 slim (glibc) - - 0.01s 18M
3.10 slim (glibc) - - 0.01s 20M
3.11 alpine (musl) - - 0.03s 19.6M
3.11 alpine (musl) - - 0.04s 21.4M
3.11 slim (glibc) - - 0.04s 20M
3.11 slim (glibc) - - 0.02s 22M
3.12 alpine (musl) - - 0.02s 11.5M
3.12 alpine (musl) - - 0.02s 13.2M
3.12 slim (glibc) - - 0.04s 12M
3.12 slim (glibc) - - 0.03s 14M
3.13 alpine (musl) - - 0.02s 11.2M
3.13 alpine (musl) - - 0.02s 12.8M
3.13 slim (glibc) - - 0.04s 12M
3.13 slim (glibc) - - 0.04s 13M
3.9 alpine (musl) - - 0.01s 17.3M
3.9 alpine (musl) - - 0.03s 18.7M
3.9 slim (glibc) - - 0.01s 18M
3.9 slim (glibc) - - 0.01s 19M

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