liblaf.lazy_loader
¶
Stub-driven lazy exports for package modules.
Use attach_stub in a package __init__.py and
declare the exported imports in the adjacent .pyi file. The runtime loader
supports both package-relative imports and absolute imports while keeping the
public surface area small.
Classes:
-
Getter–Define how one exported name is resolved from a stub entry.
-
GetterContext–Carry module metadata needed to resolve a lazy export.
-
GetterImport–Resolve an
import ...statement from the stub file. -
GetterImportFrom–Resolve a
from ... import ...statement from the stub file. -
LazyLoader–Resolve stub-declared exports on first attribute access.
-
StubVisitor–Parse a stub AST into getters and optional export names.
Functions:
-
attach_stub–Create module hooks from a sibling stub file.
-
env_bool–Parse an environment variable as a boolean.
Attributes:
-
__commit_id__(str | None) – -
__version__(str) – -
__version_tuple__(tuple[int | str, ...]) –
__version_tuple__
module-attribute
¶
Getter
dataclass
¶
Bases: ABC
flowchart TD
liblaf.lazy_loader.Getter[Getter]
click liblaf.lazy_loader.Getter href "" "liblaf.lazy_loader.Getter"
Define how one exported name is resolved from a stub entry.
Methods:
-
get–Resolve the exported value for the given module context.
Attributes:
GetterContext
dataclass
¶
Carry module metadata needed to resolve a lazy export.
Parameters:
Attributes:
GetterImport
dataclass
¶
Bases: Getter
flowchart TD
liblaf.lazy_loader.GetterImport[GetterImport]
liblaf.lazy_loader._getter.Getter[Getter]
liblaf.lazy_loader._getter.Getter --> liblaf.lazy_loader.GetterImport
click liblaf.lazy_loader.GetterImport href "" "liblaf.lazy_loader.GetterImport"
click liblaf.lazy_loader._getter.Getter href "" "liblaf.lazy_loader._getter.Getter"
Resolve an import ... statement from the stub file.
An aliased import exposes the alias directly. Without an alias, the getter follows Python import semantics and exposes the top-level module name.
Parameters:
Methods:
-
get–Resolve the exported value for the given module context.
Attributes:
-
asname(str | None) – -
attr_name(str) –Return the attribute name exposed on the target module.
-
name(str) –Optional alias exposed on the lazily loaded module.
get
¶
get(ctx: GetterContext) -> Any
Resolve the exported value for the given module context.
Source code in src/liblaf/lazy_loader/_getter.py
GetterImportFrom
dataclass
¶
Bases: Getter
flowchart TD
liblaf.lazy_loader.GetterImportFrom[GetterImportFrom]
liblaf.lazy_loader._getter.Getter[Getter]
liblaf.lazy_loader._getter.Getter --> liblaf.lazy_loader.GetterImportFrom
click liblaf.lazy_loader.GetterImportFrom href "" "liblaf.lazy_loader.GetterImportFrom"
click liblaf.lazy_loader._getter.Getter href "" "liblaf.lazy_loader._getter.Getter"
Resolve a from ... import ... statement from the stub file.
This getter supports both absolute imports and relative imports. When a direct attribute lookup would recurse back into the lazily loaded module, it falls back to importing the submodule itself.
Parameters:
-
(module¶str | None) –Imported attribute or submodule name.
-
(name¶str) –Optional alias exposed on the lazily loaded module.
-
(asname¶str | None) –Relative import depth encoded by the stub AST node.
-
(level¶int) –
Methods:
-
get–Resolve the exported value for the given module context.
Attributes:
-
asname(str | None) –Relative import depth encoded by the stub AST node.
-
attr_name(str) –Return the attribute name exposed on the target module.
-
level(int) – -
module(str | None) –Imported attribute or submodule name.
-
name(str) –Optional alias exposed on the lazily loaded module.
get
¶
get(ctx: GetterContext) -> Any
Resolve the exported value for the given module context.
Source code in src/liblaf/lazy_loader/_getter.py
LazyLoader
dataclass
¶
LazyLoader(
name: str,
package: str | None,
exports: list[str] | None,
getters: dict[str, Getter],
)
Resolve stub-declared exports on first attribute access.
The loader backs the tuple returned by
attach_stub. It keeps the parsed getter
objects, exposes a module-friendly __dir__ and __all__, and caches each
resolved value onto the target module after the first lookup.
Parameters:
-
(name¶str) –Package anchor used for relative imports.
-
(package¶str | None) –Export names declared by
__all__in the stub, if present. -
(exports¶list[str] | None) –Mapping from exported attribute names to getter objects.
-
(getters¶dict[str, Getter]) –
Methods:
-
__dir__–List declared exports together with materialized module attributes.
-
__getattr__–Import and cache one declared export.
Attributes:
-
__all__(list[str]) –Return exported names for module
__all__. -
context(GetterContext) –Return import context shared by all getters.
-
exports(list[str] | None) –Mapping from exported attribute names to getter objects.
-
getters(dict[str, Getter]) – -
module(ModuleType) –Return the target module object from
sys.modules. -
name(str) –Package anchor used for relative imports.
-
package(str | None) –Export names declared by
__all__in the stub, if present.
exports
instance-attribute
¶
Mapping from exported attribute names to getter objects.
package
instance-attribute
¶
package: str | None
Export names declared by __all__ in the stub, if present.
__dir__
¶
__getattr__
¶
Import and cache one declared export.
Parameters:
Returns:
-
Any–The resolved module, function, class, or other imported object.
Raises:
-
AttributeError–If
nameis not declared by the stub. -
ImportError–If resolving the declared import fails.
Source code in src/liblaf/lazy_loader/_loader.py
StubVisitor
dataclass
¶
Bases: NodeVisitor
flowchart TD
liblaf.lazy_loader.StubVisitor[StubVisitor]
click liblaf.lazy_loader.StubVisitor href "" "liblaf.lazy_loader.StubVisitor"
Parse a stub AST into getters and optional export names.
Parameters:
-
(exports¶list[str] | None, default:None) –Mapping populated from explicit import statements in the stub.
-
(getters¶dict[str, Getter], default:<class 'dict'>) –dict() -> new empty dictionary dict(mapping) -> new dictionary initialized from a mapping object's (key, value) pairs dict(iterable) -> new dictionary initialized as if via: d = {} for k, v in iterable: d[k] = v dict(**kwargs) -> new dictionary initialized with the name=value pairs in the keyword argument list. For example: dict(one=1, two=2)
Methods:
-
finish–Build a loader from the imports collected so far.
-
visit_AnnAssign–Capture annotated
__all__assignments from the stub. -
visit_Assign–Capture
__all__ = [...]assignments from the stub. -
visit_Import–Convert
import ...statements into getter entries. -
visit_ImportFrom–Convert
from ... import ...statements into getter entries.
Attributes:
-
exports(list[str] | None) –Mapping populated from explicit import statements in the stub.
-
getters(dict[str, Getter]) –
exports
class-attribute
instance-attribute
¶
Mapping populated from explicit import statements in the stub.
getters
class-attribute
instance-attribute
¶
finish
¶
finish(name: str, package: str | None) -> LazyLoader
Build a loader from the imports collected so far.
Source code in src/liblaf/lazy_loader/_visitor.py
visit_AnnAssign
¶
visit_AnnAssign(node: AnnAssign) -> None
Capture annotated __all__ assignments from the stub.
Source code in src/liblaf/lazy_loader/_visitor.py
visit_Assign
¶
visit_Assign(node: Assign) -> None
Capture __all__ = [...] assignments from the stub.
Source code in src/liblaf/lazy_loader/_visitor.py
visit_Import
¶
visit_Import(node: Import) -> None
Convert import ... statements into getter entries.
Source code in src/liblaf/lazy_loader/_visitor.py
visit_ImportFrom
¶
visit_ImportFrom(node: ImportFrom) -> None
Convert from ... import ... statements into getter entries.
Raises:
-
ValueError–If the stub contains a wildcard import.
Source code in src/liblaf/lazy_loader/_visitor.py
attach_stub
¶
Create module hooks from a sibling stub file.
Call this from a package __init__.py and assign the returned values to
__getattr__, __dir__, and __all__. The function reads the adjacent
.pyi file, parses its explicit import statements into a LazyLoader, and optionally resolves every export at
import time when EAGER_IMPORT is enabled.
Parameters:
-
(name¶str) –Module name, typically
__name__. -
(file¶str) –Path to the module file whose sibling
.pyifile declares the lazy exports, typically__file__. -
(package¶str | None | MissingType, default:MISSING) –Optional package name used to resolve relative imports, typically
__package__. When the argument is omitted entirely,nameis reused soattach_stub(__name__, __file__)works as a drop-in call for package__init__.pymodules. PassingNoneexplicitly preservesNone.
Returns:
-
GetAttr–A tuple containing
__getattr__,__dir__, and__all__in that -
Dir–order.
Raises:
-
FileNotFoundError–If the sibling
.pyifile does not exist. -
SyntaxError–If the sibling stub cannot be parsed as Python.
-
ValueError–If
EAGER_IMPORTis not a valid boolean string or the stub uses an unsupported construct such as a wildcard import. -
ImportError–If eager loading is enabled and one of the declared imports cannot be resolved.
Source code in src/liblaf/lazy_loader/_attach_stub.py
env_bool
¶
Parse an environment variable as a boolean.
Parameters:
-
(name¶str) –Environment variable name to read.
-
(default¶bool, default:False) –Value to return when the variable is unset.
Returns:
-
bool–The parsed boolean value.
Raises:
-
ValueError–If the environment variable is set but does not match one of the accepted truthy or falsy strings.