Registry
hya.registry ¶
Implement the resolver registry to easily register resolvers.
hya.registry.ResolverRegistry ¶
Implement a resolver registry.
This class manages a collection of resolver functions that can be registered with keys and later used with OmegaConf.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
Mapping[str, Callable[..., Any]] | None
|
Optional initial state mapping containing key-resolver pairs. If provided, a copy is made to prevent external modifications. |
None
|
Example
>>> from hya.registry import ResolverRegistry
>>> registry = ResolverRegistry()
>>> @registry.register("my_key")
... def my_resolver(value):
... pass
...
hya.registry.ResolverRegistry.state
property
¶
state: Mapping[str, Callable[..., Any]]
The state of the registry.
This is a read-only view: use register to add a resolver.
hya.registry.ResolverRegistry.has_resolver ¶
has_resolver(key: str) -> bool
Check if a resolver is explicitly registered for the given key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The key to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if a resolver is registered for this key, |
bool
|
False otherwise |
Example
>>> from hya.registry import ResolverRegistry
>>> registry = ResolverRegistry()
>>> @registry.register("my_key")
... def my_resolver(value):
... pass
...
>>> registry.has_resolver("my_key")
True
>>> registry.has_resolver("missing")
False
hya.registry.ResolverRegistry.register ¶
register(
key: str, *, exist_ok: bool = False
) -> Callable[[F], F]
Register a resolver to registry with the specified key.
This method returns a decorator that can be used to register resolver functions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The key used to register the resolver. Must be unique unless exist_ok is True. |
required |
exist_ok
|
bool
|
If False, a RuntimeError is raised if you try to register a new resolver with an existing key. If True, the existing resolver will be overridden. |
False
|
Returns:
| Type | Description |
|---|---|
Callable[[F], F]
|
A decorator function that registers the resolver and returns it unchanged. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If the resolver is not callable. |
RuntimeError
|
If the key already exists and exist_ok is False. |
Example
>>> from hya.registry import ResolverRegistry
>>> registry = ResolverRegistry()
>>> @registry.register("my_key")
... def my_resolver(value):
... return value * 2
...
>>> my_resolver(5)
10
hya.registry.ResolverRegistry.register_lazy ¶
register_lazy(
key: str, target: str, *, exist_ok: bool = False
) -> None
Register a resolver that is imported on its first call.
The module of the resolver is not imported when the resolver is registered, but the first time OmegaConf calls the resolver. It is useful for the resolvers that need a heavy package, because the package is not imported if the resolver is never used. The registered callable is named after the function of the target and its docstring gives the target, so that tracebacks and the state of the registry say what it is.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The key used to register the resolver. Must be unique unless
|
required |
target
|
str
|
The resolver to import, as |
required |
exist_ok
|
bool
|
If |
False
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
RuntimeError
|
If the key already exists and |
Example
>>> from hya.registry import ResolverRegistry
>>> registry = ResolverRegistry()
>>> registry.register_lazy("hya.np.array", "hya.numpy:to_array_resolver")
>>> registry.has_resolver("hya.np.array")
True
hya.registry.ResolverRegistry.register_resolvers ¶
register_resolvers(*, replace: bool = False) -> None
Register the resolvers to OmegaConf.
This method publishes the registered resolvers to OmegaConf. It is
idempotent: a resolver already published by this registry, and still
known by OmegaConf, is not registered again, so calling this method
twice changes nothing. A resolver removed from OmegaConf (for example
with OmegaConf.clear_resolvers()) is published again. A
resolver is not published, and a warning is emitted, if OmegaConf
already has a resolver for its key (a resolver published before the
registry changed, or registered by another source) and replace is
False.
The resolvers are published with annotation_validation="off" when
OmegaConf validates the annotations (2.4 and later): the annotations of
the resolvers are not a runtime contract.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
replace
|
bool
|
If |
False
|
Notes
OmegaConf caches the resolved value of the nodes of a
configuration. Replacing a resolver does not clear the cache of
the existing configurations: use OmegaConf.clear_cache(cfg)
to clear it.
Example
>>> from hya.registry import ResolverRegistry
>>> registry = ResolverRegistry()
>>> @registry.register("multiply")
... def multiply_resolver(x, y):
... return x * y
...
>>> registry.register_resolvers()