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