A Python tool to maintain a modular package architecture.


Keywords
python, module, package, guard, enforcement, boundary, enforcer, domain, architecture, ci, cli, code-quality, config, continu, dependency-management, developer-tools, devops, framework, library, monorepo, open-source, productivity, programming, rust, static-code-analysis, terminal
License
MIT
Install
pip install tach==0.6.5

Documentation

Tach

downloads version license python ci pyright ruff

a Python tool to enforce modular design, written in Rust.

Docs

gauge-logo

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_demo

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.)

Getting Started

Installation

pip install tach

Setup

Tach allows you to configure where you want to place module boundaries in your project.

You can do this interactively! 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.

If your Python code lives below your project root, you should mark your Python source root using the 's' key.

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 ''.

Enforcement

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 similar to this one:

❌ tach/check.py[L8]: Cannot import 'tach.filesystem'. Tag 'tach' cannot depend on 'tach.filesystem'. 

Each error indicates an import which violates your module's declared dependencies. If your terminal supports hyperlinks, you can click on the failing file path to go directly to the error.

Extras

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 you just want to see the dependencies and usages of a given file or module in your project, you can use tach report.

tach report my_package/
# OR
tach report my_module.py

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!