Skip to main content

graphlint

PyPI Python License

English | 简体中文

Dead code detection for AI-generated codebases.

AI agents generate code rapidly, leaving behind dead and redundant code that pollutes the LLM's context window and dilutes attention. Graphlint analyzes your codebase's dependency graph to identify entry points and detect dead code — components unreachable from any entry point — so agents can self-clean and keep the codebase lean.

Supported Languages

Language Status Parser Features
Python (.py) Built-in ast (stdlib) Decorators, type annotations, dynamic imports, framework-aware entry detection
Rust (.rs) Built-in (opt-in deps) tree-sitter Attribute macros, traits, pub visibility, macro_rules!
C# (.cs) Built-in (opt-in deps) tree-sitter Partial classes, properties/indexers/events, attributes, .csproj awareness, test framework entries
C / C++ (.c .h .cpp .cc .cxx .hpp .hh .hxx) Built-in (opt-in deps) tree-sitter struct/union/enum members, typedefs, macros, #include tracking, per-TU static linkage, C library entry mode, C++ classes/namespaces/templates, cross-TU member-call resolution
TypeScript / JavaScript (.ts .tsx .js .jsx .mts .cts .mjs .cjs) Built-in (opt-in deps) tree-sitter JSX/React components, Next.js pages, NestJS decorators, Jest/Vitest tests, import/export analysis

Install optional language support:

pip install graphlint[rust]        # adds tree-sitter and tree-sitter-rust
pip install graphlint[csharp]      # adds tree-sitter and tree-sitter-c-sharp
pip install graphlint[c]           # adds tree-sitter + tree-sitter-c + tree-sitter-cpp (C/C++)
pip install graphlint[typescript]  # adds tree-sitter + tree-sitter-typescript + tree-sitter-javascript

C/C++ are analysed by a single unified C/C++ analyzer that owns .c .h .cpp .cc .cxx .hpp .hh .hxx. It uses tree-sitter-c for C files, tree-sitter-cpp for C++ files, and routes a .h by the language of the TU(s) that include it — exactly as a C/C++ compiler does. One extra, graphlint[c], installs both grammars.

