Skip to main content

High-performance dead code elimination analysis tool for Python.

Project description

CytoScnPy - High-Performance Python Static Analysis

CI License Version

A fast static analysis tool for Python codebases, powered by Rust with hybrid Python integration. Detects dead code, security vulnerabilities (including taint analysis), and code quality issues with extreme speed. Code quality metrics are also provided.

Why CytoScnPy?

  • Blazing Fast: Faster in dead code detection.
  • Memory Efficient: Uses less memory.
  • Comprehensive: Dead code, secrets, security, taint analysis, quality metrics
  • Framework Aware: Flask, Django, FastAPI, Pydantic, Azure Functions
  • Benchmarked: Continuous benchmarking with 135-item ground truth suite

Installation

Linux / macOS:

# Install
curl -fsSL https://raw.githubusercontent.com/djinn09/CytoScnPy/main/install.sh | bash

Windows (PowerShell):

# Install
irm https://raw.githubusercontent.com/djinn09/CytoScnPy/main/install.ps1 | iex

Via Pip:

pip install cytoscnpy

From Source:

git clone https://github.com/djinn09/CytoScnPy.git
cd CytoScnPy
pip install maturin
maturin develop -m cytoscnpy/Cargo.toml

MCP Server (for AI Assistants)

CytoScnPy includes an MCP server for AI assistant integration:

# Start MCP server (after pip install)
cytoscnpy mcp-server

For Claude Desktop, Cursor, or GitHub Copilot configuration, see the MCP Server Documentation.

Features

  • Dead Code Detection: Unused functions, classes, imports, and variables with cross-module tracking.
    • Cascading Detection: Methods inside unused classes are automatically flagged as unused.
    • Auto-Fix: Remove dead code automatically with --fix (preview by default, use --apply to execute).
  • Clone Detection: Find duplicate code with --clones.
  • Security Analysis: Taint analysis (SQLi, XSS), secret scanning (API keys, suspicious variables), and dangerous code patterns (eval, exec).
  • Code Quality Metrics: Cyclomatic complexity, Halstead metrics, Maintainability Index, and raw metrics (LOC, SLOC).
  • Framework Support: Native understanding of Flask, Django, FastAPI, Pydantic, and Azure Functions v2 patterns.
  • Smart Heuristics: Handles dataclasses, __all__ exports, visitor patterns, and dynamic attributes intelligently.
  • Cross-File Detection: Tracks symbol usage across the entire codebase, including nested packages and complex import chains, to ensure code used in other modules is never incorrectly flagged.

Usage

Command Line

cytoscnpy [PATHS]... [OPTIONS]

Examples:

# Dead code analysis
cytoscnpy .                                     # Analyze current directory
cytoscnpy /path/to/project --json               # JSON output for CI/CD

# Security checks (short flags: -s, -d, -q)
cytoscnpy . --secrets --danger --quality
cytoscnpy . -s -d -q                        # Same with short flags

# Confidence threshold (0-100)
cytoscnpy . --confidence 80

# Path filtering
cytoscnpy . --exclude-folder venv --exclude-folder build
cytoscnpy . --include-folder specific_venv      # Override defaults
cytoscnpy . --include-tests

# Jupyter notebooks
cytoscnpy . --include-ipynb --ipynb-cells

# Clone detection (find duplicate code)
cytoscnpy . --clones --clone-similarity 0.8

# Auto-fix dead code (preview first, then apply)
cytoscnpy . --fix                    # Preview changes (dry-run by default)
cytoscnpy . --fix --apply            # Apply changes
cytoscnpy . --fix -a                 # Apply changes (short flag)

# Generate HTML report (quality auto-enabled; add --secrets --danger for security)
cytoscnpy . --html --secrets --danger

Options:

Flag Description
-c, --confidence <N> Set confidence threshold (0-100)
-s, --secrets Scan for API keys, tokens, credentials
-d, --danger Scan for dangerous code + taint analysis
-q, --quality Scan for code quality issues
-n, --no-dead Skip dead code detection (security/quality only)
--html Generate HTML report (auto-enables quality)
--json Output results as JSON
-v, --verbose Enable verbose output for debugging
--quiet Quiet mode: summary only, no tables
--include-tests Include test files in analysis
--exclude-folder <DIR> Exclude specific folders
--include-folder <DIR> Force include folders
--include-ipynb Include Jupyter notebooks
--ipynb-cells Report findings per notebook cell
--clones Detect duplicate code
--clone-similarity <N> Clone similarity threshold (0.0-1.0)
--fix Preview dead code removal (dry-run by default)
-a, --apply Apply --fix changes to files

