How I find what breaks before I change a Python function
I've been bitten by the same bug more times than I'd like to admit. I change a "small" helper in a Python codebase, the tests I thought to run pass, and two days later something three modules away breaks because it called that helper in a way I never knew about. grep helps a little, but it tells you where a name appears, not what depends on it. A name like charge or total shows up in comments,…
Changing a small helper function in a Python codebase can have unintended consequences, such as breaking unrelated modules that call the function in unexpected ways. Traditional tools like grep are limited, as they only show where a name appears, not what depends on it. To address this issue, the author created a small command-line interface (CLI) called blastradius, which answers questions about the callers and dependents of a specific function.
Blastradius is MIT licensed and has no dependencies. It can be installed using pip install git+https://github.com/enhanciar/blastradius. It supports Python 3.10 or later and requires no API key or network calls. The tool runs entirely locally on the source code.
When using blastradius, you provide the path to the file and method you want to analyze, along with optional flags like the depth of the analysis and output format. For example, blastradius shop/money.py::apply_tax runs an analysis on the apply_tax function in the shop/money.py file. The output shows the number of files affected, the total number of definitions, and the edges between them (the relationships between the functions and modules).
On a real-world example with the popular requests library, blastradius found 5 direct callers, 14 transitive dependents, and 15 test functions that reach the get_netrc_auth function. This targeted information allows developers to focus on the most relevant tests and areas of the codebase when making changes, rather than running the entire test suite and hoping for the best.
The tool works by parsing the source code with Python's built-in ast module, recording definitions, calls, and imports. It then builds a graph of relationships between functions and modules, allowing for a breadth-first search to determine which tests are affected by a change. While blastradius is currently limited to static analysis of Python code, its design focuses on accuracy by dropping ambiguous edges rather than guessing.
Blastradius is currently under development as part of a larger product called Enhanciar, which aims to answer questions by linking to relevant sources. The author welcomes feedback on missed edges or any bugs encountered, as they consider this tool useful beyond just refactoring, and they encourage users to share their experiences with the tool.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.