windlass.core.lazy¶
lazy
¶
Optional dependency loading.
Windlass ships a tiny core and pushes every integration behind an extras group. This module is the single place where those optional imports happen, so that:
import windlassnever pulls in torch, chromadb or langgraph;- a missing package produces an actionable :class:
MissingDependencyErrorinstead of a rawImportError; - modules are imported at most once and cached.
Usage inside an adapter::
from windlass.core.lazy import require
openai = require("openai", extra="openai", feature="The OpenAI provider")
client = openai.AsyncOpenAI()
Nothing here is exported publicly; adapters are the only callers.
LazyModule
¶
A module proxy that defers the import until first attribute access.
Lets a module hold a reference to an optional dependency at import time without paying the import cost or failing when it is absent.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
module
|
str
|
Dotted module path. |
required |
extra
|
str
|
Extras group providing it. |
required |
feature
|
str
|
Human readable feature name. |
required |
Example
np = LazyModule("json", extra="core", feature="JSON") np.dumps([1]) # import happens here '[1]'
Source code in src\windlass\core\lazy.py
is_available
¶
Return whether module can be imported without importing it.
Uses :func:importlib.util.find_spec, so the module's top-level code does
not execute. Safe to call on hot paths and at registration time.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
module
|
str
|
Dotted module path, e.g. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True when the module is installed and importable. |
Example
is_available("json") True is_available("definitely_not_installed_xyz") False
Source code in src\windlass\core\lazy.py
require
¶
Import an optional module or raise a helpful error.
Thread-safe and memoised: concurrent first-time callers import exactly once.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
module
|
str
|
Dotted module path to import. |
required |
extra
|
str
|
The |
required |
feature
|
str
|
Human readable feature name for the error message. |
required |
Returns:
| Type | Description |
|---|---|
ModuleType
|
The imported module. |
Raises:
| Type | Description |
|---|---|
MissingDependencyError
|
When the module is not installed. The message contains the exact install command. |
Example
require("json", extra="core", feature="JSON") # doctest: +ELLIPSIS
Source code in src\windlass\core\lazy.py
require_attr
¶
Import an optional module and pull one attribute off it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
module
|
str
|
Dotted module path. |
required |
attr
|
str
|
Attribute (usually a class) to retrieve. |
required |
extra
|
str
|
Extras group providing the module. |
required |
feature
|
str
|
Human readable feature name. |
required |
Returns:
| Type | Description |
|---|---|
Any
|
The requested attribute. |
Raises:
| Type | Description |
|---|---|
MissingDependencyError
|
When the module is absent, or present but does
not expose |
Source code in src\windlass\core\lazy.py
lazy_import
¶
Return a zero-arg callable that imports path on demand.
Used by the registry to describe a provider without importing its module.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Either |
required |
Returns:
| Type | Description |
|---|---|
Callable[[], Any]
|
A callable resolving to the module or attribute. |
Example
loader = lazy_import("json:dumps") loader()([1]) '[1]'