A Python tool to guard against incorrect usage of python modules.
Project description
modguard
A Python tool to support and enforce a modular package architecture within a monorepo.
What is modguard?
Modguard enables you to explicitly define the public interface for a given module. Marking a package with a modguard.Boundary
makes all of its internals private by default, exposing only the members marked with the @public
decorator.
This promotes an architecture of decoupled modules, and ensures the communication between domains is only done through their expected public interfaces.
Modguard is incredibly lightweight, and has no impact on the runtime of your code. Instead, its checks are performed through a CLI tool performing static analysis.
Installation
pip install modguard
Usage
Add a Boundary
to the __init__.py
of the module you're creating an interface for.
# core/__init__.py
from modguard import Boundary
Boundary(__name__)
Add the public
decorator to any callable in the module that should be exported.
# core/main.py
from modguard import public
# Adding the decorator here signifies this function is public
@public
def public_function(user_id: int) -> str:
...
# This function will be considered private
def private_function():
...
Modguard will now flag any incorrect dependencies between modules.
> # From the root of your project
> modguard .
❌ ./utils/helpers.py: Import 'core.main.private_function' in ./utils/helpers.py is blocked by boundary 'core.main'
Details
Modguard works by analyzing the abstract syntax tree (AST) of your codebase. The Boundary
class and @public
decorator have no runtime behavior, but are detected by modguard statically.
License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file modguard-0.2.0.tar.gz
.
File metadata
- Download URL: modguard-0.2.0.tar.gz
- Upload date:
- Size: 19.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.12.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 155bd957741eed83768964571d60f3b9e5b73014186ab9927fcb308f49af40e2 |
|
MD5 | 6e3ebd53c759fffb1b973dd36ac8c2d0 |
|
BLAKE2b-256 | 039d41609a555893f00a4d0b799e0e6f1da4de1bbeeed838d35faffbffd8c2ef |
File details
Details for the file modguard-0.2.0-py3-none-any.whl
.
File metadata
- Download URL: modguard-0.2.0-py3-none-any.whl
- Upload date:
- Size: 19.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.12.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 91a75143c58aecb22c982c2dc99cadf71b6ccfacc7a1604aa1c139bf60d8881b |
|
MD5 | adf3395daa8f9f0660f9000f9f25102f |
|
BLAKE2b-256 | 176c93b80fd6a6fa1b472e0c2f8745a45af81277b9d6d5a55e18db1c5ee2382b |