Skip to main content

Branch Logo

Branch

PyPI Version Tests Coverage License

A Python CLI tool for static analysis of control flow complexity. Quantifies test surface area through N-Path complexity calculation, models short-circuit evaluation of boolean expressions, and provides mathematically-proven refactoring recommendations.

Requirements

  • Python 3.14+
  • typer
  • pytest

Installation

# Using uv (recommended)
uv add branch

# Or using pip
pip install branch

# From source
git clone https://github.com/nesalia-inc/branch.git
cd branch
uv build
uv add -e .

Quick Start

# Analyze a Python file
branch analyze your_code.py

# With detailed explanation
branch analyze your_code.py --explain

# JSON output for CI
branch analyze your_code.py --json

# Filter by minimum improvement threshold
branch analyze your_code.py --explain --min-gain 50

Example Output

$ branch analyze examples/patterns/c1_duplicate_outcomes.py --explain

[C1] Duplicate Outcomes [MECHANICAL]
  Both branches return identical value

  Impact: N-Path 2 -> 1 (50.0% reduction)
         Depth 1 -> 0

Impact Summary:
  Functions analyzed: 4
  Critical issues: 0
  Warning issues: 0
  Pattern suggestions: 3

Features

  • N-Path Complexity - Mathematical path counting through boolean short-circuit evaluation
  • Virtual Refactoring Engine - Proof-based transformation suggestions
  • Pattern Detection - Automated detection of refactoring opportunities:
    • C1: Duplicate Outcomes (if a: return X else: return X → return X)
    • A2: Arrow Code (deep nesting → early returns)
    • D1: If/Elif Chain → Dispatch Map
  • Two-Phase Analysis - Fast AST scan with selective deep CFG analysis
  • Structured CFG - Preserves elif nesting, handles short-circuit correctly
  • Health Score - Combined metric: H = (NP * W1) + (SD * W2)

Architecture

Source Code → AST → CFG → IR → Analysis → Transformations → Output

Two-Phase Analysis

  1. Phase 1: Fast AST Scan (O(n)) - Estimates complexity, triggers Phase 2 only if thresholds exceeded
  2. Phase 2: Full CFG Analysis - Structured CFG, exact N-Path calculation, virtual refactoring

Key Metrics

Metric Description
N-Path (NP) Total execution paths (maps to test cases needed)
Structural Depth (SD) Nesting depth (maps to cognitive complexity)

Documentation

See the docs/ folder for detailed technical specifications:

CLI Commands

branch analyze <file.py>        # Analyze a Python file
branch analyze <file.py> --explain  # Detailed output with transformation proofs
branch analyze <file.py> --json    # JSON output for CI integration
branch analyze <file.py> --min-gain <N>  # Filter by min improvement %
branch ping                     # Check installation

Pattern Confidence Tags

Tag Meaning
MECHANICAL Exact match, always safe to apply
PROBABILISTIC Pattern match may not apply in all cases
HEURISTIC Suggestion based on heuristics

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Author

  • Nesalia Inc.

Support

For bug reports or feature requests, please open an issue on GitHub.

License

MIT License - see the LICENSE file for details.

Metadata

Release files for branch-py 0.1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for branch-py 0.1.3
File Size Uploaded
branch_py-0.1.3.tar.gz 190.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for branch-py 0.1.3
File Interpreter ABI Platform
branch_py-0.1.3-py3-none-any.whl Python 3 none any Details

Total release size: 228.2 kB

Release files / branch_py-0.1.3.tar.gz

Download URL branch_py-0.1.3.tar.gz
Size 190.1 kB
Tags Source
SHA-256 checksum
How to use checksums
083e011cdddef985d8c24d89b970534b9f64f2403f9f846614512047d57c5016
BLAKE2b-256 checksum
How to use checksums
9fc8e1a3d8dfc04aa88eb6307de705ea40880e98712d7fbcf1fe01271dc094ad
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.9.26 {"installer":{"name":"uv","version":"0.9.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / branch_py-0.1.3-py3-none-any.whl

Download URL branch_py-0.1.3-py3-none-any.whl
Size 38.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7859faf77109ce66adb3e1777f0b3465f68c754bca4a6afb03745be905109e9b
BLAKE2b-256 checksum
How to use checksums
e91ee62fd580eac9437f63ec6916326f5f27e6e7b508e23cb5bc3e013364641b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.9.26 {"installer":{"name":"uv","version":"0.9.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release history Release notifications | RSS feed

This release

0.1.3 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page