Skip to content

Strict Mode

A module can enable 'strict mode' by setting strict: true in its configuration within tach.yml.

How does it work?

When a module is in strict mode, other modules may only import names declared in __all__. For Python packages marked as modules, __all__ is checked within __init__.py. This creates an explicit public interface for the module which prevents coupling to implementation details, and makes future changes easier.

Example

Given modules called 'core' and 'parsing', we may have tach.yml contents like this:

# tach.yml
modules:
- path: parsing
  depends_on:
  - core
  strict: true
- path: core
  depends_on: []
  strict: true

Then, in parsing.py, we may have:

from core.main import get_data  # This import fails

get_data()

This import would fail tach check with the following error:

 parsing.py[L1]: Module 'core' is in strict mode. Only imports from the public interface of this module are allowed. The import 'core.main.get_data' (in module 'parsing') is not included in __all__.

If get_data should actually be part of the public interface of 'core', it needs to be specified in __all__ of core/__init__.py or core.py:

core/__init__.py

from .main import get_data

__all__ = ["get_data"]

which would allow 'parsing' to depend on this interface:

from core import get_data  # This import is OK

get_data()
tach check will now pass!
 All module dependencies validated!