Skip to main content

Helper functions to make lazy imports easier in Python

Project description

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.

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

lazy_helper-0.1.0.tar.gz (3.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

lazy_helper-0.1.0-py3-none-any.whl (4.5 kB view details)

Uploaded Python 3

File details

Details for the file lazy_helper-0.1.0.tar.gz.

File metadata

  • Download URL: lazy_helper-0.1.0.tar.gz
  • Upload date:
  • Size: 3.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for lazy_helper-0.1.0.tar.gz
Algorithm Hash digest
SHA256 3ef32a1f70d0dcf830ba20e8e28457a9fa7211d114b702a8bcaa0d4c13b8d85b
MD5 67a9844191c43c2b0a811f371e20892c
BLAKE2b-256 89e3ab0f2c6ede8009d982a267e6378d1f2000fe8934438c4fb744642c60e795

See more details on using hashes here.

Provenance

The following attestation bundles were made for lazy_helper-0.1.0.tar.gz:

Publisher: python-publish.yml on Jamie-Chang/lazy-helper

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file lazy_helper-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: lazy_helper-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 4.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for lazy_helper-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 967dd2ca72af85b977e87df26f6f7ef7b52cd2b0db89764bcc1d06ca02fd657d
MD5 8cf097d2785904491f7cf391ca898f5c
BLAKE2b-256 34047ac41628a475ab7ab1a1a630b26782f0c41e60c2811c21132a1e836ae549

See more details on using hashes here.

Provenance

The following attestation bundles were made for lazy_helper-0.1.0-py3-none-any.whl:

Publisher: python-publish.yml on Jamie-Chang/lazy-helper

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page