CI/CD Gate Options:

Flag Description
--fail-threshold <N> Exit code 1 if unused code % > N
--max-complexity <N> Exit code 1 if any function complexity > N
--min-mi <N> Exit code 1 if maintainability index < N
--fail-on-quality Exit code 1 if any quality issues found
--max-nesting <N> Exit code 1 if any block nesting > N
--max-args <N> Exit code 1 if any function has > N args
--max-lines <N> Exit code 1 if any function has > N lines

Full CLI Reference: See docs/CLI.md for complete command documentation.

Metric Subcommands

cytoscnpy raw .                    # Raw Metrics (LOC, SLOC, Comments)
cytoscnpy cc .                     # Cyclomatic Complexity
cytoscnpy hal .                    # Halstead Metrics
cytoscnpy mi .                     # Maintainability Index
cytoscnpy stats . --all            # Full project report (secrets, danger, quality)
cytoscnpy stats . --all -o report.md  # Save report to file
cytoscnpy files .                  # Per-file metrics table

Tip: Add --json for machine-readable output, --exclude-folder <DIR> to skip directories globally, or --ignore <PATTERN> for subcommand-specific glob filtering.

Feature Flags

The crate supports experimental features that can be enabled at compile time:

Feature Description
cfg Enables Control Flow Graph (CFG) construction and behavioral validation for clone detection

To build with a feature enabled:

cargo build --features cfg

⚙️ Configuration

Create .cytoscnpy.toml (uses [cytoscnpy]) or add to pyproject.toml (uses [tool.cytoscnpy]):

.cytoscnpy.toml example:

[cytoscnpy]
# General Settings
confidence = 60  # Minimum confidence threshold (0-100)
exclude_folders = ["venv", ".tox", "build", "node_modules", ".git"]
include_folders = ["src", "tests"]  # Optional: whitelist folders
include_tests = false  # Note: include_ipynb and ipynb_cells are CLI-only (use flags)

# Analysis Features
secrets = true
danger = true
quality = true

# Fail Threshold (exit code 1 if exceeded)
fail_threshold = 10.0  # Fail if >10% of code is unused
# fail_threshold = 0.0  # Zero tolerance: fail on any unused code

# Code Quality Thresholds
max_lines = 100       # Max lines per function
max_args = 5          # Max arguments per function
complexity = 10       # Max cyclomatic complexity
nesting = 4           # Max indentation depth
min_mi = 65.0         # Minimum Maintainability Index
ignore = ["R001"]     # Ignore specific rule IDs

# Advanced Secret Scanning
[cytoscnpy.secrets_config]
entropy_enabled = true
entropy_threshold = 4.5  # Higher = more random (API keys usually >4.0)
min_length = 16          # Min length to check for entropy
scan_comments = true     # Scan comments for secrets
skip_docstrings = false  # Skip docstrings in entropy scanning
min_score = 50           # Minimum confidence score (0-100)
suspicious_names = ["db_config", "oauth_token"] # Add custom suspicious variable names

# Custom Secret Patterns
[[cytoscnpy.secrets_config.patterns]]
name = "Slack Token"
regex = "xox[baprs]-([0-9a-zA-Z]{10,48})"
severity = "HIGH"

Note: Notebook options (include_ipynb, ipynb_cells) are currently CLI-only but will be added to the configuration file in a future release.

CI/CD Quality Gates

Configure quality gates for CI/CD pipelines. Set thresholds and the CLI exits with code 1 if exceeded.

CLI Flags:

# Unused code percentage gate
cytoscnpy . --fail-threshold 5  # Fail if >5% unused

# Complexity gate
cytoscnpy . --max-complexity 10  # Fail if any function >10

# Maintainability Index gate
cytoscnpy . --min-mi 40  # Fail if MI <40

# Quiet mode for clean CI output
cytoscnpy . --fail-threshold 5 --quiet

