Python Package

Reducing Complexity

Techniques to measure and reduce cyclomatic and cognitive complexity in Python code.

Early Access: Until 2026-12-31

mgpy treats complexity as a measurable refactoring target: hotspots should be visible, comparable and trackable across baselines.

Key points

  • CLI: On Windows the examples use the recommended py -3.12 -m <module> ... form (for example py -3.12 -m manifestguard ...). On Linux/macOS this usually maps to python3.12 -m ....
  • The built-in AST analyzer is the default engine and keeps trend data stable over time.
  • Early returns, smaller helper functions and clearer responsibilities reduce violations faster than formatting-only cleanups.
  • Always save a baseline checkpoint before larger structural changes.

Recommended mgpy workflow

  1. Capture the current hotspots with a fixed threshold first.
  2. Break the worst functions into smaller, testable steps instead of moving the whole module tree at once.
  3. After each refactor, compare the result against the stored baseline.

Quick start

py -3.12 -m manifestguard complexity --threshold 12
py -3.12 -m manifestguard baseline --save-label pre-refactor
py -3.12 -m manifestguard baseline --compare pre-refactor

Requirements

Columns
Installation interpreter
Python 3.12 + pip
Recommended default path for installation and CLI calls.
Project target versions
Python 3.8 to 3.12
These are the project/runtime targets mgpy can analyze.
mgpy runtime
Validated on Python 3.10 to 3.13
The tool runtime itself is covered for this range.
CLI invocation
Windows: py -3.12 -m manifestguard
Linux/macOS usually maps to python3.12 -m manifestguard.
Runtime packages
tomlkit, click, pydantic, packaging, watchdog, PyNaCl, rfc8785
tomli is only added for Python below 3.11.
Offline / wheel install
Optional via pip --no-index or wheel
Useful for air-gapped or approved bundle distribution paths.