Registry
hya.registry ¶
Implement the resolver registry to easily register resolvers.
hya.registry.ResolverRegistry ¶
Implement a resolver registry.
A registry maps keys to resolvers. register and
register_lazy add a resolver to the registry, and
register_resolvers publishes the registry to OmegaConf, so that
configurations can use the keys. A registry is independent of the
other registries.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
Mapping[str, Callable[..., Any]] | None
|
Optional initial state mapping containing key-resolver pairs,
or |
None
|
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If a key of |
Example
>>> from omegaconf import OmegaConf
>>> from hya.registry import ResolverRegistry
>>> registry = ResolverRegistry({"demo.upper": str.upper})
>>> @registry.register("demo.double")
... def double_resolver(value):
... return value * 2
...
>>> registry.register_resolvers()
>>> conf = OmegaConf.create({"x": "${demo.double:4}", "y": "${demo.upper:abc}"})
>>> conf.x, conf.y
(8, 'ABC')
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.
Example
>>> from hya.registry import ResolverRegistry
>>> registry = ResolverRegistry()
>>> @registry.register("my_key")
... def my_resolver(value):
... return value
...
>>> list(registry.state)
['my_key']
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: names made of ASCII
letters, digits and |
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 key is not a string (for example
|
ValueError
|
If the key is not made of names joined by dots. |
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, as for |
required |
target
|
str
|
The resolver to import, as |
required |
exist_ok
|
bool
|
If |
False
|
Raises:
| Type | Description |
|---|---|
TypeError
|
If the key or |
ValueError
|
If |
RuntimeError
|
If the key already exists and |
Note
The first call of the resolver raises a
ModuleNotFoundError if the module does not exist, and an
AttributeError if the module has no such function.
Through an interpolation, OmegaConf reports it as an
InterpolationResolutionError.
Note
A resolver registered lazily cannot receive the special
parameters of OmegaConf (_root_, _parent_ or
_node_): OmegaConf reads them from the signature, which
is not known before the import.
Example
>>> from hya.registry import ResolverRegistry
>>> registry = ResolverRegistry()
>>> registry.register_lazy("demo.sqrt", "math:sqrt")
>>> registry.has_resolver("demo.sqrt")
True
>>> registry.state["demo.sqrt"](16)
4.0
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: calling it twice changes nothing. A resolver removed
from OmegaConf (for example with OmegaConf.clear_resolvers())
is published again.
Without replace, a resolver already published by this registry,
and whose key OmegaConf still knows, is not registered again. A key
is skipped if OmegaConf already has a resolver for it that this
registry did not publish (registered by another source), or if the
registry changed since it was published. One warning lists the
skipped keys, after the other keys are published, so no key is left
out when warnings are errors. A resolver that another source
registers over a published key is not detected: it is kept without
a warning.
With replace=True, every key of the registry is published,
whatever OmegaConf holds.
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
|
Raises:
| Type | Description |
|---|---|
UserWarning
|
If a key is skipped and the warnings are turned into errors. The other keys are published first. |
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()
hya.default ¶
Define the public interface to interact with the default resolver registry.
hya.default.get_default_registry
cached
¶
get_default_registry() -> ResolverRegistry
Get or create the default global resolver registry.
The first call creates the registry, and the next calls return the
same instance. The registry initially contains the default resolvers
and can be extended with custom resolvers. It has the same keys on
every machine: the resolvers that need an optional package
(braceexpand, numpy or torch) are always registered. The
module of such a resolver is imported on its first call, not when
hya is imported, and the call raises an error that says how to
install the package if it is missing.
Importing hya publishes the default resolvers to OmegaConf. A
resolver added to the registry afterwards is published by the next
call to register_resolvers.
Returns:
| Type | Description |
|---|---|
ResolverRegistry
|
The default |
Notes
A modification of the returned registry affects all the future
calls to this function. If you need an isolated registry, create
a new ResolverRegistry instance.
Example
>>> from omegaconf import OmegaConf
>>> from hya import get_default_registry
>>> registry = get_default_registry()
>>> registry.has_resolver("hya.add")
True
>>> @registry.register("my_key")
... def my_resolver(value):
... return value * 2
...
>>> registry.register_resolvers()
>>> OmegaConf.create({"key": "${my_key:21}"}).key
42