Features

  • Dead code detection — finds components unreachable from any entry point via graph traversal
  • Multi-language support — Python, Rust, C#, C/C++ (unified), TypeScript, and JavaScript backends via a language adapter abstraction; Python uses stdlib ast, the others use tree-sitter
  • Language-specific awareness — Python decorators, Rust attribute macros (#[tokio::main], #[test]), C# attributes ([Fact], [HttpGet]), C translation-unit scope (static internal linkage, per-TU header statics) and library entry mode, C++ classes/namespaces/templates, enum class members, and cross-TU member-call resolution, TS/JS JSX elements and ES module imports/exports, trait implementations, pub/public visibility, partial classes, and more
  • AST/CST parsing — extracts functions, methods, structs, enums, traits, impls, macros, classes, properties, indexers, events, variables, and fields; aware of type annotations, destructured variables, and generics
  • Dependency graph — builds directed edges: read, write, call, inherit, decorate
  • Entry point detection — built-in rules covering Python frameworks (FastAPI, Flask, Django, Click, Typer, Celery, pytest), Rust conventions (main, async runtimes, WASM, proc macros, FFI, tests, pub API), .NET conventions (console, xUnit, NUnit, MSTest, Web API, Minimal API, Generic Host, WinForms, WPF), C/C++ conventions (main/WinMain/wWinMain/DllMain/_tmain for C and a free main for C++, class_definition: patterns, test_file by convention, and C library mode via external-linkage symbols), and TS/JS conventions (main, module index, CLI/server listen, Next.js pages, NestJS decorators, React JSX, Jest/Vitest tests) plus custom rules
  • Configurable entry templates — add custom entry rules via ast_pattern prefixes including function_call:, function_def:, decorator:, class_definition: (C#), file_match:, file_is_program (C#), visibility:pub (Rust), visibility:public (C#), trait_impl: (Rust), macro_def: (Rust), jsx_element: (TypeScript), export: (TypeScript), and more
  • --public-as-entry flag — treat all public items (Rust pub, C# public, C external-linkage symbols) as entry points for library analysis
  • Warning detection — 11 warning types including circular references, unused imports, write-only variables, and more
  • Incremental updates — after initial full scan, only changed files are re-indexed; delta-aware reachability analysis avoids full-graph recomputation; incompatible index schema versions are auto-detected and rebuilt
  • Python API + CLI — integrate into any Tool, CI pipeline, or let agents self-analyze and self-clean

Installation

pip install graphlint

Requirements: Python >= 3.9

For Rust support (.rs files), install the optional tree-sitter dependencies:

pip install graphlint[rust]

For C# support (.cs files), install the optional tree-sitter dependencies:

pip install graphlint[csharp]

For C/C++ support (.c .h .cpp .cc .cxx .hpp .hh .hxx files), install the optional tree-sitter dependencies (one extra covers both grammars):

pip install graphlint[c]   # adds tree-sitter + tree-sitter-c + tree-sitter-cpp (C/C++)

For TypeScript/JavaScript support (.ts .tsx .js .jsx .mts .cts .mjs .cjs files), install the optional tree-sitter dependencies:

pip install graphlint[typescript]

Quick Start

Agent Integration

Graphlint provides a command to inject its usage prompt into your AI coding tools at the global level, so every project automatically has graphlint's guidance:

# Install graphlint prompt into agent tools (opencode, cursor, codex, cc)
graphlint install

# Copy the prompt to clipboard for manual paste into your agent
graphlint prompt

# Remove graphlint prompt from agent tools
graphlint uninstall

Run graphlint install and select the tools you use — the prompt (usage scenarios, essential commands, and parameters) will be added to their global configuration. For details, see Agent Integration.

If your agent tool is not listed in install, run graphlint prompt to copy the prompt to your clipboard and provide it to your agent manually. For tools you'd like native support for, feel free to submit an issue — these requests are typically handled quickly.

DeepSeek Harness Plugin

A plugin bundle for the DeepSeek Harness plugin ecosystem ships in this repository under integrations/dsh:

  • Toolsgraphlint_query (dependency-graph queries with structured results), graphlint_build (index build as a background job, polled with job_output), graphlint_config (show/get/set for .graphlint/config.json).
  • Skill — a graphlint skill teaches the agent when and how to use the tools.
  • Safety — tools default to the session working directory and hard-refuse any root outside it, so an accidental high-level scan cannot block a turn.

Install the bundle from npm:

dsh plugin --profile web add dsh-graphlint

Then restart dsh web. To link a local checkout instead (development):

# 1. Clone the repository and build the bundle (requires Node.js >= 20)
git clone https://github.com/AngelosZou/graphlint.git
cd graphlint/integrations/dsh
npm install
npm run build

# 2. Link the bundle into a profile (run from the repository root)
cd ..
dsh plugin --profile web add link:./integrations/dsh

# 3. Restart dsh web

CLI

# Find dead code in current directory
graphlint query --warn-types "dead_code"

# Full analysis with JSON output
graphlint query --json

# View a specific graph detail
graphlint query -g 1 --detail full

# Exit non-zero when dead code or circular refs found (for CI)
graphlint query --json --fail-on dead_code,circular_ref

# Treat all public items as entry points (library analysis mode)
graphlint query --public-as-entry

# Rebuild index
graphlint build --force

# Configure
graphlint config show
graphlint config set --key lang --value en

Exit Codes

Code Meaning
0 Success — no warnings matched --fail-on
1 Error — invalid parameters, exception, or config error
2 Warnings found — --fail-on matched specified warning types

Use --fail-on with a comma-separated list of warning types to make graphlint query return exit code 2 when matching warnings are found. This enables CI pipeline integration without blocking on non-critical warnings.

Graphlint is static-analysis based and cannot recognize certain Python dynamic references (e.g., getattr, importlib), which may produce unexpected exit codes. Only use --fail-on for CI blocking behavior when you're confident in your configuration. Agents are better suited for logic that requires contextual judgment. See Limitations for details.

Python API

from graphlint.api import query

# Find dead code components
result = query(warn_types="dead_code", json_output=True)

# Full dependency graph analysis
result = query(include_tests=True, json_output=True)

Warning Types

Warning Description
unused_import Imported module or name is never used
dynamic_import Dynamic import via importlib or __import__
circular_ref Circular dependency between functions/classes
syntax_error File contains a syntax error
write_only Variable is written but never read
deprecated_usage Usage of a deprecated function/class
dead_code Component unreachable from any entry point
type_mismatch Suspicious type annotations
unresolved_ref Reference to an undefined name
unused_variable Variable is defined but never used
file_too_large File exceeds the configured size limit

Development

# Clone the repository
git clone https://github.com/AngelosZou/graphlint.git
cd graphlint

# Create a virtual environment
python -m venv env
env/Scripts/activate  # Windows
source env/bin/activate  # Unix

# Install dev dependencies
pip install -e ".[dev]"

# Run tests
pytest

# Run with coverage
pytest --cov=graphlint

# Run type checking
mypy graphlint/

# Run linting
ruff check graphlint/ tests/

Configuration

Graphlint stores its configuration in .graphlint/config.json within the analyzed directory. Use graphlint config commands to manage settings, or edit the file directly.

See graphlint config show for the full default configuration.

Documentation

Full documentation is available in the docs/ directory:

Limitations

  • Static analysis only — graphlint performs static analysis and cannot detect runtime linkage such as getattr, importlib, or dynamic dispatch patterns, which may result in false positives. This primarily affects Python; Rust's static dispatch model produces fewer false positives. Mitigation: add custom entry rules matching your codebase's conventions. For example, graphlint's own codebase uses function_def:_detect_* and function_def:visit_* patterns to prevent functions discovered via getattr from being flagged as dead.
  • Python dynamic imports — due to Python's dynamic import mechanisms (importlib, getattr, metaclasses, etc.), the default entry templates may produce false positives in codebases that rely heavily on runtime dispatch. Users should tune the entry_rules configuration to match their project's conventions.
  • Rust macro expansion — tree-sitter parses unexpanded source; procedural macros and macro_rules! bodies appear as opaque token trees. Some macro-generated call paths may be missed. #[derive] attributes are partially recognized via implicit inherit edges.
  • C# partial classes & reflection — tree-sitter parses each .cs file independently; partial class fragments are merged into a single logical node via part_of edges, but members called only through reflection (Activator.CreateInstance, DI container registration) may be missed, similar to Python's dynamic import limitations.
  • C/C++ static analysis limits — member calls resolve type-aware across translation units (via a project-wide class→method index), but there is no virtual/overload/dynamic dispatch: a call through a Base* pointer resolves statically to the base's method, so a derived override reached only that way may be reported dead. The preprocessor is treated as pure AST (#if is never evaluated, macros are not expanded); function-pointer indirect call targets are not traced; a static symbol in a header is per-TU. A pure-virtual (= 0) interface method is exempt from dead-code (dispatched by the vtable). C function parameters are not modelled as nodes, so a parameter named like a caller's local can resolve by flat name.
  • --public-as-entry scope — this flag applies to languages with public visibility declarations (Rust pub, C# public). It has no effect on Python files. Toggling this flag triggers a full re-index. For long-term library analysis, prefer enabling the rust_pub_api entry rule via graphlint config to persist the setting.
  • Large codebase build time — on a large codebase with 700+ .py files, 1,000+ classes, and 14,000+ functions, a full rebuild takes approximately 200 seconds (actual performance depends on hardware). Small projects (~60 files) complete in ~1 second. This cost is one-time, after the initial full scan, subsequent queries use incremental updates.

License

MIT — see LICENSE for details.

Links

Download files

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

Source Distribution

graphlint-0.7.0.tar.gz (267.3 kB view details)

Uploaded Source

Built Distribution

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

graphlint-0.7.0-py3-none-any.whl (225.8 kB view details)

Uploaded Python 3

File details

Details for the file graphlint-0.7.0.tar.gz.

File metadata

  • Download URL: graphlint-0.7.0.tar.gz
  • Upload date:
  • Size: 267.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.14

File hashes

Hashes for graphlint-0.7.0.tar.gz
Algorithm Hash digest
SHA256 4fba7a5732ca22e42559a00057d8a1f2dc8e46f76c27c90d5ce668926b33edc3
MD5 c01e5491a1e412201a26938b51fc4141
BLAKE2b-256 cb544f341c396b7fa87c10353b4d7dc8fb8e73db00480d8a2a5ed14512825f33

See more details on using hashes here.

File details

Details for the file graphlint-0.7.0-py3-none-any.whl.

File metadata

  • Download URL: graphlint-0.7.0-py3-none-any.whl
  • Upload date:
  • Size: 225.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.14

File hashes

Hashes for graphlint-0.7.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0f0c27308868baca5e5a6096fd2f41cd8631026e0caa642c26feb6eeb1cd3794
MD5 f93653dff3c783c7ad6397b6547d9167
BLAKE2b-256 c84d859f8ac9587bd5b4d97046d9502acfd32bb20933da248741c16b30d4cd5d

See more details on using hashes here.

Release history Release notifications | RSS feed

0.7.1

2 files

This release

0.7.0 This release

2 files

0.6.0

2 files

0.5.0

2 files

0.4.1

2 files

0.4.0

2 files

0.3.8

2 files

0.3.7

2 files

0.3.6

2 files

0.3.5

2 files

0.3.4

2 files

0.3.3

2 files

0.3.2

2 files

0.3.1

2 files

0.3.0

2 files

0.2.0

2 files

0.1.12

2 files

0.1.11

2 files

0.1.10

2 files

0.1.9

2 files

0.1.8

2 files

0.1.7

2 files

0.1.6

2 files

0.1.5

2 files

0.1.4

2 files

0.1.3

2 files

0.1.2

2 files

0.1.1

2 files

0.1.0

2 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