Skip to content

Import contracts

import-boundaries declares which modules a module may import. Contracts lists every kind and the keys they share.

import-boundaries

[[tool.heki.import-boundaries]]
name = "facts-leaf"
importers = ["pkg.facts"]
allowed-imports = ["<stdlib>", "pkg._records"]

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

[[tool.heki.import-boundaries]]
name = "no-root-import"
importers = ["pkg.*"]
forbidden-imports = ["pkg"]

Which modules the modules of importers may import, checked by IMP201. Its names are module patterns: a whole module name or a glob whose * spans dots, never a suffix (pkg.* names pkg.facts and pkg.model.shared, not pkg), or one of the groups <stdlib> (the standard library), <project> (the project's modules), and <third-party> (every other module).

Key Default Meaning
name "" The contract's name in reports.
importers every module The modules judged.
exempt-importers [] Modules of importers that are not judged.
allowed-imports [] The modules the importers may import: every other import is reported, but those of relative-imports. "pkg.db:Session" allows one name of a module: only from pkg.db import Session imports it (a glob may stand in either part), and another name of pkg.db is still reported.
forbidden-imports [] When set, only imports of these modules are reported, less the allowed ones, as in Allowed and forbidden functions. "pkg.parser:_*" forbids the names of a module that match: from pkg.parser.base import _helper is reported through "pkg.parser.*:_*", and from pkg.parser import jsonschema (a submodule) and from pkg.parser.base import Parser are not (a glob may stand in either part). A contract setting none of allowed-imports, forbidden-imports, and relative-imports allows no imports.
relative-imports [] The modules the importers may import only through a relative import (Relative imports only).
ignored-imports [] "importer -> imported" pairs of module patterns whose imports the contract accepts ("pkg.cli -> pkg.db"; "pkg.cli -> pkg.db:Session" accepts one name of the module, as in allowed-imports). With reach, an import accepted or allowed by a name is not followed.
type-checking "ignore" "ignore" or "report": whether the imports under if TYPE_CHECKING:, which never run, count.
reach "statement" "statement": each import of an importer is judged. "import-time": every module loading an importer loads while it loads. "all": every module a chain of imports that run leads to.
unresolved "ignore" "ignore" or "report": whether the imports whose module heki cannot name are reported under IMP202: a name the code does not fix, a call that may be importlib.import_module or __import__ (a method of that name on an object of unknown class, a name bound to one on some path), and a relative import that climbs past the top-level package.
module-reads "ignore" "ignore" or "report": whether a module an importer reaches by value, without importing it, counts as an import of it (Modules reached by value).
message none The help of every report of the contract.
fail false true: the reports of this contract make heki check (and --watch) exit with code 1, whatever their tier and severity; see Failing on a contract.
baseline "allow" "never": a baseline holds none of the reports of this contract; see Keeping a contract out of the baseline.
unused-allowances "ignore" "warning" or "error": heki check over the whole project reports each entry of exempt-importers, allowed-imports, ignored-imports, and exempt-decorators that allowed no import; see Unused allowances.
exempt-decorators [] Decorators ({ name = "pytest.mark.allow_fail", reasons = "required" }) that exempt the functions they mark when they state a reason: the contract accepts the imports (and the reads of module-reads) in their bodies; see Exempting decorators.
allowance-reasons "optional" "required": every entry of exempt-importers, allowed-imports, and ignored-imports comes from a group with a reason; see Reasons for allowances.
  • An import counts wherever it runs: in the module body, a class body, a function, or lazily, with or without an ImportError handler around it. importlib.import_module and __import__ with a constant name count as imports of that module.
  • import a.b.c imports a.b.c, not a or a.b, which it loads first. from a.b import c imports the submodule a.b.c when the package has one and binds no c before importing it, and a.b otherwise. An import of a module the project lacks below one of its packages fails, and is no import; from a.b import c of a name a.b binds nowhere still imports a.b, and may import a module a.b.c outside the project's files (a compiled extension module). When the package binds c to an object of its submodule after importing it (from .c import c), from a.b import c imports a.b, and also a.b.c while the package has not run that binding yet.
  • One report per import and module. A report's target, which a baseline counts it by, is the module imported.

Modules reached by value

With module-reads = "report", a module that an importer reaches by value, without an import statement, counts as an import of that module, and is reported under IMP201 as a read (`f` reads `pkg._impl` by value):

  • an attribute that is a project module: a submodule read through its package (pkg._impl.helper()), also handed on (register(pkg._impl)), or a name of another module holding the module (pkg.helpers.impl, also from pkg.helpers import impl);
  • getattr(pkg, "_impl") with a constant name, and a call returning a project module;
  • sys.modules["pkg._impl"] with a constant key, for any module.

An import by a constant name (importlib.import_module("pkg._impl").helper()) is an import, with or without the key; what it gives is not counted again.

A read names no module by its spelling, so a module of relative-imports is allowed. A read loads nothing: with reach, it is judged in its importer only. The report's target is the module, so an import and a read of the same module in one function share one baseline entry, which counts both. A read keeps the timing an import in its place has: under if TYPE_CHECKING: it counts only with type-checking = "report" (reads `pkg._impl` by value under `TYPE_CHECKING`), and reach = "import-time" counts only the reads of the module body. A load of a name bound to the module (impl.helper() after impl = pkg._impl) is no further read. sys.modules["m"] is taken to hold the module m: a module that puts another object there is not modelled. - With reach, each module reached is reported once per importer, at the import of the importer that starts a shortest chain to it, with the other steps as related locations. A step through a parent package counts: loading pkg.models.base runs pkg/models/__init__.py first. The packages holding the importer load before it and are never reached. The walks of one contract share a budget proportional to the import graph, in the order of the modules; past it, the importers left get no report. - Definitions are not import targets: who may use a class or a function is a call-owners or read-owners contract.

Outside a package, only its public entry: every module but the package's own may import app.billing, and none of the modules under it.

[[tool.heki.import-boundaries]]
name = "billing-entry"
exempt-importers = ["app.billing", "app.billing.*"]
forbidden-imports = ["app.billing.*"]
message = "import `app.billing` and use what it exports"

from app.billing import Invoice imports the package, while from app.billing import invoice, import app.billing.invoice, and from app.billing.invoice import Invoice import the submodule.

A start-up contract: loading pkg.cli must not load torch, however many modules lie between them.

[[tool.heki.import-boundaries]]
name = "startup"
importers = ["pkg.cli"]
forbidden-imports = ["torch", "torch.*"]
reach = "import-time"

Relative imports only

A package copied into other packages under another name works only while its modules import each other relatively: from pkg._runtime import b names a package the copy does not have.

[[tool.heki.import-boundaries]]
name = "runtime-leaf"
importers = ["pkg._runtime", "pkg._runtime.*"]
allowed-imports = ["<stdlib>"]
relative-imports = ["pkg._runtime", "pkg._runtime.*"]
# pkg/_runtime/a.py
from . import b  # allowed
from pkg._runtime import c  # IMP201: allowed only through a relative import
  • An import names a module relatively when it gives the module's place from the importer: from . import b, from .b import f, from .. import c, and importlib.import_module(".b", __package__) (or __name__). By its full name are import pkg._runtime.b, from pkg._runtime import b, importlib.import_module("pkg._runtime.b"), __import__("pkg._runtime.b"), and a relative name anchored at a package the code spells, importlib.import_module(".b", "pkg._runtime"). __import__ with a level other than 0 names a module heki cannot tell, which IMP202 reports with unresolved = "report". heki inspect --imports shows how each import names its modules (spelling).
  • A module the key matches needs no other entry. Imported relatively, it is allowed, also when forbidden-imports names it; imported by its full name, it is reported, also when allowed-imports names it. An ignored-imports pair accepts the import either way. A by-name import given several names judges each module by the names that give it: one a relative name and a full name both give counts as named by its full name.
  • The key takes module names and globs, not groups: a relative import reaches only the modules of the importer's own top-level package. An importer outside the package of a module the key matches cannot name it relatively, so each of its imports of that module is reported.
  • The imports judged are the ones the contract counts, as for the other keys: wherever they run, and under TYPE_CHECKING only with type-checking = "report". With reach, how an import names its module is judged only at the importer's own imports (with "import-time", those that run while it loads): how the modules on a chain import each other is theirs, and a module of the key that a chain reaches is allowed.
  • A report's target is the module imported, as for the other reports of the contract.