Skip to main content

A static code analyzer that enforces architectural boundaries for layers and outher libraries

Project description

Layers Linter

A static code analyzer that enforces architectural boundaries for layers and outher libraries

Overview

Layers Linter helps you maintain a clean architecture by:

  1. Enforcing layer dependencies: Ensures that dependencies between modules follow the allowed directions
  2. Restricting library usage: Verifies that libraries are only used in permitted layers
  3. Integrating with your workflow: Works as a standalone CLI tool or as a Flake8 plugin

The linter analyzes your project's import structure by parsing the AST (Abstract Syntax Tree) of Python files, building a dependency graph, and validating it against your configuration.

Installation

# Install from PyPI
pip install layers-linter

# Or install with Flake8 plugin support
pip install layers-linter[flake8]

Usage

Command Line Interface

Default path to deps.toml config in the current working directory.

# Basic usage
layers-linter /path/to/your/project

# With custom configuration file
layers-linter /path/to/your/project --config /path/to/your/deps.toml

Flake8 Plugin

Add to your .flake8 configuration:

[flake8]
select = LA
la-config = deps.toml

Then run Flake8 as usual:

Default path to deps.toml config in the current working directory.

flake8 /path/to/your/project [--la-config /path/to/your/config]

Configuration

Layers Linter uses a TOML configuration file to define your project's architecture. By default, it looks for a file named deps.toml in the current directory.

Configuration Format

# Optional: Modules to exclude from analysis
exclude_modules = ["*.__init__"]

# Layer definitions
[layers]

[layers.dicontainer]
description = ""
# Modules that belong to this layer
contains_modules = [
    "project.container"
]
# Layers that can use this layer (if empty list, no other layers can use it)
upstream = ["presentation"]
# Layers that this layer can use (if empty list, it can't use any other layers)
downstream = ["usecases"]


[layers.usecases]
description = ""
contains_modules = [
    "project.domains.*.use_cases.*",
    "project.domains.*.use_cases",
]
upstream = ["dicontainer", "presentation"]
downstream = []


[layers.presentation]
description = ""
contains_modules = [
    "project.presentation.*",
    "project.domains.*.endpoints"
]
upstream = []
downstream = ["dicontainer"]


# Library restrictions
[libs]

[libs.presentation]
# Layers that can use this library (if not specified, all layers can use it)
upstream = ["fastapi"]

Pattern Matching

The contains_modules field supports pattern matching with wildcards:

  • project.module - Exact match
  • project.module.* - Module and all submodules
  • project.*.module - Any module with the specified pattern
  • *.module - Any module ending with the specified pattern

Dependency Rules

  • Upstream: Controls which layers can use this layer

    • Empty list ([]): No other layers can use this layer
    • Not specified or "none": No restrictions
  • Downstream: Controls which layers this layer can use

    • Empty list ([]): This layer cannot use any other layers
    • Not specified or "none": No restrictions

Error Codes

  • LA001: Invalid dependency between layers

    • Example: Invalid dependency from layer 'presentation' to layer 'repositories'
  • LA020: Restricted library usage

    • Example: Layers [presentation] cannot use restricted library 'sqlalchemy'

Notes

  • Imports inside if typing.TYPE_CHECKING: blocks are ignored
  • The linter builds a complete dependency graph before validation, allowing for comprehensive analysis

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

layers_linter-1.0.1.tar.gz (9.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

layers_linter-1.0.1-py3-none-any.whl (8.7 kB view details)

Uploaded Python 3

File details

Details for the file layers_linter-1.0.1.tar.gz.

File metadata

  • Download URL: layers_linter-1.0.1.tar.gz
  • Upload date:
  • Size: 9.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.3

File hashes

Hashes for layers_linter-1.0.1.tar.gz
Algorithm Hash digest
SHA256 2057b3b3eb062a4b36b72f37eea635e23fa8170db3e74349e96c56c098cfad4a
MD5 411f9cca7c64ee4a0ca0928c101606d3
BLAKE2b-256 322e393e8779312cd0930b3bc2c471e273e899e2dbd9c89560720241570eafc4

See more details on using hashes here.

File details

Details for the file layers_linter-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for layers_linter-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ae0e5db236ae2242a4001b7a6db10911e0581881746cdd16f5f1c718820d93eb
MD5 20c3f4679ee103ea9ad6e413e0beb397
BLAKE2b-256 5f3d921eac76a38758890f25842f02b902c4ef123533ae880bf9409d3874f5a4

See more details on using hashes here.

Supported by

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