A small, readable command-line tool that scans a Python codebase with ast and reports functions that are likely unused.
Finding dead code manually is slow and error-prone. This tool gives you a fast first pass by:
- Collecting all function and method definitions.
- Collecting called identifiers from function calls.
- Reporting definitions that never appear in calls.
It is intentionally simple so teams can understand and extend it quickly.
main.py: CLI entrypoint and output formatting.analyzer.py: AST parsing, data collection, filtering, and core analysis logic.
- Walk the target directory and collect
.pyfiles. - Parse each file into an AST.
- Record:
- function definitions (
defandasync def) - called names from
ast.Call
- function definitions (
- Filter out entries that should not be treated as unused (for example descriptor methods and known dynamic-dispatch handlers).
- Print likely unused functions with file and line number.
No external dependencies are required. Uses Python standard library only.
Run on the current directory:
python main.py .Run on another project:
python main.py C:\path\to\projectFail CI if unused functions are found:
python main.py . --fail-on-unusedpath: project root directory (default: current directory)--exclude-dir <name>: exclude a directory name (can be used multiple times)--ignore-private: ignore names like_helper--include-dunder: include dunder names like__init__--fail-on-unused: exit with code1if unused functions are found
Likely unused functions:
- package/module.py:42 cleanup_data
- package/service.py:91 Worker._build_cache
When nothing is found:
No unused functions found.
This analyzer uses name-based matching. That keeps it fast, but means:
- Dynamic behavior (reflection, plugin loading, runtime attribute lookups) may hide real usage.
- Same-name functions in different scopes can produce false positives/negatives.
- A reported function is a signal for review, not guaranteed dead code.
- Run analyzer.
- Review reported functions manually.
- Remove confirmed dead code.
- Re-run with
--fail-on-unusedin CI to keep the codebase clean over time.