Registry / testing / deadcode

deadcode

JSON →
library2.4.1pypypi✓ verified 84d ago

The `deadcode` library is a static analysis tool for Python that identifies unused functions, classes, and variables within a codebase. It helps developers clean up projects by pinpointing code that is no longer reachable or imported. As of version 2.4.1, it focuses on providing a robust CLI experience with programmatic API access. The project maintains an active development cycle, with major releases incorporating significant refactors and minor releases for bug fixes and feature enhancements.

pip install deadcode
INSTALL
IMPORT
SIG · DEADCODE
D
deadcode
testingpythonv2.4.1
Install
1.8s avg
Import
Disk
17MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v2.4.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.910 runs
installs and imports cleanly · install 0.0s · import 0.000s · 18.2MB
glibc
py 3.103.910 runs
installs and imports cleanly · install 1.8s · import 0.000s · 19MB
17MB installed
● package 17MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

importlib
from deadcode import importlib
from deadcode.finder import find_dead_code

This quickstart demonstrates how to use the `deadcode` command-line interface (CLI) to scan a temporary Python project for unused code. It creates two dummy Python files, one with a dead function, then executes `deadcode` and prints its output and the generated JSON report. This illustrates the typical workflow of using `deadcode` on a project.

import subprocess import json import tempfile from pathlib import Path import shutil # Create a temporary directory to simulate a project temp_dir = Path(tempfile.mkdtemp()) # Create dummy Python files within the temporary project # my_module.py contains an unused function (temp_dir / "my_module.py").write_text(""" def used_func(): return "hello" def unused_func(): return "world" """) # main.py imports and uses 'used_func', leaving 'unused_func' dead (temp_dir / "main.py").write_text(""" from my_module import used_func print(used_func()) """) # Define the output report file path output_file = temp_dir / "deadcode_report.json" try: print(f"Scanning directory: {temp_dir}") # Run the deadcode CLI tool result = subprocess.run( ["deadcode", "--path", str(temp_dir), "--output", str(output_file)], capture_output=True, text=True, check=True # Will raise CalledProcessError if command fails ) print("\n--- CLI STDOUT ---") print(result.stdout) if result.stderr: print("\n--- CLI STDERR ---") print(result.stderr) # Check if the report file was generated and print its content if output_file.exists(): with open(output_file, 'r') as f: report = json.load(f) print("\n--- DEAD CODE REPORT ---") print(json.dumps(report, indent=2)) else: print("\nNo dead code report file generated.") except subprocess.CalledProcessError as e: print(f"\nERROR: deadcode command failed with exit code {e.returncode}") print(f"Stdout: {e.stdout}") print(f"Stderr: {e.stderr}") except FileNotFoundError: print("\nERROR: 'deadcode' command not found. Please ensure the 'deadcode' package is installed and its executable is in your system's PATH.") finally: # Clean up the temporary directory and its contents if temp_dir.exists(): print(f"\nCleaning up temporary directory: {temp_dir}") shutil.rmtree(temp_dir)
deadcode --version
Debug
Known issues
breakingVersion 2.0.0 introduced significant breaking changes, including a major refactor of the internal parsing logic, CLI arguments, and configuration. If upgrading from pre-2.0.0 versions, direct code and config might break.
fix
Review the official changelog and documentation for 2.x versions. The configuration file was changed from `pyproject.toml` (for `[tool.deadcode]`) to a dedicated `deadcode.toml`.
affects: <2.0.0
breakingThe `exclude_dirs` argument was removed from both the CLI and programmatic `find_dead_code` function in version 2.0.0. Its functionality has been merged into `exclude_patterns`.
fix
Replace usage of `--exclude-dirs` with `--exclude-patterns` in CLI calls, or update programmatic calls to use `config.exclude_patterns` for directory exclusion (e.g., `['**/tests/', '**/docs/']`).
affects: <2.0.0
gotchaStatic analysis tools like `deadcode` may produce false positives or negatives, especially with highly dynamic code, complex import mechanisms (e.g., `__import__`, `importlib`), or when code is generated at runtime.
fix
Carefully review the reports. Use the `--exclude-patterns` option in `deadcode.toml` or via CLI arguments to fine-tune analysis for specific project structures, dynamically generated files, or known false positives.
affects: All
Upgrade
Version history
2.4.1latest on PyPI · released Aug 9, 2024
Audit
Dependencies
logururequiredLogging functionality
typer[all]requiredBuilding the command-line interface
richrequiredRich terminal output
pydanticrequiredData validation and settings management
pyyamlrequiredYAML configuration parsing
tomlrequiredTOML configuration parsing
radonrequiredCode complexity and analysis
dulwichrequiredGit repository interaction (e.g., ignoring untracked files)
Agent activity
8 hits · last 30 days
node
6
Resources
deadcode — pip install deadcode · libregistry