Skip to main content

Enforces rules for the imports within and between Python packages.

Project description

https://img.shields.io/pypi/v/import-linter.svg Python versions CI Status

Import Linter allows you to define and enforce rules for the imports within and between Python packages.

Overview

Import Linter is a command line tool to check that you are following a self-imposed architecture within your Python project. It does this by analysing the imports between all the modules in one or more Python packages, and compares this against a set of rules that you provide in a configuration file.

The configuration file contains one or more ‘contracts’. Each contract has a specific type, which determines the sort of rules it will apply. For example, the forbidden contract type allows you to check that certain modules or packages are not imported by parts of your project.

Import Linter is particularly useful if you are working on a complex codebase within a team, when you want to enforce a particular architectural style. In this case you can add Import Linter to your deployment pipeline, so that any code that does not follow the architecture will fail tests.

If there isn’t a built in contract type that fits your desired architecture, you can define a custom one.

Quick start

Install Import Linter:

pip install import-linter

Decide on the dependency flows you wish to check. In this example, we have decided to make sure that myproject.foo has dependencies on neither myproject.bar nor myproject.baz, so we will use the forbidden contract type.

Create an .importlinter file in the root of your project to define your contract(s). In this case:

[importlinter]
root_package = myproject

[importlinter:contract:1]
name=Foo doesn't import bar or baz
type=forbidden
source_modules=
    myproject.foo
forbidden_modules=
    myproject.bar
    myproject.baz

Now, from your project root, run:

lint-imports

If your code violates the contract, you will see an error message something like this:

=============
Import Linter
=============

---------
Contracts
---------

Analyzed 23 files, 44 dependencies.
-----------------------------------

Foo doesn't import bar or baz BROKEN

Contracts: 1 broken.


----------------
Broken contracts
----------------

Foo doesn't import bar or baz
-----------------------------

myproject.foo is not allowed to import myproject.bar:

-   myproject.foo.blue -> myproject.utils.red (l.16)
    myproject.utils.red -> myproject.utils.green (l.1)
    myproject.utils.green -> myproject.bar.yellow (l.3)

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

import-linter-1.12.1.tar.gz (28.4 kB view details)

Uploaded Source

Built Distribution

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

import_linter-1.12.1-py3-none-any.whl (40.8 kB view details)

Uploaded Python 3

File details

Details for the file import-linter-1.12.1.tar.gz.

File metadata

  • Download URL: import-linter-1.12.1.tar.gz
  • Upload date:
  • Size: 28.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for import-linter-1.12.1.tar.gz
Algorithm Hash digest
SHA256 3aa4da5bbed4b2e575fd2479bc7962090ae95666676fb872a4819a293b973463
MD5 35d8c6993f65c672dc02fa69b19b1925
BLAKE2b-256 7fbc0fd5d1e60a61b638ba45860856396f63e485e6e41b5dc39b7c36c3066472

See more details on using hashes here.

File details

Details for the file import_linter-1.12.1-py3-none-any.whl.

File metadata

  • Download URL: import_linter-1.12.1-py3-none-any.whl
  • Upload date:
  • Size: 40.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for import_linter-1.12.1-py3-none-any.whl
Algorithm Hash digest
SHA256 92f5ddec77c523bd95c0af88d3fdb354b705a776f7a0e59a7b25b035828a0257
MD5 3b811d7b69d17167d90b94efb1c1571c
BLAKE2b-256 fdef8caf983bfb4ddaa21524ccc402c15c4dd47377d66983cfae19ac77991c63

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