Skip to content

lazy

polars_ts._lazy

Shared lazy-import helper for polars-ts modules.

Provides a generic make_getattr factory that turns a flat registry dict into a module-level __getattr__ function. This eliminates the duplicated if-chain boilerplate across submodule __init__.py files.

Usage in a submodule __init__.py::

from polars_ts._lazy import make_getattr

_IMPORTS: dict[str, tuple[str, str]] = {
    "KShape": ("polars_ts.clustering.kshape", "KShape"),
    "kmedoids": ("polars_ts.clustering.kmedoids", "kmedoids"),
}

__getattr__, __all__ = make_getattr(_IMPORTS, __name__)

For names that need custom logic (e.g. wrapping ImportError)::

def _load_scum():
    try:
        from polars_ts.models.scum import SCUM
    except ImportError:
        raise ImportError("statsforecast is required for SCUM.") from None
    return SCUM

__getattr__, __all__ = make_getattr(_IMPORTS, __name__, overrides={"SCUM": _load_scum})

make_getattr(registry, module_name, *, overrides=None)

Create __getattr__ and __all__ from a lazy-import registry.

Parameters

registry Mapping from public name to (module_path, attribute_name). module_name The __name__ of the calling module (for error messages). overrides Per-name callables that replace the default import logic. Each value must be a zero-argument callable returning the object. Use this for names that need custom ImportError handling.

Returns

tuple (__getattr__, __all__) ready to be assigned at module level.