Skip to main content

Layer Enforcer

Layer Enforcer is a tool for linting your imports within the project.

The tool was designed to help maintain following order of things:

  • Dependencies always flow towards root of the tree. Often this is domain layer.

  • Dependencies may skip layers, as long as they flow in same direction. For instance, your web layer may import use cases from domain layer.

  • Infer layer from imports or module names. Say, if you import fastapi anywhere on the chain, the module is assigned to web layer, or maybe you want models to always be db layer.

  • Flexible layer structure. Preferably defined via config file.

  • Code is packaged by component. I.e. single component may contain code from the different layers.

Be warned, since this might not the best fit for you.

Algorithm

First pass: Match modules to layers. Report conflict if single module match more than one layer.

Second pass: For each module, iterate through all the imported modules. If current module has no assigned layer, assign first found layer within list of imported modules. If current module has an assigned layer, report conflict if import is not allowed.

Installation

pip install layer-enforcer

For Development

pip install -e .[test,lint]

Usage

layer-enforcer myproject myotherproject --layers layers.yml

pyproject.toml

Instead of explicitly specifying CLI arguments, it is possible to provide configuration via pyproject.toml within the tool.layer_enforcer table:

[tool.layer_enforcer]
modules = ["myproject", "myotherproject"]
ignore = ["myproject.containers"]
layers = "layers.yml"

layers.yml

An example of clean-architecture-ish layer layout for typical web app:

name: domain
submodules: ["entities", "use_cases"]
layers:
- name: service
  submodules: ["services"]
  layers:
  - name: infrastructure
    imports: ["stripe", "requests", "passlib"]
    layers:
    - name: db
      imports: ["sqlalchemy", "psycopg2", "alembic"]
      submodules: ["models", "memory", "database"]
    - name: web
      imports: ["fastapi", "jose"]
      submodules: ["views", "schemas"]
    - name: tasks
      imports: ["celery"]
      submodules: ["tasks"]

Metadata

Release files for layer-enforcer 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for layer-enforcer 0.2.0
File Size Uploaded Provenance Actions
layer_enforcer-0.2.0.tar.gz 10.3 kB Without provenance Details for layer_enforcer-0.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for layer-enforcer 0.2.0
File Interpreter ABI Platform Actions
layer_enforcer-0.2.0-py3-none-any.whl Python 3 none any Details for layer_enforcer-0.2.0-py3-none-any.whl

Total release size: 22.6 kB

Release files / layer_enforcer-0.2.0.tar.gz

Download URL layer_enforcer-0.2.0.tar.gz
Size 10.3 kB
Tags Source
SHA-256 checksum
How to use checksums
2ccb59fc75047a2f76f4bd88c0a87b3010d07f344746fdd21ae26f7c5345d3f7
BLAKE2b-256 checksum
How to use checksums
a089ca952cc42bf990469195aebe8ee2e79ebfb62649a6fa8f0d2b5e658fc12b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.5

Release files / layer_enforcer-0.2.0-py3-none-any.whl

Download URL layer_enforcer-0.2.0-py3-none-any.whl
Size 12.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
58b59f7198888d4dbad91b16bbe31313df129d2620c8f14ecec1af7632cd702b
BLAKE2b-256 checksum
How to use checksums
feceed86bc0e8bea30e5445475ec21737aec933ddf2d5527c1972d5e42dd0650
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.5

Security for layer-enforcer 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page