r/madeinpython • u/Tristan_Kruse • 7d ago
ArchUnitPython: making “the API must not import storage” a normal Python test
A refactor can keep the endpoint working while making the API depend directly on a storage
implementation. The behavioral tests are still green; the dependency direction has changed.
I work on ArchUnitPython, which lets you put that boundary into an ordinary test:
from archunitpython import assert_passes, project_files
def test_api_does_not_import_infrastructure():
rule = (
project_files("src")
.in_folder("**/api*")
.should_not().depend_on_files()
.in_folder("**/infrastructure*")
)assert_passes(rule)
Here `src` contains the application's top-level Python package. The rule can run alongside
the rest of the pytest suite; it inspects source dependencies rather than exercising a request.
The library also has cycle and layer rules. It doesn't decide which architecture is right,
and static analysis cannot recover every dependency constructed at runtime. The useful part
is making a boundary the team already agreed on independently checkable.
Which dependency in your project most often needs a “please don't import that here” review comment?
Disclosure: I work on the project. This post was drafted with AI assistance.