Skip to content

import-alias-policy (KIS002)

Import alias policy

Fix is available, but unsafe: it is only applied with --unsafe-fixes.

This rule is enabled by default.

What it does

Flags from X import Y as Z when the alias Z buys nothing: Y isn't bound to anything else in the module, so the alias only adds indirection.

Why is this bad?

An unneeded alias gives one thing two names. Readers must learn that bar_baz is really foo.bar.baz, grepping for baz misses its uses, and the same import can end up aliased differently from file to file. Aliases earn their place when they resolve a real name clash; this rule flags the ones that don't.

Example

from foo.bar import baz as bar_baz   # nothing else is named `baz`

bar_baz()

Use instead:

from foo.bar import baz

baz()

Fix behavior

With --fix --unsafe-fixes, konform drops the alias and renames every use of it back to the original name.

The fix is unsafe: konform can only see uses of the alias within the file being fixed, so it can't rule out dynamic references by name (for example getattr or globals()). It is skipped, and the violation is still reported, when the alias is shadowed somewhere or bound to more than one import in the file.

What is not flagged

# The alias avoids a clash with a local name or another import
from foo.bar import baz as bar_baz
baz = compute()

# Same name imported from different modules: the aliases keep them apart
from foo.bar import baz as bar_baz
from nor.kind import baz as kind_baz

# The alias is a deliberate re-export
__all__ = ["bar_baz"]
from foo.bar import baz as bar_baz

# Leading underscore: a "private, don't re-export" marker
from foo.bar import baz as _baz

Also left alone: self-aliases (from X import Y as Y, Ruff's PLC0414), relative imports (no stable module identity for a collision check) and plain import X as Z (dropping that alias changes what gets bound).

Enforcing one aliasing convention

If your project deliberately aliases imports (say, always <last module part>_<name>), set alias-template; aliases equal to its rendering are never flagged, even when the rename isn't needed.

[tool.konform.lint.import-alias-policy]
alias-template = "{module_last}_{name}"
from foo.bar import baz as bar_baz   # OK: matches the template
from foo.bar import baz as other     # KIS002: doesn't match, and isn't needed

Placeholders: {name}, {module} (dots become _), {module_first}, {module_last}. The template only exempts aliases, it never flags any. An invalid template is reported on stderr and ignored.

Options

Configured in [tool.konform.lint.import-alias-policy].

Option Type Default Description
level "warning" \| "error" "warning" Severity of violations.
alias-template str unset Aliases matching this template are always allowed.

Suppression

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