lazy-helper
Helper functions to make lazy imports easier in Python
Motivation
Lazy imports are useful for:
- Improving startup speed.
- Simplifying packages with optional dependencies.
- Typing imports that don't need to be loaded at run time.
There are a few mechanisms in Python for lazy imports right now. All of them have some compromise.
There is currently a proposal in the works but it'll take a while to come to Python if at all. (It probably will! Maybe in 3.15)
This package provides an opinionated approach to lazy loading, focusing on ease of use and minimising the biggest downside of no typing.
Installation
uv add lazy-helper
The cli is helpful for typing but can be kept as a dev dependency:
uv add --dev 'lazy-helper[cli]'
Usage
Defining the imports
First create a module to group all your lazy imports. I like to call mine lazy.py but it's really up to you.
In your module, first create a lazy loader object. As a convention you're expected to assign it to __lazy__:
from lazy_helper import Lazy
__lazy__ = Lazy()
Now define your lazy imports:
__lazy__("typing") # same as `import typing`
__lazy__("typing", "TypedDict") # same as `from typing import TypedDict`
__lazy__("typing", as_="tp") # `import typing as tp`
__lazy__("typing", "TypedDict", as_="TD") # `from typing import TypedDict as TD`
Finally expose lazy imports at the module level:
__getattr__ = __lazy__.__getattr__
__dir__ = __lazy__.__dir__
__all__ = __lazy__.__all__
Using the imports
Now if when you need to use any of the imports, you must import the module but not the attributes in the module.
from . import lazy
from .lazy import TD # Wrong ❌, this will eagerly import TD.
Now in attributes of lazy will only be loaded when we use it:
lazy.TD # will load the typing dynamically
Generating the stubs
Remember ot install the cli as a dev dependency:
uv add --dev 'lazy-helper[cli]'
Then run lazy-stubgen passing in any of the files that contain the __lazy__ objects.
lazy-stubgen examples/lazy.py
You will see a lazy.pyi file, which contains what the imports would look like.
import typing
from typing import TypedDict
import typing as tp
from typing import TypedDict as TD
__all__ = [
"typing",
"TypedDict",
"tp",
"TD",
]
The stubs will also allow type checkers like mypy and pyright to maintain typing information.
Metadata
Release files for lazy-helper 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| lazy_helper-0.1.0.tar.gz | 3.2 kB | With provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| lazy_helper-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 7.7 kB
Release files / lazy_helper-0.1.0.tar.gz
| Download URL | lazy_helper-0.1.0.tar.gz |
|---|---|
| Size | 3.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
3ef32a1f70d0dcf830ba20e8e28457a9fa7211d114b702a8bcaa0d4c13b8d85b
|
|
BLAKE2b-256 checksum How to use checksums |
89e3ab0f2c6ede8009d982a267e6378d1f2000fe8934438c4fb744642c60e795
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 27, 2025.
Transparency logRelease files / lazy_helper-0.1.0-py3-none-any.whl
| Download URL | lazy_helper-0.1.0-py3-none-any.whl |
|---|---|
| Size | 4.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
967dd2ca72af85b977e87df26f6f7ef7b52cd2b0db89764bcc1d06ca02fd657d
|
|
BLAKE2b-256 checksum How to use checksums |
34047ac41628a475ab7ab1a1a630b26782f0c41e60c2811c21132a1e836ae549
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 27, 2025.
Transparency logSecurity for lazy-helper 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
All 2 files in this release have been uploaded with provenance metadata.
Attestation for lazy_helper-0.1.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 27, 2025.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.