Priority: CLI flag > config file > environment variable > default

Environment Variable: CYTOSCNPY_FAIL_THRESHOLD=5.0

Performance

Accuracy (Benchmark Suite: 135 items)

Detection Type Precision Recall F1 Score
Classes 0.73 0.79 0.76
Functions 0.71 0.74 0.73
Methods 0.86 0.93 0.89
Imports 0.67 0.40 0.50
Variables 0.30 0.15 0.20
Overall 0.71 0.64 0.68

See benchmark/README.md for detailed comparison against Vulture, Flake8, Pylint, Ruff, and others.

Architecture

See cytoscnpy/README.md for detailed architecture and technology stack information.

Testing

See CONTRIBUTING.md for testing instructions.

Contributing

See CONTRIBUTING.md for development setup and guidelines.

License

Apache-2.0 License - see License file for details.

Links

References

CytoScnPy's design and implementation are inspired by:

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

cytoscnpy-1.2.1-cp314-cp314-manylinux_2_39_x86_64.whl (2.2 MB view details)

Uploaded CPython 3.14manylinux: glibc 2.39+ x86-64

cytoscnpy-1.2.1-cp314-cp314-macosx_11_0_arm64.whl (2.0 MB view details)

Uploaded CPython 3.14macOS 11.0+ ARM64

cytoscnpy-1.2.1-cp313-cp313-win_amd64.whl (2.0 MB view details)

Uploaded CPython 3.13Windows x86-64

