Skip to main content

High-performance dead code elimination analysis tool for Python.

Project description

CytoScnPy - High-Performance Python Static Analysis

CI Coverage codecov Security Audit Docs 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

[!IMPORTANT] Behavioral Change: Starting from version 1.2.2, tests are excluded by default across both the CLI and the library API to reduce noise in production analysis. Use the --include-tests flag or set include_tests = true in your configuration to scan test files.

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

# Pre-commit integration
# See https://djinn09.github.io/CytoScnPy/pre-commit/ for setup

Common Options:

Flag Description
-s, --secrets Scan for API keys and hardcoded credentials
-d, --danger Scan for dangerous code + taint analysis
-q, --quality Scan for code quality issues (complexity, etc.)
--clones Activate duplicate code detection
--fix Preview/dry-run dead code removal
-a, --apply Apply fixes to files (use with --fix)
--json Output results in machine-readable JSON

[!TIP] > View the Full CLI Reference for detailed usage, advanced configuration, and quality gate options.

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.

⚙️ 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
max_complexity = 10   # Max cyclomatic complexity
max_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.

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.3-cp314-cp314-manylinux_2_39_x86_64.whl (2.3 MB view details)

Uploaded CPython 3.14manylinux: glibc 2.39+ x86-64

cytoscnpy-1.2.3-cp314-cp314-macosx_11_0_arm64.whl (2.1 MB view details)

Uploaded CPython 3.14macOS 11.0+ ARM64

cytoscnpy-1.2.3-cp313-cp313-win_amd64.whl (2.1 MB view details)

Uploaded CPython 3.13Windows x86-64

cytoscnpy-1.2.3-cp313-cp313-macosx_11_0_arm64.whl (2.1 MB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

cytoscnpy-1.2.3-cp312-cp312-win_amd64.whl (2.1 MB view details)

Uploaded CPython 3.12Windows x86-64

cytoscnpy-1.2.3-cp312-cp312-manylinux_2_39_x86_64.whl (2.3 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.39+ x86-64

cytoscnpy-1.2.3-cp312-cp312-macosx_11_0_arm64.whl (2.1 MB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

cytoscnpy-1.2.3-cp311-cp311-win_amd64.whl (2.1 MB view details)

Uploaded CPython 3.11Windows x86-64

cytoscnpy-1.2.3-cp311-cp311-macosx_11_0_arm64.whl (2.1 MB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

cytoscnpy-1.2.3-cp310-cp310-win_amd64.whl (2.1 MB view details)

Uploaded CPython 3.10Windows x86-64

cytoscnpy-1.2.3-cp39-cp39-win_amd64.whl (2.1 MB view details)

Uploaded CPython 3.9Windows x86-64

File details

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

File metadata

File hashes

Hashes for cytoscnpy-1.2.3-cp314-cp314-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 502db574ec06d3533dd3082814b15e418835b58094170336e6e842e3ea34bf97
MD5 2827a4b194586b30d52f79e86cd5ac73
BLAKE2b-256 bce9ff50e1095321b7078cb25ce5011966d0407ae0a8edf50342d8bc35aa3029

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cytoscnpy-1.2.3-cp314-cp314-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 d134364223806297d0b82fed4678a6d4ad72a69ba1cf9bf476c8a2192d164713
MD5 f9413ca64b834f68090d4856aba52638
BLAKE2b-256 9c0345160cf5314927cf878385db04e8d8cdaa60d3daa27ee0277638f4fb5662

See more details on using hashes here.

File details

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

File metadata

  • Download URL: cytoscnpy-1.2.3-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 2.1 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.3-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 6d243099cdc9ac412ef167cf0c2e0543ea8a5b57ae22f23948a1902c17da3072
MD5 27b41f5cb8a460f5b0ff23c422d3cebf
BLAKE2b-256 6c596c953702272a55340c8e22b10212ba95d7eb887c2d919b26440af1ccb9c3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cytoscnpy-1.2.3-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 76c44dcbd768cdee9f15009351b16e069a365c616fe362cadb8c870221ff35fd
MD5 8a2b95c2eeefeb931c5ceb08d1ffda7d
BLAKE2b-256 ca7888efca2f0b0187ad2f88e7d72b02ef032f0333b96644d3265a6c1bcd79e8

See more details on using hashes here.

File details

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

File metadata

  • Download URL: cytoscnpy-1.2.3-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 2.1 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.3-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 7a6f3869a3e788f26b7b3b6ceaf7356729d5b758e4bf2d01be18bf6d0187a158
MD5 f26a8278191683efb65ccedf3556fc34
BLAKE2b-256 99779af484d6e663620f3b01ba006623b3b3741c89ae8581d3bbb7ceb2a58d34

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cytoscnpy-1.2.3-cp312-cp312-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 ba58e339a5e8b553ab2556228a7ed3fd92d07fcd23b5eb5933f546f6f2060325
MD5 7c9d1e885822b6cef2be5725d1629a86
BLAKE2b-256 8ef0d9f0504b1452cc35430d9afe4e4a1b72351973ebc171683d2df04e171d04

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cytoscnpy-1.2.3-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 a71c655a9d3a4bfdea14645059a1e9b00784d207b9e2aa97f4944d4dcf791276
MD5 6e0b86a8f9e717b18907266fdacc4332
BLAKE2b-256 47e241cfa4b4df4983f74b2b4755e6495e13f8c51783a58f235d87154432affc

See more details on using hashes here.

File details

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

File metadata

  • Download URL: cytoscnpy-1.2.3-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 2.1 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.3-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 98ac26445d91e8e2383985af8951de50c2f5f0841f33d62af27b2a4f30d4a73c
MD5 35e793fe6a33d5c93ff8d76bc51938f5
BLAKE2b-256 f4182ffdbd78ea4b7ddc3ca821c3c2372c30d6fbbb27da6c79a101dd27ac1bff

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cytoscnpy-1.2.3-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 fdb7ea02c65908833e4d992fea138f0d28f1d99df55ace1e582fe3c80d8eb853
MD5 9ea55425a0a5679d271b34e338e7ab82
BLAKE2b-256 d97c54a3811223e809d20aeee39bb70b38eefd0d80c0a7457ddc563882afa168

See more details on using hashes here.

File details

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

File metadata

  • Download URL: cytoscnpy-1.2.3-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 2.1 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.3-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 1d54131b34890c8abe2854072640499947a3b2448758daf7d2bafb67530daa0b
MD5 22d529d3961b409cf17b541ba0b8eda1
BLAKE2b-256 3e11507bad5ac904bb76d81b05ad3fb6dc206e2c64fa065755f184aead304e72

See more details on using hashes here.

File details

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

File metadata

  • Download URL: cytoscnpy-1.2.3-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 2.1 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.3-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 0f1cc83525abc195dc263641a7ca77d790302076217f55d4a7e98c978ddf16eb
MD5 27f60c67a1fda265b819890856b0ebe7
BLAKE2b-256 19c1fc32926cec98a858eb5c94e56e145e9acde5dab1f2f98a96e2049ce72651

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