hydra_zen.third_party.pydantic.pydantic_parser#
- hydra_zen.third_party.pydantic.pydantic_parser(target, *, parser=<function validate_call.<locals>.validate>)[source]#
A target-wrapper that adds pydantic parsing to the target.
This can be passed to
instantiate
as a_target_wrapper_
to add pydantic parsing to the (recursive) instantiation of the target.- Parameters:
- targetCallable
- parserType[pydantic.validate_arguments], optional
A configured instance of pydantic’s validation decorator.
- The default validator that we provide specifies:
arbitrary_types_allowed: True
Examples
from hydra_zen import builds, instantiate from hydra_zen.third_party.pydantic import pydantic_parser from pydantic import PositiveInt def f(x: PositiveInt): return x good_conf = builds(f, x=10) bad_conf = builds(f, x=-3)
>>> instantiate(good_conf, _target_wrapper_=pydantic_parser) 10 >>> instantiate(bad_conf, _target_wrapper_=pydantic_parser) ValidationError: 1 validation error for f (...)
This also enables type conversion / parsing. E.g. Hydra can only produce lists from the CLI, but this parsing layer can convert them based on the annotated type. (Note: this only works for pydantic v2 and higher.)
>>> def g(x: tuple): return x >>> conf = builds(g, x=[1, 2, 3]) >>> instantiate(conf, _target_wrapper_=pydantic_parser) (1, 2, 3)