Skip to main content

Relaxed layer architecture linter based on the import graph.

Project description

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-enforce

For Development

pip install -e .[test,lint]

Usage

layer-enforcer myproject myotherproject --layers layers.yaml

layers.yaml

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"]

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

layer_enforcer-0.1.1.tar.gz (7.3 kB view details)

Uploaded Source

Built Distribution

layer_enforcer-0.1.1-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

Details for the file layer_enforcer-0.1.1.tar.gz.

File metadata

  • Download URL: layer_enforcer-0.1.1.tar.gz
  • Upload date:
  • Size: 7.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.4.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.9.5

File hashes

Hashes for layer_enforcer-0.1.1.tar.gz
Algorithm Hash digest
SHA256 3d64d99989ed270f15765efc82c35754c46d337f41531b1f1a59fec928cd8fd8
MD5 e2d86305ca05f46b5f150fd61037479c
BLAKE2b-256 c37256d61758771b4cb4b89387b0dd77800f83d2e261626694226c163ec58cfa

See more details on using hashes here.

File details

Details for the file layer_enforcer-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: layer_enforcer-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 7.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.4.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.9.5

File hashes

Hashes for layer_enforcer-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 9b4d9c3710c0689556cd27de3c4613926b169dfbcb061332fa3404171f1a2157
MD5 33e0391096f13965ef50aa33899f3a76
BLAKE2b-256 b8ed360e193741936dcfd99869e2892bb87ffefd98654293591a145aabaf90ac

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page