Skip to content

unresolved-guarded-import (IMP202)

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

Strict mode of an import-boundaries contract (unresolved = "report"): an import whose module heki cannot name, which may be one the contract does not allow.

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 the strict mode of IMP201, which is experimental. 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

The strict mode of a [[tool.heki.import-boundaries]] contract that sets unresolved = "report": reports the imports of the judged modules that IMP201 cannot judge because heki cannot name the module they load, when that module may be one the contract does not allow:

  • a by-name import (importlib.import_module, __import__) of a name the code does not fix;
  • a by-name import of a name built from a known start (f"pkg.plugins.{name}"), unless no forbidden pattern can match a module under it, or an allowed glob covers every one (pkg.plugins.*); a full name may also be one of a module relative-imports allows only through a relative import;
  • a by-name import of one of several names, some breaking the contract and some not; the report gives every reason one of them may break it for;
  • from a import c where heki cannot tell whether c is the submodule a.c: a package outside the project, one whose __init__.py binds c before the submodule may be imported, one that binds c to an object of the submodule after importing it (from .c import c), which the import takes the submodule instead of while the package is still loading, or one that binds no c while the project has no module a.c, which Python then looks for outside the project's files (a compiled extension module). The package a loads either way, and IMP201 judges it.

With reach, the sites of the modules a walk reaches are judged too, and reported at the import of the importer that starts the chain to them.

What a report states

A report states that the importer has an import whose module heki cannot name, that may be one the contract forbids or does not allow, or one it allows only through a relative import.

Why is this bad?

A contract that passes says nothing about the imports heki cannot follow. IMP202 reports them under their own code, so they can be counted, kept in a baseline, and reduced by writing the module name out, without mixing them with the violations IMP201 proves.

Example

[[tool.heki.import-boundaries]]
name = "core-plugins"
importers = ["pkg.core.*"]
forbidden-imports = ["pkg.plugins.*"]
unresolved = "report"
import importlib


def plugin(name: str) -> object:
    return importlib.import_module(f"pkg.plugins.{name}")  # IMP202

Fix safety

No automatic fix.

Options

  • [[tool.heki.import-boundaries]]:
  • unresolved: "ignore" (default) or "report".
  • the other keys: as for IMP201.