a Python tool to enforce modular design, written in Rust.
Tach lets you define and enforce dependencies between Python modules in your project.
This enforces a decoupled, modular architecture, which makes maintenance and development easier. If a module tries to import from another module that is not listed as a dependency, tach
will report an error.
Here's an example:
Tach is:
- π Open source
- π Installable via pip
- π§ Able to be adopted incrementally
- β‘ Implemented with no runtime impact
- βΎοΈ Interoperable with your existing systems (cli, hooks, ci, etc.)
pip install tach
Tach allows you to configure where you want to place module boundaries in your project.
You can do this interactively! From the root of your python project, run:
tach mod
# Up/Down: Navigate Enter: Mark/unmark module Right: Expand Left: Collapse Ctrl + Up: Jump to parent
# Ctrl + s: Exit and save Ctrl + c: Exit without saving Ctrl + a: Mark/unmark all
Mark and unmark each module boundary you want to create with 'Enter' (or 'Ctrl + a' to mark all sibling modules). Common choices would be to mark all of your top-level Python source packages, or just a few packages which you want to isolate.
Once you have marked all the modules you want to enforce constraints between, run:
tach sync
This will create the main configuration file for your project, tach.yml
, with the dependencies that currently exist between each module you've marked.
You can then see what Tach has found by viewing the tach.yml
's contents:
cat tach.yml
NOTE: Your 'project root' directory (the directory containing your tach.yml
) will implicitly be treated as a module boundary, and may show up in your dependency constraints as ''.
Tach comes with a simple cli command to enforce the boundaries that you just set up! From the root of your Python project, run:
tach check
You will see:
β
All module dependencies validated!
You can validate that Tach is working by either commenting out an item in a depends_on
key in tach.yml
, or by adding an import between modules that didn't previously import from each other.
Give both a try and run tach check
again. This will generate an error:
β path/file.py[LNO]: Cannot import 'path.other'. Tags ['scope:other'] cannot depend on ['scope:file'].
If an error is generated that is an intended dependency, you can sync your actual dependencies with tach.yml
:
tach sync
After running this command, tach check
will always pass.
If your configuration is in a bad state, from the root of your python project you can run:
tach clean
This will wipe all the configuration generated and enforced by Tach.
Tach also supports:
More info in the docs. Tach logs anonymized usage statistics which are easily opted out of. If you have any feedback, we'd love to talk!
If you have any questions or run into any issues, let us know by either reaching out on Discord or submitting a Github Issue!