Skip to content

import-cycle (IMP101)

Family: Imports (IMP) · Heuristic · Opt-in · Experimental · Severity warning (a bug that is rarely harmful) · No automatic fix

Opt-in: project modules that import each other while they load (an import cycle), reported once per group at the import closing a shortest cycle.

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 only when select or extend-select names its code or name. 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, and which imports count can still change. 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

Reports each group of the project's modules that import each other while they load. A group is a set of modules each of which reaches every other one through imports in module bodies: an import cycle, or several cycles sharing modules. Each group is reported once, at the import that closes a shortest cycle from the group's first module by name, with the other imports of that cycle as related locations.

The imports that count are those of the timing classes [tool.heki.imports] cycle-edges names, by default only "body":

  • "body": an import that runs while its module loads. It is in the module body or in a class body the module body runs, also on a branch or inside try. A constant by-name import there (importlib.import_module("pkg.b")) counts too, and so does a lazy import (lazy import, or a name __lazy_modules__ lists) whose name the module body reads.
  • "lazy": a lazy import whose name the module body does not read.
  • "function": an import inside a function.
  • "type-checking": an import under if TYPE_CHECKING:, which never runs.

What an import loads is decided as Python decides it, as for IMP201: from pkg import name imports the submodule pkg.name only when pkg/__init__.py binds no name before importing it. An import also loads the parent packages of its module first: import pkg.sub.leaf runs pkg/sub/__init__.py, and that load is a step of a cycle. The packages holding the importing module are the exception: Python loaded them before the module ran, so import pkg.b in pkg/a.py does not step back into pkg/__init__.py. An import of such a package itself, or of a name it binds (from pkg import name in pkg/a.py), is a step: it reads the package while it may still be loading. A name the package binds nowhere, with no module of that name among the project's files, is a submodule Python finds elsewhere (a compiled extension module), and no step.

What a report states

A report states that the modules of the group import each other: each step of the cycle it shows is an import, of a class cycle-edges names, written in one module and loading the next module or, first, its package.

Its premises:

  • Timing class: each import is of a class cycle-edges names. An import whose class heki cannot tell (a lazy import whose reads heki cannot place) is no step.
  • Module loaded: each import loads that project module: the name it gives, made absolute and resolved against the project's modules, with from imports split between a submodule and an attribute as for IMP201. An import whose module heki cannot name is no step: a by-name import of a name the code does not fix, or from a import c where heki cannot tell whether c is the submodule a.c (the package a loads either way, and is a step unless it holds the importing module, which then may not read it).

A report says where each import of the cycle is written, never that a branch around it is taken, and never that the cycle fails: modules that import each other can load fine.

Why is this bad?

Modules that import each other while they load work only while their statements stay in an order that lets each one find what it needs: moving a definition, adding an import, or importing another module of the group first can make an import fail with an ImportError about a partially initialized module. A group of modules importing each other also cannot be loaded, tested, or understood one at a time. Unlike a check that reads only the import statements, heki counts the imports that run while modules load: imports inside functions and under TYPE_CHECKING count only when cycle-edges asks for them, and it knows which module from a import b loads.

Example

# pkg/a.py
from pkg.b import helper_b


def helper_a() -> int:
    return helper_b() + 1
# pkg/b.py
from pkg.a import helper_a  # IMP101: closes the import cycle `pkg.a` -> `pkg.b` -> `pkg.a`


def helper_b() -> int:
    return 1


def uses_a() -> int:
    return helper_a()

Use instead:

# pkg/b.py
def helper_b() -> int:
    return 1


def uses_a() -> int:
    from pkg.a import helper_a

    return helper_a()

Fix safety

No automatic fix.

Options

  • [tool.heki.imports]: cycle-edges, the timing classes of the imports that count (configuration).
  • Its reports are heuristic warnings: lint.fail-on = "warning" makes a cycle fail the run.
  • A Ratchet baseline records each group by its modules, not by the import its report sits at: an entry covers a group when it holds all of the group's modules.

Known limitations

  • Imports heki does not see (exec, runpy, a module loaded from a file path, a write to sys.modules) are no steps, and neither is a cycle through a module outside the project.