是否可以在 hydra-core python 包的结构化配置中使用 Pydantic 而不是数据类?

Is it possible to use Pydantic instead of dataclasses in Structured Configs in hydra-core python package?

最近开始使用hydra to manage the configs in my application. I use Structured Configs to create schema for .yaml config files. Structured Configs in Hyda uses dataclasses for type checking. However, I also want to use some kind of validators for some of the parameter I specify in my Structured Configs (something like this).

您知道是否可以通过某种方式使用 Pydantic 来达到此目的?当我尝试使用 Pydantic 时,OmegaConf 抱怨它:

omegaconf.errors.ValidationError: Input class 'SomeClass' is not a structured config. did you forget to decorate it as a dataclass?

参见 pydantic.dataclasses.dataclass,它是带有一些额外类型检查的标准库数据类的直接替代品。

对于那些想知道这是如何工作的人,这里有一个例子:

import hydra
from hydra.core.config_store import ConfigStore
from omegaconf import OmegaConf
from pydantic.dataclasses import dataclass
from pydantic import validator


@dataclass
class MyConfigSchema:
    some_var: float

    @validator("some_var")
    def validate_some_var(cls, some_var: float) -> float:
        if some_var < 0:
            raise ValueError(f"'some_var' can't be less than 0, got: {some_var}")
        return some_var


cs = ConfigStore.instance()
cs.store(name="config_schema", node=MyConfigSchema)


@hydra.main(config_path="/path/to/configs", config_name="config")
def my_app(config: MyConfigSchema) -> None:
    # The 'validator' methods will be called when you run the line below
    OmegaConf.to_object(config)


if __name__ == "__main__":    
    my_app()

config.yaml

defaults:
  - config_schema

some_var: -1  # this will raise a ValueError