Properties¶
These are the rules hya guarantees. The Design Choices page explains the reasons and
the trade-offs, and the limits list what hya does not guarantee. Each rule has a
number, so that other pages can refer to it.
| Group | Properties |
|---|---|
| Import and registry | P1 Import is enough, P2 Cheap import, P3 Namespaced, P7 Explicit, strict registry, P8 Replace is explicit, P9 One default registry |
| Behaviour of the resolvers | P4 Thin resolvers, P5 Pure, P6 Deterministic output, P16 Resolved input, P17 Faithful hash, P15 Composable |
| Values and errors | P10 Errors are OmegaConf's, P11 Any value out |
| Python API and compatibility | P13 Keyword-only options, P12 Typed and documented, P14 Installs next to Hydra |
Import and registry¶
P1: Import is enough¶
import hya registers every available default resolver with OmegaConf. No other call is needed, and
register_resolvers() publishes them again after OmegaConf.clear_resolvers().
P2: Cheap import¶
import hya imports neither numpy, torch nor braceexpand, and emits no warning when no other
source has registered a hya. key. The module of an optional resolver is imported on the first call
of that resolver.
The optional resolvers are registered with ResolverRegistry.register_lazy, and import hya does
not look for their packages on disk.
P3: Namespaced¶
Every built-in key starts with hya.. Optional ones are grouped (hya.np.*, hya.torch.*).
Nothing is registered outside this namespace.
P7: Explicit, strict registry¶
A ResolverRegistry is a key to callable map. register adds, register_resolvers publishes. The
constructor takes a mapping or None. Every entry has a callable value and a key that every
supported OmegaConf can interpolate, and every lazy target is module:function made of names;
anything else raises at registration.
A key is made of names joined by dots, where a name is made of ASCII letters, digits and _ and
does not start with a digit. OmegaConf 2.3 and later can interpolate a - too, but OmegaConf 2.2
cannot, so hya rejects it (P14). The module of a lazy target follows the rule of the keys (so it
cannot be relative, as .numpy) and its function is a Python identifier. That they exist is checked
on the first call. A registry is independent of the other registries.
P8: Replace is explicit¶
register_resolvers() is idempotent. A key that changed since it was published, or that another
source registered before the registry published it, is skipped unless replace=True; one warning
lists the skipped keys, after the other keys are published. With replace=True, every key of the
registry is published, whatever OmegaConf holds.
After import hya, the default resolvers are already published, so registry.register_resolvers()
is silent. Overriding a built-in (registry.register("hya.add", exist_ok=True)) warns until
register_resolvers(replace=True) is called. OmegaConf caches the resolved nodes of a
configuration: replace=True does not clear the cache of the existing configurations
(OmegaConf.clear_cache(cfg)).
P9: One default registry¶
get_default_registry() returns one process-wide registry holding the built-in resolvers. It holds
the same keys on every machine, whatever packages are installed. Users extend it or create their own
registry.
Behaviour of the resolvers¶
P4: Thin resolvers¶
A resolver is a standard library callable or a function of a few lines with one documented
behaviour. No hidden state, no I/O beyond what its name says (hya.path touches the file system). A
resolver written by hya raises on an input it cannot honour instead of guessing or dropping a part
of it.
P5: Pure¶
A resolver never modifies its arguments. A resolver written by hya returns a new list, dictionary
or array and never a container of an argument; hya.torch.tensor returns a copy (hya.add of a
single other object returns it, and so does hya.mul, which takes numbers, of its single argument).
A standard library callable can return an argument or one of its elements (hya.max of several
values), as ${x} returns the node x: copy it before you mutate it.
P6: Deterministic output¶
The same resolved arguments give the same output, including error messages (sorted, no set order),
on every process and OmegaConf release. The type of the result of a resolver written by hya does
not depend on how an argument is written (a literal or a reference), as long as it takes the
argument (hya.mul takes numbers). hya.path also depends on the file system (working directory,
home directory, symbolic links), which is its purpose.
P16: Resolved input¶
A resolver written by hya that reads a list or a mapping (hya.add, hya.sha256,
hya.iter_join, hya.np.array, hya.torch.tensor) reads its resolved content: interpolations
are resolved at any depth, and a missing value (???) raises MissingMandatoryValue.
P17: Faithful hash¶
hya.sha256 hashes the whole content of a value: a string as is, any other value as canonical JSON
(sorted keys, no space, non-ASCII escaped). Two such values with different JSON forms have different
hashes, and a value without a JSON form (a non-finite float included) raises.
P15: Composable¶
A resolver written by hya accepts the values OmegaConf gives it (str, int, float, bool,
None, list, dict, ListConfig, DictConfig), from a writable or a read-only configuration,
and the output of another hya resolver of a type it takes (a Path for hya.path, a
torch.dtype for hya.torch.tensor), without a warning. hya.np.array and hya.torch.tensor take
a tensor (on the CPU, without gradient for NumPy) and an array, and hya.torch.tensor takes a data
type as a torch.dtype or as its name.
Values and errors¶
P10: Errors are OmegaConf's¶
A resolver raises standard exceptions only, so a failure surfaces as InterpolationResolutionError
whose message names the original exception, found in the __context__ chain (__cause__ is
None). A missing optional package says how to install it.
A missing configuration value (???) is reported by the exceptions of OmegaConf
(MissingMandatoryValue, which the message names). hya adds no exception class of its own and no
wrapper. With OmegaConf 2.4 and later, the annotations of the resolvers published by hya are not
validated at call time.
P11: Any value out¶
A resolver may return any Python object (Path, ndarray, Tensor, dtype, list). Such values
are for use in code: for an array, a tensor or a data type, OmegaConf.resolve needs the
allow_objects flag and to_yaml does not support them.
Use such a value in code (cfg.key, or OmegaConf.to_container(cfg, resolve=True)). To resolve a
whole configuration in place, give it the allow_objects flag, when you create it
(OmegaConf.create(..., flags={"allow_objects": True})) or around the call
(with flag_override(cfg, "allow_objects", True): OmegaConf.resolve(cfg), with
from omegaconf import flag_override).
Python API and compatibility¶
P13: Keyword-only options¶
The options of the Python API are keyword-only (exist_ok, replace). The arguments of a resolver
are positional, because the interpolation grammar has no keyword syntax; an optional argument of a
resolver comes last and has a default.
P12: Typed and documented¶
The package has a py.typed file and its public types are complete (pyright --verifytypes scores
100 %). Every public function, class, method and property has a docstring with an example and is
shown in the reference, and every default key is listed in the resolvers page.
P14: Installs next to Hydra¶
hya accepts every OmegaConf release that a Hydra release accepts (omegaconf>=2.2,<3.0) and
behaves the same on all of them. CI runs the tests on the oldest and the newest.
Limits¶
hya does not provide the following.
- Thread-safety of registering resolvers while OmegaConf resolves interpolations. The resolver table of OmegaConf is a global dictionary.
- Detection of a conflicting registration.
register_resolvers()withoutreplace=Truekeeps silently a resolver that another source registered over a published key: detecting it needs the privateBaseContainer._resolversof OmegaConf, which changes between releases.register_resolvers(replace=True)publishes the key again. - Type-tagged hashes.
hya.sha256of the string[1]equals the hash of the list[1], an integer key equals the same string key, a path or an enum equals its string in JSON, and an enum that is also anint, afloator astrequals its value. A mapping that mixes key types cannot be sorted and raises. - Special parameters of lazy resolvers. A resolver registered with
register_lazycannot receive_root_,_parent_or_node_: OmegaConf reads them from the signature, which is not known before the import. - Warnings of the libraries for inputs they discourage, for example
torch.tensorof a list of NumPy arrays. - Caching of the output of the resolvers. OmegaConf does not cache it: every access of an interpolation calls the resolver again.
- Export of objects.
OmegaConf.to_yaml(resolve=True)does not support an array, a tensor or a data type, andOmegaConf.resolveneeds theallow_objectsflag for them (P11).