### Launch Interactive UI Source: https://import-linter.readthedocs.io/en/stable/ui Starts a local web server to visualize package architecture. The module must be importable from the current directory. ```bash import-linter explore mypackage ``` ```bash import-linter explore mypackage.subpackage ``` -------------------------------- ### Set up Pre-commit Hooks Source: https://import-linter.readthedocs.io/en/stable/contributing Install and set up pre-commit hooks for code quality checks. This is optional but recommended. ```bash just install-precommit ``` -------------------------------- ### Install import-linter with UI extras using poetry Source: https://import-linter.readthedocs.io/en/stable/get_started/install Install import-linter with the optional UI dependencies using poetry. ```bash poetry add "import-linter[ui]" --group dev ``` -------------------------------- ### Install import-linter with UI extras using uv Source: https://import-linter.readthedocs.io/en/stable/get_started/install Install import-linter with the optional UI dependencies using uv. ```bash uv add --dev "import-linter[ui]" ``` -------------------------------- ### Install import-linter with uv Source: https://import-linter.readthedocs.io/en/stable/get_started/install Use this command to install import-linter as a development dependency with uv. ```bash uv add --dev import-linter ``` -------------------------------- ### Install import-linter with poetry Source: https://import-linter.readthedocs.io/en/stable/get_started/install Use this command to install import-linter as a development dependency with poetry. ```bash poetry add import-linter --group dev ``` -------------------------------- ### Install import-linter with UI extras using pip Source: https://import-linter.readthedocs.io/en/stable/get_started/install Install import-linter with the optional UI dependencies using pip. ```bash pip install "import-linter[ui]" ``` -------------------------------- ### Install import-linter with pip Source: https://import-linter.readthedocs.io/en/stable/get_started/install Use this command to install import-linter as a development dependency with pip. ```bash pip install import-linter ``` -------------------------------- ### Simple Protected Contract (TOML with tool.importlinter) Source: https://import-linter.readthedocs.io/en/stable/contract_types/protected Define a protected contract using the `tool.importlinter` TOML structure. This configuration is equivalent to the previous TOML example but uses a different section. ```toml [tool.importlinter] root_package = "mypackage" [[tool.importlinter.contracts]] name = "Simple protected contract" type = "protected" protected_modules = [ "mypackage.protected", "mypackage.also_protected", ] allowed_importers = [ "mypackage.allowed", "mypackage.also_allowed", ] ``` -------------------------------- ### Simple Protected Contract (TOML) Source: https://import-linter.readthedocs.io/en/stable/contract_types/protected Define a protected contract using TOML format. This example specifies modules that should not be directly imported and lists the allowed importers. ```toml [importlinter] root_package = mypackage [importlinter:contract:simple-protected-contract] name = Simple protected contract type = protected protected_modules = mypackage.protected mypackage.also_protected allowed_importers = mypackage.allowed mypackage.also_allowed ``` -------------------------------- ### Forbidden Contract with External Packages (TOML with Arrays) Source: https://import-linter.readthedocs.io/en/stable/contract_types/forbidden Configures a forbidden contract in TOML using array syntax, including external packages. This example demonstrates the use of 'include_external_packages = true' and specifies forbidden external modules. ```TOML [tool.importlinter] root_package = "mypackage" include_external_packages = true [[tool.importlinter.contracts]] name = "My forbidden contract (internal and external packages)" type = "forbidden" source_modules = [ "mypackage.one", "mypackage.two", ] forbidden_modules = [ "mypackage.three", "django", "requests", ] ignore_imports = [ "mypackage.one.green -> sqlalchemy", ] ``` -------------------------------- ### Basic Forbidden Contract (TOML with Arrays) Source: https://import-linter.readthedocs.io/en/stable/contract_types/forbidden Defines a basic forbidden contract using TOML with module lists as arrays. This is functionally equivalent to the first TOML example but uses a different syntax for lists. ```TOML [tool.importlinter] root_package = "mypackage" [[tool.importlinter.contracts]] name = "My forbidden contract (internal packages only)" type = "forbidden" source_modules = [ "mypackage.one", "mypackage.two", "mypackage.three.blue", ] forbidden_modules = [ "mypackage.four", "mypackage.five.green", ] ignore_imports = [ "mypackage.one.green -> mypackage.utils", "mypackage.two -> mypackage.four", ] [[tool.importlinter.contracts]] # This contract prevents mypackage.one from importing # anything from its siblings. name = "Forbidden siblings contract" type = "forbidden" source_modules = [ "mypackage.one", ] forbidden_modules = [ "mypackage.*", ] [[tool.importlinter.contracts]] # This contract prevents mypackage.one from importing # anything from its descendants. # Note as_packages must be False, otherwise this contract would have no effect # due to the rules around overlapping modules. name = "Forbidden descendants contract" type = "forbidden" source_modules = [ "mypackage.one", ] forbidden_modules = [ "mypackage.one.**", ] as_packages = false ``` -------------------------------- ### Basic Forbidden Contract (TOML) Source: https://import-linter.readthedocs.io/en/stable/contract_types/forbidden Defines a basic forbidden contract using TOML. This contract prevents 'mypackage.one', 'mypackage.two', and 'mypackage.three.blue' from importing 'mypackage.four' or 'mypackage.five.green'. It also includes an example of ignoring specific imports. ```TOML [importlinter] root_package = mypackage [importlinter:contract:my-forbidden-contract] name = My forbidden contract (internal packages only) type = forbidden source_modules = mypackage.one mypackage.two mypackage.three.blue forbidden_modules = mypackage.four mypackage.five.green ignore_imports = mypackage.one.green -> mypackage.utils mypackage.two -> mypackage.four [importlinter:contract:forbidden-siblings] # This contract prevents mypackage.one from importing # anything from its siblings. name = Forbidden descendants contract type = forbidden source_modules = mypackage.one forbidden_modules = mypackage.* [importlinter:contract:forbidden-descendants] # This contract prevents mypackage.one from importing # anything from its descendants. # Note as_packages must be false, otherwise this contract would have no effect # due to the rules around overlapping modules. name = Forbidden descendants contract type = forbidden source_modules = mypackage.one forbidden_modules = mypackage.one.** as_packages = false ``` -------------------------------- ### Implement Custom ForbiddenImportContract Source: https://import-linter.readthedocs.io/en/stable/custom_contract_types Subclass `importlinter.Contract` to define a custom contract. Implement the `check` method to perform the validation logic and `render_broken_contract` to display issues. This example defines a contract to forbid specific imports. ```python from importlinter import Contract, ContractCheck, fields, output class ForbiddenImportContract(Contract): """ Contract that defines a single forbidden import between two modules. """ importer = fields.StringField() imported = fields.StringField() def check(self, graph, verbose): output.verbose_print( verbose, f"Getting import details from {self.importer} to {self.imported}..." ) forbidden_import_details = graph.get_import_details( importer=self.importer, imported=self.imported, ) import_exists = bool(forbidden_import_details) return ContractCheck( kept=not import_exists, metadata={ 'forbidden_import_details': forbidden_import_details, } ) def render_broken_contract(self, check): output.print_error( f'{self.importer} is not allowed to import {self.imported}:', bold=True, ) output.new_line() for details in check.metadata['forbidden_import_details']: line_number = details['line_number'] line_contents = details['line_contents'] output.indent_cursor() output.print_error(f'{self.importer}:{line_number}: {line_contents}') ``` -------------------------------- ### Serve Documentation Locally Source: https://import-linter.readthedocs.io/en/stable/contributing Build and serve the project documentation locally to preview changes. ```bash just serve-docs ``` -------------------------------- ### Build Documentation with Just Source: https://import-linter.readthedocs.io/en/stable/contributing Build the project documentation without serving it locally. ```bash just build-docs ``` -------------------------------- ### Navigate to Project Directory Source: https://import-linter.readthedocs.io/en/stable/contributing Change into the directory where you cloned the Import Linter repository. ```bash cd import-linter ``` -------------------------------- ### Run the linter with default settings Source: https://import-linter.readthedocs.io/en/stable/get_started/run Execute the linter using its default configuration and settings. ```bash lint-imports ``` -------------------------------- ### Run the linter with a custom config file Source: https://import-linter.readthedocs.io/en/stable/get_started/run Specify an alternative configuration file for the linter to use. ```bash lint-imports --config path/to/alternative-config.ini ``` -------------------------------- ### Configure pre-commit hook (local system) Source: https://import-linter.readthedocs.io/en/stable/get_started/run Set up Import Linter as a local pre-commit hook using `language: system`. ```yaml repos: - repo: local hooks: - id: lint_imports name: "Lint imports" entry: "lint-imports" # Adapt with custom arguments, if need be. language: system pass_filenames: false ``` -------------------------------- ### Run Comprehensive Check with Just Source: https://import-linter.readthedocs.io/en/stable/contributing Perform a full check including linters, documentation build, and tests across all supported Python versions before pushing changes. ```bash just check ``` -------------------------------- ### Show timings Source: https://import-linter.readthedocs.io/en/stable/get_started/run Display the execution times for graph building and contract checking. ```bash lint-imports --show-timings ``` -------------------------------- ### Draw Graph with Options Source: https://import-linter.readthedocs.io/en/stable/ui Outputs a dependency graph in DOT format with specified display options. Use flags to include import totals or cycle breakers. ```bash import-linter drawgraph mypackage | dot -Tpng -o graph.png ``` ```bash import-linter drawgraph mypackage --show-import-totals ``` ```bash import-linter drawgraph mypackage --show-cycle-breakers ``` -------------------------------- ### Basic TOML Configuration Source: https://import-linter.readthedocs.io/en/stable/get_started/configure This TOML format is an alternative for basic configuration. It allows specifying the root package and optional settings for external packages and type checking imports. ```toml [tool.importlinter] root_package = "mypackage" # Optional: include_external_packages = true exclude_type_checking_imports = true ``` -------------------------------- ### Run Linters with Just Source: https://import-linter.readthedocs.io/en/stable/contributing Execute the code linters using the 'just' task runner. ```bash just lint ``` -------------------------------- ### Run All Tests with Just Source: https://import-linter.readthedocs.io/en/stable/contributing Execute all tests across all supported Python versions using the 'just test-all' command. This also tests against the lowest dependency versions. ```bash just test-all ``` -------------------------------- ### Basic INI Configuration Source: https://import-linter.readthedocs.io/en/stable/get_started/configure Use this INI format for basic configuration, specifying the root package and optional settings like including external packages or excluding type checking imports. ```ini [importlinter] root_package = mypackage # Optional: include_external_packages = True exclude_type_checking_imports = True ``` -------------------------------- ### Configure pre-commit hook (remote repository) Source: https://import-linter.readthedocs.io/en/stable/get_started/run Set up Import Linter as a pre-commit hook by referencing its GitHub repository. ```yaml - repo: https://github.com/seddonym/import-linter rev: hooks: - id: import-linter ``` -------------------------------- ### Format Code with Just Source: https://import-linter.readthedocs.io/en/stable/contributing Run the code formatting command using the 'just' task runner. ```bash just format ``` -------------------------------- ### Use a custom cache directory Source: https://import-linter.readthedocs.io/en/stable/get_started/run Configure the linter to use a different directory for caching. ```bash lint-imports --cache-dir path/to/cache ``` -------------------------------- ### Protected Contract with Wildcards (TOML with tool.importlinter) Source: https://import-linter.readthedocs.io/en/stable/contract_types/protected Configure a protected contract using `tool.importlinter` TOML, featuring wildcard matching for protected modules and allowed importers. The `as_packages` option is set to `false`. ```toml [tool.importlinter] root_package = "mypackage" [[tool.importlinter.contracts]] name = "Models can only be imported by colors direct descendant" type = "protected" protected_modules = [ "mypackage.**.models", ] allowed_importers = [ "mypackage.colors.*", ] ignore_imports = [ "mypackage.one.green -> mypackage.one.models", "mypackage.colors.red.foo -> mypackage.three.models", ] as_packages = false ``` -------------------------------- ### Run Tests with Just Source: https://import-linter.readthedocs.io/en/stable/contributing Execute the test suite using the 'just' task runner, typically running against the latest supported Python version. ```bash just test ``` -------------------------------- ### Clone the Import Linter Repository Source: https://import-linter.readthedocs.io/en/stable/contributing Clone your fork of the Import Linter repository to your local machine. ```bash git clone git@github.com:your_name_here/import-linter.git ``` -------------------------------- ### Minimal Acyclic Siblings Contract (TOML) Source: https://import-linter.readthedocs.io/en/stable/contract_types/acyclic_siblings Sets up a minimal acyclic_siblings contract using TOML configuration. Specifies the root package and contract details like name, type, and ancestors. ```toml [tool.importlinter] root_package = "mypackage" [[tool.importlinter.contracts]] name = "Minimal acyclic siblings contract" type = "acyclic_siblings" ancestors = ["mypackage"] ``` -------------------------------- ### Multiple Root Packages TOML Configuration Source: https://import-linter.readthedocs.io/en/stable/get_started/configure This TOML format allows configuring Import Linter with multiple root packages, suitable for complex project structures. ```toml [tool.importlinter] root_packages = [ "packageone", "packagetwo" ] # Optional: include_external_packages = true exclude_type_checking_imports = true ``` -------------------------------- ### Layering across multiple root packages in .ini format Source: https://import-linter.readthedocs.io/en/stable/contract_types/layers Configure a layers contract for a project with multiple top-level packages. Ensure these packages are also listed in the '[importlinter] root_packages' configuration. ```ini [importlinter] root_packages= high medium low [importlinter:contract:my-layers-contract] name = My three-tier layers contract (multiple root packages) type = layers layers = high medium low ``` -------------------------------- ### Read Project Configuration Source: https://import-linter.readthedocs.io/en/stable/api Use this function to programmatically read your project's Import Linter configuration. If no filename is provided, it defaults to the standard location. This is useful for external tools that need to analyze contract details. ```python >>> from importlinter import api >>> api.read_configuration() { "session_options": {"root_packages": ["importlinter"]}, "contracts_options": [ { "containers": ["importlinter"], "layers": [ "cli", "api", "configuration", "adapters", "contracts", "application", "domain", ], "name": "Layered architecture", "type": "layers", } ], } ``` -------------------------------- ### Multiple Contracts TOML Configuration Source: https://import-linter.readthedocs.io/en/stable/get_started/configure Configure multiple contracts using the TOML format. Each contract needs a name and type, and can include an optional ID for selection. ```toml [[tool.importlinter.contracts]] name = "Contract One" type = "some_contract_type" # (additional options) [[tool.importlinter.contracts]] id = "two" # Optional name = "Contract Two" type = "another_contract_type" # (additional options) ``` -------------------------------- ### Verbose mode Source: https://import-linter.readthedocs.io/en/stable/get_started/run Enable noisy output to show progress during execution. ```bash lint-imports --verbose ``` -------------------------------- ### Layering across multiple root packages in TOML format Source: https://import-linter.readthedocs.io/en/stable/contract_types/layers Configure a layers contract for a project with multiple top-level packages using TOML. The root packages must be declared in the '[tool.importlinter] root_packages' section. ```toml [tool.importlinter] root_packages = [ "high", "medium", "low", ] [[tool.importlinter.contracts]] name = "My three-tier layers contract (multiple root packages)" type = "layers" layers = [ "high", "medium", "low", ] ``` -------------------------------- ### Draw Graph to stdout Source: https://import-linter.readthedocs.io/en/stable/ui Outputs a dependency graph in DOT format to standard output. This can be piped to tools like Graphviz. ```bash import-linter drawgraph mypackage ``` -------------------------------- ### Protected Contract with Wildcards (TOML) Source: https://import-linter.readthedocs.io/en/stable/contract_types/protected Configure a protected contract using TOML, including wildcard matching for protected modules and allowed importers. The `ignore_imports` option can be used to exclude specific import paths from the check. ```toml [importlinter] root_package = mypackage [importlinter:contract:models-can-only-be-imported-by-colors] name = Models can only be imported by colors direct descendant type = protected protected_modules = mypackage.**.models allowed_importers = mypackage.colors.* ignore_imports = mypackage.one.green -> mypackage.one.models mypackage.colors.red.foo -> mypackage.three.models as_packages = False ``` -------------------------------- ### Define Layers Contract in .ini format Source: https://import-linter.readthedocs.io/en/stable/contract_types/layers Configure a layers contract using the .ini file format. Specify the contract name, type, and the ordered list of layers from highest to lowest. ```ini [importlinter:contract:my-layers-contract] name = My layers contract type = layers layers = mypackage.high mypackage.medium mypackage.low ``` -------------------------------- ### Sample Broken Acyclic Siblings Contract Output Source: https://import-linter.readthedocs.io/en/stable/contract_types/acyclic_siblings Illustrates the output when an acyclic_siblings contract is violated, highlighting the specific cycles and dependencies that need to be removed. ```text Broken acyclic_siblings contract -------------------------------- No cycles are allowed in mypackage.foo. It could be made acyclic by removing 1 dependency: - .alpha -> .beta (3 imports) No cycles are allowed in mypackage.foo.alpha. It could be made acyclic by removing 11 dependencies: - .blue -> .green (4 imports) - .blue -> .yellow (11 imports) - .green -> .orange (1 import) - .purple -> .red (3 imports) - .purple -> .yellow (2 imports) (and 6 more). ``` -------------------------------- ### Multiple Contracts INI Configuration Source: https://import-linter.readthedocs.io/en/stable/get_started/configure Define multiple contracts using the INI format. Each contract requires a name and type, with additional options specific to the contract type. ```ini [importlinter:contract:one] name = Contract One type = some_contract_type # (additional options) [importlinter:contract:two] name = Contract Two type = another_contract_type # (additional options) ``` -------------------------------- ### Disable caching Source: https://import-linter.readthedocs.io/en/stable/get_started/run Run the linter without utilizing any caching mechanisms. ```bash lint-imports --no-cache ``` -------------------------------- ### Multiple Root Packages INI Configuration Source: https://import-linter.readthedocs.io/en/stable/get_started/configure Configure Import Linter with multiple root packages using this INI format. This is useful for monorepos or projects with several distinct packages. ```ini [importlinter] root_packages= packageone packagetwo # Optional: include_external_packages = True exclude_type_checking_imports = True ``` -------------------------------- ### Minimal Acyclic Siblings Contract (INI) Source: https://import-linter.readthedocs.io/en/stable/contract_types/acyclic_siblings Defines a basic acyclic_siblings contract using INI configuration. Sets the root package and the contract's name, type, and ancestors. ```ini [importlinter] root_package = mypackage [importlinter:contract:my-contract] name = Minimal acyclic siblings contract type = acyclic_siblings ancestors = mypackage ``` -------------------------------- ### Configure Import Linter Contract Source: https://import-linter.readthedocs.io/en/stable This configuration defines a 'forbidden' contract where modules within 'myproject.green' are not allowed to import from 'myproject.blue'. Ensure the root_package is set correctly for your project. ```ini # .importlinter [importlinter] root_package = myproject [importlinter:contract:one] name = Green must not import blue type = forbidden source_modules = myproject.green forbidden_modules = myproject.blue ``` -------------------------------- ### Register Custom Contract Type (TOML) Source: https://import-linter.readthedocs.io/en/stable/custom_contract_types Register your custom contract type in the `[importlinter]` section of your TOML configuration file by mapping a name to the Python path of your contract class. ```toml [importlinter] root_package = mypackage contract_types= forbidden_import: somepackage.contracts.ForbiddenImportContract ``` -------------------------------- ### Define Independence Contract (INI) Source: https://import-linter.readthedocs.io/en/stable/contract_types/independence Use this format to define an independence contract in an .importlinter configuration file. Specify the contract name, type, and the modules that should be independent. You can also list imports to ignore. ```ini [importlinter:contract:my-independence-contract] name = My independence contract type = independence modules = mypackage.foo mypackage.bar mypackage.baz ignore_imports = mypackage.bar.green -> mypackage.utils mypackage.baz.blue -> mypackage.foo.purple ``` -------------------------------- ### Define Layers Contract in TOML format Source: https://import-linter.readthedocs.io/en/stable/contract_types/layers Configure a layers contract using the TOML file format. This is an alternative to the .ini format for defining the contract name, type, and layer hierarchy. ```toml [[tool.importlinter.contracts]] name = "My layers contract" type = "layers" layers = [ "mypackage.high", "mypackage.medium", "mypackage.low", ] ``` -------------------------------- ### Specify Custom Cache Directory Source: https://import-linter.readthedocs.io/en/stable/caching Change the cache directory by passing a different `cache_dir` argument to the `lint-imports` command. ```bash lint-imports --cache-dir /path/to/cache ``` -------------------------------- ### Check specific contracts Source: https://import-linter.readthedocs.io/en/stable/get_started/run Limit the linting process to one or more specific contracts by their IDs. ```bash lint-imports --contract some-contract --contract another-contract ``` -------------------------------- ### Define Non-Independent Sibling Modules in Layers (Pyproject.toml) Source: https://import-linter.readthedocs.io/en/stable/contract_types/layers Use colons to separate sibling modules that are allowed to import from each other. This allows for a more relaxed architecture where siblings can have interdependencies. ```toml [[tool.importlinter.contracts]] name = "Contract with sibling modules (independent)" type = "layers" layers = [ "mypackage.high", "mypackage.blue : mypackage.green : mypackage.yellow", "mypackage.low", ] ``` -------------------------------- ### read_configuration Source: https://import-linter.readthedocs.io/en/stable/api Reads the Import Linter configuration from a specified file or the default location. This function is intended for external projects that need to programmatically access and analyze contract configurations. ```APIDOC ## `read_configuration(config_filename=None)` ### Description Return a dictionary containing configuration from the supplied file. If no filename is supplied, look in the default location. This function is designed for use by external projects wishing to analyse the contracts themselves, e.g. to track the number of ignored imports. ### Parameters #### Path Parameters - **config_filename** (str, optional) - The path to the file containing the configuration. ### Returns A dictionary with two keys: - **"session_options"** (dict) - dictionary of strings passed as top level configuration. Note that if a single `root_package` is in the configuration, it will be normalised to a single-item list of `root_packages`, as shown in the example above. - **"contracts_options"** (list) - list of dictionaries, one for each contract, keyed with: - **"name"** (str) - the name of the contract. - **"type"** (str) - the type of the contract. - Any other contract-specific configuration. ### Return type dict ### Example ```python >>> from importlinter import api >>> api.read_configuration() { "session_options": {"root_packages": ["importlinter"]}, "contracts_options": [ { "containers": ["importlinter"], "layers": [ "cli", "api", "configuration", "adapters", "contracts", "application", "domain", ], "name": "Layered architecture", "type": "layers", } ], } ``` ``` -------------------------------- ### Register Custom Contract Type (Python) Source: https://import-linter.readthedocs.io/en/stable/custom_contract_types Register your custom contract type in the `[tool.importlinter]` section of your Python configuration file by mapping a name to the Python path of your contract class. ```python [tool.importlinter] root_package = "mypackage" contract_types = [ "forbidden_import: somepackage.contracts.ForbiddenImportContract" ] ``` -------------------------------- ### Acyclic Siblings Contract with Options (TOML) Source: https://import-linter.readthedocs.io/en/stable/contract_types/acyclic_siblings Defines an acyclic_siblings contract with extended configuration in TOML, including depth, skipped descendants, and ignored imports. ```toml [[tool.importlinter.contracts]] name = "Acyclic siblings contract with more options" type = "acyclic_siblings" ancestors = [ "mypackage.foo", "mypackage.bar.*", ] depth = 5 skip_descendants = [ "mypackage.foo.purple", "mypackage.foo.**.orange", ] ignore_imports = [ "mypackage.foo.blue.one -> mypackage.foo.green.two", ] ``` -------------------------------- ### Define Non-Independent Sibling Modules in Layers (TOML) Source: https://import-linter.readthedocs.io/en/stable/contract_types/layers Use colons to separate sibling modules that are allowed to import from each other. This allows for a more relaxed architecture where siblings can have interdependencies. ```toml [importlinter:contract:my-layers-contract] name = Contract with sibling modules (independent) type = layers layers = mypackage.high mypackage.blue : mypackage.green : mypackage.yellow mypackage.low ``` -------------------------------- ### Define Custom Contract in TOML Source: https://import-linter.readthedocs.io/en/stable/custom_contract_types Use this TOML format to define a custom contract, specifying its name, type, importer, and imported modules. This configuration is typically placed in a pyproject.toml file. ```toml [importlinter:contract:my-custom-contract] name = "My custom contract" type = "forbidden_import" importer = "mypackage.foo" imported = "mypackage.bar" ``` ```toml [[tool.importlinter.contracts]] name = "My custom contract" type = "forbidden_import" importer = "mypackage.foo" imported = "mypackage.bar" ``` -------------------------------- ### Acyclic Siblings Contract with Options (INI) Source: https://import-linter.readthedocs.io/en/stable/contract_types/acyclic_siblings Configures an acyclic_siblings contract with advanced options including depth, skipped descendants, and ignored imports using INI format. ```ini [importlinter:contract:my-contract] name = Acyclic siblings contract with more options type = acyclic_siblings ancestors = mypackage.foo mypackage.bar.* depth = 5 skip_descendants = mypackage.foo.purple mypackage.foo.**.orange ignore_imports = mypackage.foo.blue.one -> mypackage.foo.green.two ``` -------------------------------- ### Run Pytest Directly with UV Source: https://import-linter.readthedocs.io/en/stable/contributing Invoke pytest directly using 'uv run' to execute specific tests or test suites. ```bash uv run pytest tests/unit/contracts ``` -------------------------------- ### Define Layers Contract with a Single Container Source: https://import-linter.readthedocs.io/en/stable/contract_types/layers Use a container to specify a group of layers, avoiding repetition in the 'layers' section. This is useful for defining a common structure across multiple packages. ```toml [importlinter:contract:my-layers-contract] name = My layers contract type = layers layers = high medium low containers = mypackage ``` ```toml [[tool.importlinter.contracts]] name = "My layers contract" type = "layers" layers = [ "high", "medium", "low", ] containers = [ "mypackage", ] ``` -------------------------------- ### Disable Caching Source: https://import-linter.readthedocs.io/en/stable/caching Skip using and writing to the cache by passing the `--no-cache` flag to the `lint-imports` command. ```bash lint-imports --no-cache ``` -------------------------------- ### Define Layers Contract with Multiple Containers Source: https://import-linter.readthedocs.io/en/stable/contract_types/layers Specify multiple containers for a layers contract, where each container has its own layered architecture. This allows for independent layered structures within different sub-packages. ```toml [importlinter:contract:my-layers-contract] name = My multiple package layers contract type = layers layers = high (medium) low containers = mypackage.foo mypackage.bar mypackage.baz ``` ```toml [[tool.importlinter.contracts]] name = "My multiple package layers contract" type = "layers" layers = [ "high", "(medium)", "low", ] containers = [ "mypackage.foo", "mypackage.bar", "mypackage.baz", ] ``` -------------------------------- ### Forbidden Contract with External Packages (TOML) Source: https://import-linter.readthedocs.io/en/stable/contract_types/forbidden Configures a forbidden contract in TOML that includes external packages like 'django' and 'requests'. Requires 'include_external_packages = True' in the main configuration. ```TOML [importlinter] root_package = mypackage include_external_packages = True [importlinter:contract:my-forbidden-contract] name = My forbidden contract (internal and external packages) type = forbidden source_modules = mypackage.one mypackage.two forbidden_modules = mypackage.three django requests ignore_imports = mypackage.one.green -> sqlalchemy ``` -------------------------------- ### Define Independence Contract (TOML) Source: https://import-linter.readthedocs.io/en/stable/contract_types/independence Use this format to define an independence contract within the [tool.importlinter.contracts] section of your pyproject.toml file. This configuration specifies the contract name, type, modules, and any imports to ignore. ```toml [[tool.importlinter.contracts]] name = "My independence contract" type = "independence" modules = [ "mypackage.foo", "mypackage.bar", "mypackage.baz", ] ignore_imports = [ "mypackage.bar.green -> mypackage.utils", "mypackage.baz.blue -> mypackage.foo.purple", ] ``` -------------------------------- ### Define Independent Sibling Modules in Layers (TOML) Source: https://import-linter.readthedocs.io/en/stable/contract_types/layers Use pipe characters to separate sibling modules that must remain independent of each other within the same layer. This configuration is suitable for enforcing strict module boundaries. ```toml [importlinter:contract:my-layers-contract] name = Contract with sibling modules (independent) type = layers layers = mypackage.high mypackage.blue | mypackage.green | mypackage.yellow mypackage.low ``` -------------------------------- ### Invalid Layer Configuration: Mixing Separators (TOML) Source: https://import-linter.readthedocs.io/en/stable/contract_types/layers This configuration is invalid because it mixes pipe and colon separators on the same line within the layers definition. Import Linter requires a consistent separator type per line. ```toml [importlinter:contract:my-invalid-contract] name = Invalid contract type = layers layers = mypackage.high # The line below is invalid, as it mixes separators. mypackage.blue | mypackage.green : mypackage.yellow mypackage.low ``` -------------------------------- ### Invalid Layer Configuration: Mixing Separators (Pyproject.toml) Source: https://import-linter.readthedocs.io/en/stable/contract_types/layers This configuration is invalid because it mixes pipe and colon separators on the same line within the layers definition. Import Linter requires a consistent separator type per line. ```toml [[tool.importlinter.contracts]] name = "Invalid contract" type = "layers" layers = [ "mypackage.high", # The line below is invalid, as it mixes separators. "mypackage.blue | mypackage.green : mypackage.yellow", "mypackage.low", ] ``` -------------------------------- ### Define Independent Sibling Modules in Layers (Pyproject.toml) Source: https://import-linter.readthedocs.io/en/stable/contract_types/layers Use pipe characters to separate sibling modules that must remain independent of each other within the same layer. This configuration is suitable for enforcing strict module boundaries. ```toml [[tool.importlinter.contracts]] name = "Contract with sibling modules (independent)" type = "layers" layers = [ "mypackage.high", "mypackage.blue | mypackage.green | mypackage.yellow", "mypackage.low", ] ``` -------------------------------- ### Define Exhaustive Layers Contract with Ignores Source: https://import-linter.readthedocs.io/en/stable/contract_types/layers Mark a layers contract as 'exhaustive' to ensure all modules within containers are defined as layers. Use 'exhaustive_ignores' to specify modules that should be excluded from this check. ```toml [importlinter:contract:my-layers-contract] name = My multiple package layers contract type = layers layers = high (medium) low containers= mypackage.foo mypackage.bar mypackage.baz exhaustive = true exhaustive_ignores = utils ``` ```toml [[tool.importlinter.contracts]] name = "My multiple package layers contract" type = "layers" layers = [ "high", "(medium)", "low", ] containers = [ "mypackage.foo", "mypackage.bar", "mypackage.baz", ] exhaustive = true exhaustive_ignores = [ "utils", ] ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.