lazy-import-mismatch (TID254)
Preview (since 0.15.6) · Related issues · View source
Derived from the flake8-tidy-imports linter.
Fix is sometimes available.
This rule is unstable and in preview. The --preview flag is required for use.
What it does
Enforces the configured lazy-import policy in contexts where lazy import
is legal.
Why is this bad?
Python 3.15 adds support for lazy import and lazy from ... import ...,
which defer the actual import work until the imported name is first used.
Depending on the policy, some modules should be imported lazily to defer import work until the name is first used, while others should remain eager to preserve import-time side effects.
The rule also recognizes imports made lazy by a literal __lazy_modules__
declaration, including when the target version is older than Python 3.15.
This declaration allows a module to use lazy imports on Python 3.15 and
later while retaining eager imports on older versions. Dynamic assignments
to __lazy_modules__ (e.g. __lazy_modules__ = non_literal()) are ignored,
as their effects cannot be determined statically.
This rule ignores contexts in which lazy import is invalid, such as
functions, classes, try/except blocks, __future__ imports, and
from ... import * statements.
Example
Use instead:
Fix availability
The fix is only available for statements that import a single name, since
changing lazy on a multi-member import could violate another name's policy.
The fix is also unavailable for imports marked lazy by a __lazy_modules__
declaration.
Fix safety
This rule's fix is marked as unsafe because changing when a module is imported can affect runtime behavior, including import-time side effects.