Skip to content

module-only-imports (KIS001)

Module-only imports

Fix is available.

This rule is enabled by default.

What it does

Checks that every from X import Y imports a module (a sub-package or .py file), not an object such as a function, class or constant.

Why is this bad?

Importing only modules keeps the origin of every name visible at the call site (path.join(...) rather than a bare join(...)) and avoids name clashes. See the Google Python Style Guide ยง2.2.

Example

from os.path import join   # KIS001: `join` is a function, not a module

Use instead:

import os.path
from os import path        # `path` is a module

Fix behavior

konform rewrites the import when it is safe to do so. It leaves the violation for you to fix by hand when the new import's name is already bound elsewhere in the file: as a local variable, or by a different import that would then overlap with it.

Unresolved imports

When a package isn't installed in this environment, KIS001 can't tell whether the imported name is a module. unresolved-level controls how that is reported. The extra search roots are described under module search roots.

Options

Configured in [tool.konform.lint.module-only-imports].

Option Type Default Description
exceptions list[str] ["__future__", "typing", "typing_extensions", "collections.abc"] Modules that may import objects directly.
level "warning" \| "error" "error" Severity of violations.
unresolved-level "warning" \| "error" \| "off" "warning" Severity for imports from packages that can't be resolved.

Suppression

x = 1  # noqa: KIS001
x = 1  # noqa: KIS  (every KIS* rule)