Utilities¶
Helpers for custom resolvers and their tests.
Configuration values¶
hya.utils.config ¶
Implement helpers to read the values of an OmegaConf configuration.
hya.utils.config.to_plain ¶
to_plain(value: Any) -> Any
Return value with its configuration nodes converted to plain,
resolved objects.
OmegaConf gives a resolver the configuration node itself
(ListConfig or DictConfig), and a literal such as [${x}]
is a plain list that contains nodes. A resolver that reads a whole
value as text or data must resolve the interpolations of these
nodes, at any depth. Lists and dictionaries are searched for nodes
and are returned as new containers, so value is not modified.
Any other object (including a tuple) is returned as is.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
The value to convert. |
required |
Returns:
| Type | Description |
|---|---|
Any
|
The value without configuration nodes. |
Raises:
| Type | Description |
|---|---|
MissingMandatoryValue
|
If a node contains a
missing value ( |
InterpolationResolutionError
|
If an interpolation of a node cannot be resolved, for example a reference to a key that does not exist. |
Example
>>> from omegaconf import OmegaConf
>>> from hya.utils.config import to_plain
>>> conf = OmegaConf.create({"a": 1, "x": ["${a}", 2]})
>>> to_plain(conf.x)
[1, 2]
>>> to_plain([conf.x, {"y": conf.x}])
[[1, 2], {'y': [1, 2]}]
Optional dependencies¶
hya.imports ¶
Implement some utility functions to manage optional dependencies.
For each optional package (braceexpand, numpy and torch),
is_<package>_available indicates whether the package is installed,
without importing it, and validate_<package> raises a
RuntimeError that says how to install it when it is missing.
hya.imports.is_braceexpand_available ¶
is_braceexpand_available() -> bool
Indicate if the braceexpand package is installed or not.
Returns:
| Type | Description |
|---|---|
bool
|
|
Example
>>> from hya.imports import is_braceexpand_available
>>> is_braceexpand_available()
hya.imports.is_numpy_available ¶
is_numpy_available() -> bool
Indicate if the numpy package is installed or not.
Returns:
| Type | Description |
|---|---|
bool
|
|
Example
>>> from hya.imports import is_numpy_available
>>> is_numpy_available()
hya.imports.is_torch_available ¶
is_torch_available() -> bool
Indicate if the torch package is installed or not.
Returns:
| Type | Description |
|---|---|
bool
|
|
Example
>>> from hya.imports import is_torch_available
>>> is_torch_available()
hya.imports.validate_braceexpand ¶
validate_braceexpand() -> None
Check if the braceexpand package is installed.
Raises:
| Type | Description |
|---|---|
RuntimeError
|
if the |
Example
>>> from hya.imports import validate_braceexpand
>>> validate_braceexpand()
hya.imports.validate_numpy ¶
validate_numpy() -> None
Check if the numpy package is installed.
Raises:
| Type | Description |
|---|---|
RuntimeError
|
if the |
Example
>>> from hya.imports import validate_numpy
>>> validate_numpy()
hya.imports.validate_torch ¶
validate_torch() -> None
Check if the torch package is installed.
Raises:
| Type | Description |
|---|---|
RuntimeError
|
if the |
Example
>>> from hya.imports import validate_torch
>>> validate_torch()
Testing¶
hya.testing ¶
Define PyTest markers for testing optional dependencies.
This module provides pytest markers that help with testing the hya library under different dependency configurations. The markers allow tests to be conditionally skipped based on whether optional packages (braceexpand, numpy, torch) are available in the test environment.
These markers are useful for ensuring tests only run when the required dependencies are present, avoiding import errors and test failures when optional packages are not installed.
For each optional package, <package>_available skips a test when the
package is not installed, and <package>_not_available skips a test
when it is installed: braceexpand_available,
braceexpand_not_available, numpy_available,
numpy_not_available, torch_available and
torch_not_available.
This module imports pytest, which the testing extra installs
(pip install 'hya[testing]').
Example
>>> from hya.testing import numpy_available
>>> @numpy_available
... def test_with_numpy():
... pass
...