cytoscnpy-1.2.1-cp313-cp313-macosx_11_0_arm64.whl (2.0 MB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

cytoscnpy-1.2.1-cp312-cp312-win_amd64.whl (2.0 MB view details)

Uploaded CPython 3.12Windows x86-64

cytoscnpy-1.2.1-cp312-cp312-manylinux_2_39_x86_64.whl (2.2 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.39+ x86-64

cytoscnpy-1.2.1-cp312-cp312-macosx_11_0_arm64.whl (2.0 MB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

cytoscnpy-1.2.1-cp311-cp311-win_amd64.whl (2.0 MB view details)

Uploaded CPython 3.11Windows x86-64

cytoscnpy-1.2.1-cp311-cp311-macosx_11_0_arm64.whl (2.0 MB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

cytoscnpy-1.2.1-cp310-cp310-win_amd64.whl (2.0 MB view details)

Uploaded CPython 3.10Windows x86-64

cytoscnpy-1.2.1-cp39-cp39-win_amd64.whl (2.0 MB view details)

Uploaded CPython 3.9Windows x86-64

File details

Details for the file cytoscnpy-1.2.1-cp314-cp314-manylinux_2_39_x86_64.whl.

File metadata

File hashes

Hashes for cytoscnpy-1.2.1-cp314-cp314-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 d0611eefd4a3db33d4711e8594448bc820f4a16df1d9705ad46a5bba3164ec85
MD5 924aa5a580a8f356f7a60bb53c2f7120
BLAKE2b-256 a8cf8cd19240448f651705cde010611a8ebdf9c6c2f9443767e9aeda6f091001

See more details on using hashes here.

File details

Details for the file cytoscnpy-1.2.1-cp314-cp314-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for cytoscnpy-1.2.1-cp314-cp314-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 9527cee8a851bcca1bad43a872a4b5415e5937db4cf25fd10a8ad7f863313c11
MD5 127f001dbab94b2312fcd7d6a47ac531
BLAKE2b-256 ce7da82c00e15635f4588f6e6fd8acd8d2e3afe58e1a1637d9500222b864a44c

See more details on using hashes here.

File details

Details for the file cytoscnpy-1.2.1-cp313-cp313-win_amd64.whl.

File metadata

  • Download URL: cytoscnpy-1.2.1-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 2.0 MB
  • Tags: CPython 3.13, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for cytoscnpy-1.2.1-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 9b85a73be5e349c28e95bb49b819dbe8683468cdb4384bf4ba22968b0f0ecb1b
MD5 ddc722d667a16314f2ccf6a5953d5245
BLAKE2b-256 f94ae32bd2ec684f6a0cd006da5c71495f36adcf6e8e283ca00a7126d0406fee

See more details on using hashes here.

File details

Details for the file cytoscnpy-1.2.1-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for cytoscnpy-1.2.1-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 d7b6df823759f7430020902ab8846ec4062414f17505e3dc05f5f2a6ab71db71
MD5 023ee33870d2ff363c826b050c90a0f2
BLAKE2b-256 f42188abe235af6a00ccea335d265337c48c137beb95896bd549cbe24e8435cd

See more details on using hashes here.

File details

Details for the file cytoscnpy-1.2.1-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: cytoscnpy-1.2.1-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 2.0 MB
  • Tags: CPython 3.12, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for cytoscnpy-1.2.1-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 78355e1a7da5049ed31fb6173a432f4ad832755a348defc07cbd46d5e50d4934
MD5 3b06e9306370e81c65dc6413d7cc5dd5
BLAKE2b-256 052ace3b90273ec12d36aa5f469923d0aefdc8685a72267da25d595ab3ce8216

See more details on using hashes here.

File details

Details for the file cytoscnpy-1.2.1-cp312-cp312-manylinux_2_39_x86_64.whl.

File metadata

File hashes

Hashes for cytoscnpy-1.2.1-cp312-cp312-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 c07e029fee6e803b2df85b33a93f5b12bf3ddfd2d2b9eb16c8324e880d92329e
MD5 6159fb716d20cbdc9f31b6f1c0256ac2
BLAKE2b-256 0f3a55034e2c0268ce6eb31b7b036a3132645305d63789eb6f89d134cfa614d3

See more details on using hashes here.

File details

Details for the file cytoscnpy-1.2.1-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for cytoscnpy-1.2.1-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 3cd249287d692793e64c9a18cf9deeef267cb8c3cd366257b4a5ebe06087b4ec
MD5 47b944f8d58a9535e529d36ab1c474ec
BLAKE2b-256 ae21aea155aa6e86f8efa9ebce0c65a74dffc3ff02bf0f135444b59e198df3f3

See more details on using hashes here.

File details

Details for the file cytoscnpy-1.2.1-cp311-cp311-win_amd64.whl.

File metadata

  • Download URL: cytoscnpy-1.2.1-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 2.0 MB
  • Tags: CPython 3.11, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for cytoscnpy-1.2.1-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 354fc56fdee9aab5d059284f642d72a4bce9046a4c096241ed5b9ce718623930
MD5 6dc5f16b5a931d6a1248fdb16f064007
BLAKE2b-256 df283ebc8ee476b234b7e0dc9070eb2130de11d5d72b7fb81bd670275c612a85

See more details on using hashes here.

File details

Details for the file cytoscnpy-1.2.1-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for cytoscnpy-1.2.1-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 8cafa3cb9f8a06aa5b17851e61c5de9acfdd59963e7e4fde57ed4c0547549566
MD5 ab53d12807c0ecba1a826d59f8394179
BLAKE2b-256 eb6cc00c663fc301576241fca5784eeaf1bc2d89ef40d8bd33ce2316b67c1bea

See more details on using hashes here.

File details

Details for the file cytoscnpy-1.2.1-cp310-cp310-win_amd64.whl.

File metadata

  • Download URL: cytoscnpy-1.2.1-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 2.0 MB
  • Tags: CPython 3.10, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for cytoscnpy-1.2.1-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 46e5880920f503d1c7761dda4d5e454eadbcaaff4f7163f66b4e235a57566240
MD5 a46328da0c879813972d34c3a2197840
BLAKE2b-256 87ba01fd06a91e1bccd09614edd8a4e9f9d4eb7a5a10edccac4325808f4e18b7

See more details on using hashes here.

File details

Details for the file cytoscnpy-1.2.1-cp39-cp39-win_amd64.whl.

File metadata

  • Download URL: cytoscnpy-1.2.1-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 2.0 MB
  • Tags: CPython 3.9, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for cytoscnpy-1.2.1-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 89fba19717f0fb36bf8d7fc6c935f500567cf182d02562cd9a8dcda1dd407595
MD5 3a4b0ccc75d6a9d26e9fdb34d7c6a27b
BLAKE2b-256 9286f4599fa1ed96b705d26e8655d2abd0e7d55b55448bfb2bdf28e8a83e1e4b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page