hydra_zen.make_custom_builds_fn#
- hydra_zen.make_custom_builds_fn(*, zen_partial=None, populate_full_signature=False, zen_exclude=frozenset({}), zen_wrappers=(), zen_meta=None, hydra_recursive=None, hydra_convert=None, zen_dataclass=None, frozen=False, zen_convert=None, builds_fn=<class 'hydra_zen.structured_configs._implementations.DefaultBuilds'>)[source]#
Returns the
buildsfunction, but with customized default values.E.g.
make_custom_builds_fn(hydra_convert='all')will return a version of thebuildsfunction where the default value forhydra_convertis'all'instead ofNone.- Parameters:
- zen_partialbool, optional (default=False)
Specifies a new the default value for
builds(..., zen_partial=<..>)- zen_wrappersNone | Callable | Builds | InterpStr | Sequence[None | Callable | Builds | InterpStr]
Specifies a new the default value for
builds(..., zen_wrappers=<..>)- zen_metaOptional[Mapping[str, Any]]
Specifies a new the default value for
builds(..., zen_meta=<..>)- populate_full_signaturebool, optional (default=False)
Specifies a new the default value for
builds(..., populate_full_signature=<..>)- zen_excludeCollection[str] | Callable[[str], bool], optional (default=[])
Specifies parameter names, or a function for checking names, to exclude those parameters from the config-creation process.
Note that inherited fields cannot be excluded.
- zen_convertOptional[ZenConvert]
A dictionary that modifies hydra-zen’s value and type conversion behavior. Consists of the following optional key-value pairs (hydra_zen.typing.ZenConvert):
- zen_dataclassOptional[DataclassOptions]
A dictionary can specify any option that is supported by
dataclasses.make_dataclass()other thanfields. The default value forunsafe_hashisTrue.Additionally, the
modulefield can be specified to enable pickle compatibility. Seehydra_zen.typing.DataclassOptionsfor details.- hydra_recursiveOptional[bool], optional (default=True)
Specifies a new the default value for
builds(..., hydra_recursive=<..>)- hydra_convertOptional[Literal[“none”, “partial”, “all”, “object”]], optional (default=”none”)
Specifies a new the default value for
builds(..., hydra_convert=<..>)- frozenbool, optional (default=False)
Deprecated since version 0.9.0:
frozenwill be removed in hydra-zen 0.10.0. It is replaced byzen_dataclass={'frozen': <bool>}.Specifies a new the default value for
builds(..., frozen=<..>)- builds_fn: BuildsFn[T]
The builds-function whose defaults are modified.
- Returns:
- custom_builds[T]
The function
builds, but with customized default values.
See also
buildsCreate a targeted structured config designed to “build” a particular object.
Examples
>>> from hydra_zen import builds, make_custom_builds_fn, instantiate
Basic usage
The following will create a
buildsfunction whose default value forzen_partialhas been set toTrue.>>> pbuilds = make_custom_builds_fn(zen_partial=True)
I.e. using
pbuilds(...)is equivalent to usingbuilds(..., zen_partial=True).>>> instantiate(pbuilds(int)) # calls `functools.partial(int)` functools.partial(<class 'int'>)
You can still specify
zen_partialon a per-case basis withpbuilds.>>> instantiate(pbuilds(int, zen_partial=False)) # calls `int()` 0
Adding data validation to configs
Suppose that we want to enable runtime type-checking - using beartype - whenever our configs are being instantiated; then the following settings for
buildswould be handy.>>> # Note: beartype must be installed to use this feature >>> from hydra_zen.third_party.beartype import validates_with_beartype >>> build_a_bear = make_custom_builds_fn( ... populate_full_signature=True, ... hydra_convert="all", ... zen_wrappers=validates_with_beartype, ... )
Now all configs produced via
build_a_bearwill include type-checking during instantiation.>>> from typing_extensions import Literal >>> def f(x: Literal["a", "b"]): return x
>>> Conf = build_a_bear(f) # a conf that includes `validates_with_beartype`
>>> instantiate(Conf, x="a") # satisfies annotation: Literal["a", "b"] "a"
>>> instantiate(Conf, x="c") # violates annotation: Literal["a", "b"] <Validation error: "c" is not "a" or "b">