Skip to content

Imports

invoke_tasklib.imports

Import-graph tasks (imports.*).

imports.check-cycles reads the import graph statically; imports.check-isolated imports every module in its own interpreter. Both are read-only.

invoke_tasklib.imports.check_cycles

check_cycles(
    c: Context,
    *,
    lazy: bool = False,
    type_checking: bool = False,
) -> None

Check for cyclic imports with a static import graph.

The import graph of the package is built from the source files of tasklib.paths.src with ast, without importing anything (the package does not have to be installed; if the directory does not exist, the installed package is read), and each cycle is printed as a path (a -> b -> a). Imports under if TYPE_CHECKING: and inside functions never run when a module is imported, so they are ignored unless asked. A syntax error or a missing optional dependency is not reported as a cycle.

Parameters:

Name Type Description Default
c Context

The invoke context.

required
lazy bool

If True, also follow the imports inside functions.

False
type_checking bool

If True, also follow the imports under if TYPE_CHECKING:.

False

Raises:

Type Description
Exit

If the package is neither in tasklib.paths.src nor installed, a module cannot be parsed, or an import cycle is found.

invoke_tasklib.imports.check_isolated

check_isolated(c: Context) -> None

Check that every module imports on its own.

Every module of the package (not only the subpackages) is discovered without being imported, then imported in its own interpreter. A module that fails to import on its own depends on something that is only initialized as a side effect of importing another module first, which is the usual symptom of a cyclic import. It also fails on a missing dependency or a syntax error. It starts one interpreter per module, a few at a time, so it is slow for a package with heavy imports; see check_cycles for the static check.

The discovery runs in the Invoke process (it imports nothing); only the imports run in the new interpreters, so the task runs no command in the Invoke context.

The package that is checked is the one that the running interpreter finds: the installed (editable) package, or the one in the current directory for a flat layout. It is the source tree, not a built artifact, which release.build checks in isolated mode.

The output is one line per step and one line per module that fails, with the last line of its traceback, then the summary. The commands are not echoed, so a package of 300 modules does not write 300 lines.

Raises:

Type Description
Exit

If the package cannot be found, or if a module fails to import on its own (code 1). Every module is tried, and all the failures are listed.