Skip to main content

Utilities for building lazily imported Python packages

Project description

autolazy

Utilities for building lazily imported Python packages.

autolazy provides helper functions that make it easy to set up lazy_loader-based packages. It automatically discovers each submodule's public API by parsing __all__ declarations — no imports, no side effects, just AST analysis — and wires everything up in a single call.

Installation

pip install autolazy

Requires Python 3.12+.

Quick start

Suppose you have a package like this:

mypkg/
├── __init__.py
├── audio.py       # __all__ = ["AudioLoader", "AudioWriter"]
└── vision.py      # __all__ = ["ImageLoader"]

Replace the usual eager imports in __init__.py with:

from autolazy import lazy_attach

__getattr__, __dir__, __all__ = lazy_attach(
    package_name=__name__,
    init_file_path=__file__,
    submod_attrs=["audio", "vision"],
)

Now symbols are imported on first access:

import mypkg

mypkg.AudioLoader   # imports audio.py only at this point
mypkg.ImageLoader   # imports vision.py only at this point

Submodules themselves can also be exposed lazily:

__getattr__, __dir__, __all__ = lazy_attach(
    package_name=__name__,
    init_file_path=__file__,
    submodules=["utils"],       # exposed as mypkg.utils
    submod_attrs=["audio"],     # symbols exposed at mypkg level
)

API

lazy_attach

lazy_attach(
    package_name: str,
    init_file_path: str,
    submodules: list[str] = [],
    submod_attrs: list[str] = [],
) -> tuple[__getattr__, __dir__, __all__]

Scans each name in submod_attrs, extracts its __all__ via AST parsing, and passes the result to lazy_loader.attach().

Parameter Description
package_name The package name — pass __name__
init_file_path Path to __init__.py — pass __file__
submodules Submodules exposed as pkg.submod (not flattened)
submod_attrs Submodules whose public symbols are flattened into the package namespace

If a name in submod_attrs has no __all__ (or the file is missing), it falls back to being treated as a plain submodule and a UserWarning is emitted for missing files.

Dotted names (e.g. "sub.module") are resolved relative to the package directory, so nested packages work out of the box.


parse_all

parse_all(path: Path) -> list[str]

Parses a Python source file and returns the names declared in __all__, without importing the module. Supports all common patterns:

__all__ = ["a", "b"]       # assignment
__all__ += ["c"]           # augmented assignment
__all__.append("d")        # append
__all__.extend(["e", "f"]) # extend

Non-string elements in __all__ are silently ignored. If multiple assignments to __all__ exist, the last one wins (matching Python semantics).

How it compares to manual lazy_loader usage

Without autolazy you must maintain submod_attrs by hand:

# manual — must be kept in sync with each module's __all__
__getattr__, __dir__, __all__ = lazy.attach(
    __name__,
    submod_attrs={
        "audio": ["AudioLoader", "AudioWriter"],
        "vision": ["ImageLoader"],
    },
)

With autolazy:

# automatic — __all__ is read from each file at import time
__getattr__, __dir__, __all__ = lazy_attach(
    package_name=__name__,
    init_file_path=__file__,
    submod_attrs=["audio", "vision"],
)

Requirements

License

MIT — see LICENSE.

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

autolazy-1.0.2.tar.gz (5.4 kB view details)

Uploaded Source

Built Distribution

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

autolazy-1.0.2-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

Details for the file autolazy-1.0.2.tar.gz.

File metadata

  • Download URL: autolazy-1.0.2.tar.gz
  • Upload date:
  • Size: 5.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.23 {"installer":{"name":"uv","version":"0.11.23","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for autolazy-1.0.2.tar.gz
Algorithm Hash digest
SHA256 8f6b319339e91b83ef840ba053210309ab688eaea998c8ebe97f0b06c6e80761
MD5 b8f3be5aeb67018fc2cbead5f0d2199e
BLAKE2b-256 d04e31f68e713ca56fffd2e288689a94d168cce1e0b4ca2cd51f8b8953fca816

See more details on using hashes here.

File details

Details for the file autolazy-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: autolazy-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 6.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.23 {"installer":{"name":"uv","version":"0.11.23","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for autolazy-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 ade64ad31d99a45a9aac92f8b1bcab8f4ba0e0ffef11d19d2fc23cc063a600f5
MD5 a209e6d289ac4606e4a4d7907934082f
BLAKE2b-256 22058a28a217258789748f1ed2bfa3f2471d44a47ab8543557dec86502095359

See more details on using hashes here.

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