Skip to content

import-boundary-violation (IMP201)

Family: Imports (IMP) · Heuristic · Runs when its contract is declared · Experimental · Severity error (a bug) · No automatic fix

An import that an import-boundaries contract does not allow: a forbidden module, or one outside the allowed imports.

Heuristic

This rule is heuristic: a premise of its claim can be open ("What a report states" lists them), so a report can be wrong about the code. It runs when its contract is declared. It has not been measured yet. See Rule Precision.

Experimental

It is new: its reports are heuristic until a review of its reports on real code bases certifies its claim. Experimental features can still change in behavior or precision, or be removed, in any release, and each change is listed in the release notes. An opt-in rule that is not experimental is stable and only off by default. See Experimental Features.

What it does

Checks the imports of the modules a [[tool.heki.import-boundaries]] contract judges against the modules it allows or forbids, and against how they must name the modules it allows only through a relative import (relative-imports). An import counts wherever it runs: in the module body, in a class body, inside a function, or lazily (lazy import, PEP 810), with or without an ImportError handler around it. An import under if TYPE_CHECKING: never runs, and counts only with type-checking = "report".

What an import loads is decided as Python decides it:

  • import a.b.c imports a.b.c. It loads a and a.b first, but it is no import of them.
  • from a.b import c imports the submodule a.b.c when the package a.b has it and binds no name c before importing it, and a.b otherwise: from pkg import parse imports the function when pkg/__init__.py re-exports it (from .parse import parse). A report of the import of a name names it, a.b.c: a baseline entry holds the name, so another name of a.b is a new report.
  • Relative imports are made absolute from the importing module.
  • importlib.import_module("pkg.db") and __import__("pkg.db") with a constant name, or a name heki establishes, import that module. With several possible names, the call is reported only when every one of them breaks the contract, and the report gives each name's reason.
  • An import of a module the project lacks below one of its packages fails, and is no import.

A module pattern is a whole module name or a glob whose * spans dots (pkg.backend.* names every module below pkg.backend, not pkg.backend itself), or a group: <stdlib>, <project>, <third-party>.

allowed-imports and the imported side of an ignored-imports pair also take one name of a module, module:name ("pkg.model.names:Resolver"; a glob may stand in either part). Only from pkg.model.names import Resolver is that import; another name of the module is reported as before. With reach, an import allowed or accepted by a name is not followed.

A module of relative-imports is allowed when the import names it relative to the importer (from . import b, from .b import f, importlib.import_module(".b", __package__)), and reported when it names it by its full name (from pkg._runtime import b, or a relative name anchored at a package the code spells), whatever the other lists say: a package copied into other packages under another name keeps working only with the relative imports.

With reach = "import-time", the contract judges every module that loading the importer loads while it loads, through the module bodies of the project's modules and the parent __init__ files each import loads first. With reach = "all", it follows every import that runs. Each module reached is reported once per importer, at the import that starts a shortest chain to it, with the rest of the chain as related locations. The packages holding the importer load before it, so they are never reached.

What a report states

A report states that the importer imports the module, where the import is written and when it runs, or, with reach, that an import of it leads to the module.

Its premises:

  • Judged importer: the module holding the import is one the contract's importers names (every module without the key) and exempt-importers does not. Established from the contract.
  • Module loaded: the import loads that module: the name it gives, made absolute and resolved against the project's modules, a from import of a name the package binds before any import of the submodule taken as an import of the package; with reach, a chain of imports of the classes the contract counts leads to it, each step loading the next module or, first, its parent package. An import whose module heki cannot name gives no report (IMP202 reports it with unresolved = "report").
  • Breaks the contract: the module matches forbidden-imports and neither allowed-imports nor relative-imports, or misses both when forbidden-imports is not set; or it matches relative-imports and the importer's own import names it by its full name. No ignored-imports pair accepts the import; a by-name import gives names heki knows whole, and every one of them does. Established from the contract.

A report says where an import is written, never that a branch around it is taken.

Why is this bad?

An import is how one part of a program comes to depend on another. A leaf module that imports a backend, shared code that imports a concrete implementation, or a command whose start-up loads a heavy library undoes a layering the project relies on, and a type checker or a linter does not see it: the import is valid Python. Unlike tools that read only the import statements, heki counts an import kept only for its effect, an import inside a function, and an import by a constant name, and it knows whether from a import b loads the submodule a.b.

Example

[[tool.heki.import-boundaries]]
name = "shared-model-backends"
importers = ["pkg.model.shared"]
forbidden-imports = ["pkg.model.backend", "pkg.model.backend.*"]
# pkg/model/shared.py
import importlib


def load() -> object:
    return importlib.import_module("pkg.model.backend")  # IMP201

Use instead:

# pkg/model/shared.py
from pkg.model import registry


def load() -> object:
    return registry.backend()

Outside a package, only its public entry:

[[tool.heki.import-boundaries]]
name = "billing-entry"
exempt-importers = ["app.billing", "app.billing.*"]
forbidden-imports = ["app.billing.*"]
# app/orders.py
from app.billing import Invoice  # the package's own export: allowed
from app.billing import invoice  # IMP201: the submodule `app.billing.invoice`

Fix safety

No automatic fix.

Options

  • [[tool.heki.import-boundaries]]: importers, exempt-importers, allowed-imports, forbidden-imports, relative-imports, ignored-imports, type-checking, reach, unresolved, message (configuration).