Skip to main content

A dependency linter for Python projects

Project description

python-dependency-linter

A dependency linter for Python projects. Define rules for which modules can depend on what, and catch violations.

Installation

pip install python-dependency-linter

Or with uv:

uv add python-dependency-linter

Quick Start

Create .python-dependency-linter.yaml in your project root:

rules:
  - name: domain-isolation
    modules: contexts.*.domain
    allow:
      standard_library: [dataclasses, typing]
      third_party: [pydantic]
      local: [contexts.*.domain]

  - name: application-dependency
    modules: contexts.*.application
    allow:
      standard_library: ["*"]
      third_party: [pydantic]
      local:
        - contexts.*.application
        - contexts.*.domain

Run:

pdl check

Output:

contexts/boards/domain/models.py:6
    [domain-isolation] contexts.boards.domain.models → contexts.boards.application.service (local)

contexts/boards/domain/models.py:9
    [domain-isolation] contexts.boards.domain.models → sqlalchemy (third_party)

Found 2 violation(s).

Configuration

Rule Structure

Each rule has:

  • name — Rule identifier, shown in violation output
  • modules — Module pattern to apply the rule to (supports * wildcard)
  • allow — Whitelist: only listed dependencies are allowed
  • deny — Blacklist: listed dependencies are denied
rules:
  - name: rule-name
    modules: my_package.*.domain
    allow:
      standard_library: [dataclasses]
      third_party: [pydantic]
      local: [my_package.*.domain]
    deny:
      third_party: [boto3]

Import Categories

Dependencies are classified into three categories (per PEP 8):

  • standard_library — Python built-in modules (os, sys, typing, ...)
  • third_party — Installed packages (pydantic, sqlalchemy, ...)
  • local — Modules in your project

Behavior

  • No rule — Everything is allowed
  • allow only — Whitelist mode. Only listed dependencies are allowed
  • deny only — Blacklist mode. Listed dependencies are denied, rest allowed
  • allow + deny — Allow first, then deny removes exceptions
  • If allow exists but a category is omitted, that category allows all

Use "*" to allow all within a category:

allow:
  standard_library: ["*"]  # allow all standard library imports

Wildcard

* matches a single level in dotted module paths:

modules: contexts.*.domain  # matches contexts.boards.domain, contexts.auth.domain, ...

Rule Merging

When multiple rules match a module, they are merged. Specific rules override wildcard rules per field:

rules:
  - name: base
    modules: contexts.*.domain
    allow:
      third_party: [pydantic]

  - name: boards-extra
    modules: contexts.boards.domain
    allow:
      third_party: [attrs]  # merged: [pydantic, attrs]

pyproject.toml

You can also configure in pyproject.toml:

[[tool.python-dependency-linter.rules]]
name = "domain-isolation"
modules = "contexts.*.domain"

[tool.python-dependency-linter.rules.allow]
standard_library = ["dataclasses", "typing"]
third_party = ["pydantic"]
local = ["contexts.*.domain"]

CLI

# Check with default config (.python-dependency-linter.yaml)
pdl check

# Specify config file
pdl check --config path/to/config.yaml

# Specify project root
pdl check --project-root path/to/project

Exit codes:

  • 0 — No violations
  • 1 — Violations found

Pre-commit

Add to .pre-commit-config.yaml:

- repo: https://github.com/heumsi/python-dependency-linter
  rev: v0.1.0
  hooks:
    - id: python-dependency-linter

License

MIT

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

python_dependency_linter-0.1.0.tar.gz (18.1 kB view details)

Uploaded Source

Built Distribution

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

python_dependency_linter-0.1.0-py3-none-any.whl (9.2 kB view details)

Uploaded Python 3

File details

Details for the file python_dependency_linter-0.1.0.tar.gz.

File metadata

  • Download URL: python_dependency_linter-0.1.0.tar.gz
  • Upload date:
  • Size: 18.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for python_dependency_linter-0.1.0.tar.gz
Algorithm Hash digest
SHA256 58d066115eb34c9c4d4fe9ab0da0780cff9283168c6688526c59f655a9dc709b
MD5 c5e5effaf4148fd7e9b14f2f9560ea42
BLAKE2b-256 42748c10c35edba18db2b1a1d4b91a26bc192660be56fff1777d97c0406550bf

See more details on using hashes here.

Provenance

The following attestation bundles were made for python_dependency_linter-0.1.0.tar.gz:

Publisher: publish.yaml on heumsi/python-dependency-linter

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file python_dependency_linter-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for python_dependency_linter-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8f9f68c4580445515308e609fd1457972575e84b6600564da7fa22974ddf52f2
MD5 e08dfe09547afac2610e3cbb17699f3f
BLAKE2b-256 3b1b040fed35f6f785d089068de0735a8084d6b9cb979e132dc095103bdf96ed

See more details on using hashes here.

Provenance

The following attestation bundles were made for python_dependency_linter-0.1.0-py3-none-any.whl:

Publisher: publish.yaml on heumsi/python-dependency-linter

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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