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:
- Enforcing layer dependencies: Ensures that dependencies between modules follow the allowed directions
- Restricting library usage: Verifies that libraries are only used in permitted layers
- 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 this layer can use (if empty list, it can't use any other layers)
depends_on = ["usecases"]
[layers.usecases]
description = ""
contains_modules = [
"project.domains.*.use_cases.*",
"project.domains.*.use_cases",
]
depends_on = []
[layers.presentation]
description = ""
contains_modules = [
"project.presentation.*",
"project.domains.*.endpoints"
]
depends_on = ["dicontainer"]
# Library restrictions
[libs]
[libs.presentation]
# Layers that can use this library (if not specified, all layers can use it)
allowed_in = ["fastapi"]
Pattern Matching
The contains_modules field supports pattern matching with wildcards:
project.module- Exact matchproject.module.*- Module and all submodulesproject.*.module- Any module with the specified pattern*.module- Any module ending with the specified pattern
Dependency Rules
- depends_on: Controls which layers this layer can use
- Empty list (
[]): This layer cannot use any other layers - Not specified or
"none": No restrictions
- Empty list (
Error Codes
-
LA001: Invalid dependency between layers
- Example:
Invalid dependency from layer 'presentation' to layer 'repositories'
- Example:
-
LA020: Restricted library usage
- Example:
Layers [presentation] cannot use restricted library 'sqlalchemy'
- Example:
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file layers_linter-2.0.0.tar.gz.
File metadata
- Download URL: layers_linter-2.0.0.tar.gz
- Upload date:
- Size: 10.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.6.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ca32cd5b060671a1cdf67c6deff502a2ca02a20a8b3fc12c08073cc018bc4732
|
|
| MD5 |
e0306f70b73965ed235eb3dd5f18a951
|
|
| BLAKE2b-256 |
df1e4390099821846ab0876e41704df742dccf7086262e4e690da38deb4d4f41
|
File details
Details for the file layers_linter-2.0.0-py3-none-any.whl.
File metadata
- Download URL: layers_linter-2.0.0-py3-none-any.whl
- Upload date:
- Size: 9.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.6.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
89400b6415fcd959b4c673ade96f5252dfd2e0ec93a46da7689568f8782bb123
|
|
| MD5 |
21b8020718e4623e680c582b6dcaf7e4
|
|
| BLAKE2b-256 |
a3e1c36b2ce57a46b4786fe506c7acf0f072a336ac83c351e559d904ad7e9418
|