Skip to content

Main functions

The package, the resolver registry and the default registry. See Custom Resolvers to learn how to use them.

hya

Contain the main features of the hya package.

Importing hya publishes the default resolvers to OmegaConf, so a configuration can use them without any other call. The package exposes ResolverRegistry and get_default_registry to add your own resolvers.

Example
>>> import hya
>>> from omegaconf import OmegaConf
>>> conf = OmegaConf.create({"steps": "${hya.ceildiv:1000,32}"})
>>> conf.steps
32

hya.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 for an empty registry. A copy is made to prevent external modifications. Each pair is added with register, so it is validated.

None

Raises:

Type Description
TypeError

If state is not a mapping or None, if a key of state is not a string, or if a value is not callable.

ValueError

If a key of state is not made of names joined by dots (see register).

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.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.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.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 _ that do not start with a digit, joined by dots (for example "my.resolver_2"). Every supported OmegaConf can interpolate such a key. 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 key is not a string (for example @registry.register used without a key), or if the resolver is not callable.

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.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 register. Must be unique unless exist_ok is True.

required
target str

The resolver to import, as "module:function", for example "hya.numpy:to_array_resolver". The module is an absolute module name: ASCII names joined by dots, as a key. The function is the name of an attribute of the module (a Python identifier). That they exist is checked on the first call.

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

Raises:

Type Description
TypeError

If the key or target is not a string.

ValueError

If target is not formatted as "module:function", or if the key is not made of names joined by dots.

RuntimeError

If the key already exists and exist_ok is False.

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.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 True, the resolvers replace the resolvers that OmegaConf already has for the same keys. If False, these keys are skipped, with a warning for the ones that this registry did not publish.

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.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 ResolverRegistry of the process.

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