Detect imports that can be lazy
Project description
flake8-lazy
flake8-lazy is a flake8 plugin that finds imports which can be made lazy.
flake8-lazy helps keep import-time overhead low by detecting imports that can be
declared as lazy in __lazy_modules__. For this package itself,
flake8-lazy --help runs roughly twice as fast when using Python 3.15's new
lazy import system.
Error messages will mention __lazy_modules__, but the lazy keyword is
supported too.
Install
python -m pip install flake8-lazy
Usually you would include this in some sort of dependency-group in your project,
e.g. dev or lint.
flake8 will automatically discover the plugin. There's also a standalone
flake8-lazy runner.
Rule codes
1xx: Missing lazy declarations
LZY101: Missing lazy stdlib module in__lazy_modules__LZY102: Missing lazy third-party or local module in__lazy_modules__
2xx: __lazy_modules__ validation
LZY201:__lazy_modules__list is not sortedLZY202: Module listed in__lazy_modules__is never importedLZY203: Module listed in__lazy_modules__appears more than onceLZY204:__lazy_modules__is assigned after importing modules it names
3xx: Native lazy keyword (Python 3.15+)
LZY301: Lazy import insidesuppress(ImportError)is misleadingLZY302: Module is declared lazy by bothlazykeyword and__lazy_modules__LZY303: Module is imported both eagerly and lazily
4xx: Lazy import safety and semantics
LZY401: Module is declared lazy but accessed at the top level
Basic example
__lazy_modules__ = ["argparse", "pathlib"]
import argparse
import pathlib
import numpy
def run() -> None:
print(argparse.ArgumentParser)
In this example, numpy is never used at module runtime, so the checker expects
it in __lazy_modules__ and emits LZY102.
How detection works
flake8-lazy inspects module-scope imports and module runtime usage.
- Counts top-level
importandfrom ... import ...statements. - Ignores imports inside functions and classes.
- Treats annotation-only usage as lazy-capable.
- Treats usage inside
if TYPE_CHECKING:as type-only. - Skips
from __future__ import .... - Requires exact module entries for nested imports.
Nested import note:
import email.header
__lazy_modules__ = ["email"] # Not enough
This emits LZY101; the required entry is "email.header". PEP 810 requires
full module names.
Missing relative imports are mentioned as .name, but you need to list the full
name, or generate the full name dynamically if you support embedding.
CLI
The project also provides a direct CLI runner:
flake8-lazy path/to/file.py another_file.py
# or
uvx flake8-lazy path/to/file.py another_file.py
Output format matches flake8-style diagnostics:
path/to/file.py:12:0: LZY102 module 'numpy' should be listed in __lazy_modules__
The command exits with status code 1 if any error is found.
Authoring __lazy_modules__
Use a static, sorted list of strings:
__lazy_modules__ = [
"argparse",
"numpy",
"pathlib",
]
Dynamic values are intentionally ignored for now.
Local development
Run tests:
nox -s tests
# or
uv run pytest
Run linting:
nox -s lint
# or
prek -a
Build docs:
nox -s docs --non-interactive
Serve docs locally:
nox -s docs
Acknowledgements
GitHub Copilot in VS Code was used to help develop this package. The Scientific Python Development Guide template was used as a starting point.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file flake8_lazy-0.1.0.tar.gz.
File metadata
- Download URL: flake8_lazy-0.1.0.tar.gz
- Upload date:
- Size: 14.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cf894c7599ec4ecb8b353134406d77adc7f00201c460624b7cd278da1fc24695
|
|
| MD5 |
ae38d037fdc4904b4249716c628adf72
|
|
| BLAKE2b-256 |
cf10536aacae0c16df4e8d5f4bb7dfcc52bf0a770af70572a042c39ccea80107
|
Provenance
The following attestation bundles were made for flake8_lazy-0.1.0.tar.gz:
Publisher:
cd.yml on henryiii/flake8-lazy
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
flake8_lazy-0.1.0.tar.gz -
Subject digest:
cf894c7599ec4ecb8b353134406d77adc7f00201c460624b7cd278da1fc24695 - Sigstore transparency entry: 1138339711
- Sigstore integration time:
-
Permalink:
henryiii/flake8-lazy@dfd96c48d4093540834a500b5b3245005699c9d7 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/henryiii
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
cd.yml@dfd96c48d4093540834a500b5b3245005699c9d7 -
Trigger Event:
release
-
Statement type:
File details
Details for the file flake8_lazy-0.1.0-py3-none-any.whl.
File metadata
- Download URL: flake8_lazy-0.1.0-py3-none-any.whl
- Upload date:
- Size: 11.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7f77df57ddf7f75babf8e50af00c83a08ffed8e04e811152773cac2d957c2c8d
|
|
| MD5 |
859510a867b840d1194678323a67473a
|
|
| BLAKE2b-256 |
1fd1d79b2f85badc7765b7315ee9d015afeef1fd457115cade96359b201d8478
|
Provenance
The following attestation bundles were made for flake8_lazy-0.1.0-py3-none-any.whl:
Publisher:
cd.yml on henryiii/flake8-lazy
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
flake8_lazy-0.1.0-py3-none-any.whl -
Subject digest:
7f77df57ddf7f75babf8e50af00c83a08ffed8e04e811152773cac2d957c2c8d - Sigstore transparency entry: 1138339763
- Sigstore integration time:
-
Permalink:
henryiii/flake8-lazy@dfd96c48d4093540834a500b5b3245005699c9d7 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/henryiii
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
cd.yml@dfd96c48d4093540834a500b5b3245005699c9d7 -
Trigger Event:
release
-
Statement type: