Skip to content

Main functions

hya

Contain the main features of the hya package.

hya.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.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.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. 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.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 exist_ok is True.

required
target str

The resolver to import, as "module:function", for example "hya.numpy:to_array_resolver".

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
ValueError

If target is not formatted as "module:function".

RuntimeError

If the key already exists and exist_ok is False.

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

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()

hya.get_default_registry cached

get_default_registry() -> ResolverRegistry

Get or create the default global resolver registry.

Returns a singleton registry instance using a simple singleton pattern. The registry initially contains the default resolvers and can be extended with custom resolvers. The registry 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.

This function uses a singleton pattern to ensure the same registry instance is returned on subsequent calls, which is efficient and maintains consistency across an application.

Returns:

Type Description
ResolverRegistry

A ResolverRegistry instance

Notes

The singleton pattern means modifications to the returned registry affect all future calls to this function. If you need an isolated registry, create a new ResolverRegistry instance directly.

Example
>>> from hya import get_default_registry
>>> registry = get_default_registry()
>>> @registry.register("my_key")
... def my_resolver(value):
...     pass
...