hydra_zen.wrapper.Zen#

class hydra_zen.wrapper.Zen(_Zen__func, *, exclude=None, pre_call=None, unpack_kwargs=False, resolve_pre_call=True, run_in_context=False, instantiation_wrapper=None)[source]#

Implements the wrapping logic that is exposed by hydra_zen.zen

Attributes:
funcCallable[Sig, R]

The function that was wrapped.

CFG_NAMEstr

The reserved parameter name specifies to pass the input config through to the inner function. Can be overwritted via subclassing. Defaults to ‘zen_cfg’

Methods

__call__(_Zen__cfg)

Extracts values from the input config based on the decorated function's signature, resolves & instantiates them, and calls the function with them.

hydra_main([config_path, config_name, ...])

Generates a Hydra-CLI for the wrapped function.

instantiate(_Zen__c)

Instantiates each config that is extracted by zen before calling the wrapped function.

validate(_Zen__cfg)

Validates the input config based on the decorated function without calling said function.

See also

zen

A decorator that returns a function that will auto-extract, resolve, and instantiate fields from an input config based on the decorated function’s signature.

__init__(_Zen__func, *, exclude=None, pre_call=None, unpack_kwargs=False, resolve_pre_call=True, run_in_context=False, instantiation_wrapper=None)[source]#
Parameters:
funcCallable[Sig, R], positional-only

The function being wrapped.

unpack_kwargs: bool, optional (default=False)

If True a **kwargs field in the wrapped function’s signature will be populated by all of the input config entries that are not specified by the rest of the signature (and that are not specified by the exclude argument).

pre_callOptional[Callable[[Any], Any] | Iterable[Callable[[Any], Any]]]

One or more functions that will be called with the input config prior to the wrapped function. An iterable of pre-call functions are called from left (low-index) to right (high-index).

This is useful, e.g., for seeding a RNG prior to the instantiation phase that is triggered when calling the wrapped function.

resolve_pre_callbool, (default=True)

If True, the config passed to the zen-wrapped function has its interpolated fields resolved prior to being passed to any pre-call functions. Otherwise, the interpolation occurs after the pre-call functions are called.

excludeOptional[str | Iterable[str]]

Specifies one or more parameter names in the function’s signature that will not be extracted from input configs by the zen-wrapped function.

A single string of comma-separated names can be specified.

run_in_contextbool, optional (default=False)

If True, the zen-wrapped function - and the pre_call function, if specified - is run in a copied contextvars.Context; i.e. changes made to any contextvars.ContextVar will be isolated to that call of the wrapped function.

run_in_context is not supported for async functions.

instantiation_wrapperOptional[Callable[[F2], F2]], optional (default=None)

If specified, a function that wraps the task function and all instantiation-targets before they are called.

This can be used to introduce a layer of validation or logging to all instantiation calls in your application.

__call__(_Zen__cfg)[source]#

Extracts values from the input config based on the decorated function’s signature, resolves & instantiates them, and calls the function with them.

Parameters:
cfgdict | DataClass | Type[DataClass] | str

(positional only) A config object or yaml-string whose attributes will be extracted by-name according to the signature of func and passed to func.

Attributes of types that can be instantiated by Hydra will be instantiated prior to being passed to func.

Returns:
func_outR

The result of func(<args extracted from cfg>)

validate(_Zen__cfg)[source]#

Validates the input config based on the decorated function without calling said function.

Parameters:
cfgdict | list | DataClass | Type[DataClass] | str

(positional only) A config object or yaml-string whose attributes will be checked according to the signature of func.

Raises:
HydraValidationError

cfg is not a valid input to the zen-wrapped function.

hydra_main(config_path=<object object>, config_name=None, version_base=<object object>)[source]#

Generates a Hydra-CLI for the wrapped function. Equivalent to hydra.main(zen(func), [...])()

Parameters:
config_pathOptional[str]

The config path, an absolute path to a directory or a directory relative to the declaring python file. If config_path is not specified no directory is added to the config search path.

Specifying config_path via Zen.hydra_main is only supported for Hydra 1.3.0+.

config_nameOptional[str]

The name of the config (usually the file name without the .yaml extension)

version_baseOptional[str]

There are three classes of values that the version_base parameter supports, given new and existing users greater control of the default behaviors to use.

  • If the version_base parameter is not specified, Hydra 1.x will use defaults compatible with version 1.1. Also in this case, a warning is issued to indicate an explicit version_base is preferred.

  • If the version_base parameter is None, then the defaults are chosen for the current minor Hydra version. For example for Hydra 1.2, then would imply config_path=None and hydra.job.chdir=False.

  • If the version_base parameter is an explicit version string like “1.1”, then the defaults appropriate to that version are used.

Returns:
hydra_mainCallable[[Any], Any]

Equivalent to hydra.main(zen(func), [...])()

Methods

__init__(_Zen__func, *[, exclude, pre_call, ...])

__call__(_Zen__cfg)

Extracts values from the input config based on the decorated function's signature, resolves & instantiates them, and calls the function with them.

validate(_Zen__cfg)

Validates the input config based on the decorated function without calling said function.

hydra_main([config_path, config_name, ...])

Generates a Hydra-CLI for the wrapped function.