A small script that warns about functions that are not called from within the code (fork from https://gist.github.com/Herst/287aa233c0acba6ed2bc2d093dec786d ported to Python 2)
✓ Published0🌍 Public
HHerst
Last edited Oct 22, 2016
Created on Oct 22, 2016
This example demonstrates a Python script that identifies functions defined in source code but never called elsewhere in the same set of files. It shows how static analysis can flag potentially dead code for cleanup. The implementation uses Python's `ast` module and a custom `NodeVisitor` subclass to traverse abstract syntax trees, collecting both function definitions and references across multiple modules. A separate, simpler version relies on regular expressions via `re.findall` to match definitions and calls. Both approaches compare the set of defined names against referenced names, excluding patterns like `visit_*` or names starting with `test_`, and print the file and line number of each unused function.
AI-generated description