[Python] pydantic-settings custom_resources does not work as expected

Discussão em 'Python' iniciado por Stack, Fevereiro 22, 2025.

  1. Stack

    Stack Membro Participativo

    class DummyConfig(BaseSettings):
    PARAM1: str
    PARAM2: str
    API_KEY: str
    model_config = SettingsConfigDict(
    env_prefix="dummy_", env_file=".env", env_file_encoding="utf-8"
    )

    @classmethod
    def customise_sources(
    cls,
    init_settings,
    env_settings,
    file_secret_settings):
    return (
    init_settings,
    env_settings,
    get_aws_prams_store,
    file_secret_settings,
    )


    def get_aws_prams_store(param_name: str = "/dev/dummy-service/API_KEY"):
    session = boto3.session.Session(
    profile_name='my-account', region_name="us-east-1")
    client = session.client("ssm")
    response = client.get_parameters(
    Names=[param_name],
    WithDecryption=True
    )
    params = {param["Name"].split("/")[-1]: param["Value"]
    for param in response["Parameters"]}
    return params


    .env content:

    dummy_PARAM1=value1
    dummy_PARAM2=value1


    I tested get_aws_prams_store out of config and it's working well

    config= DummyConfig()


    I got an error API_KEY Field required [type=missing, .....]

    I set API_KEY=None just to pass the test.

    print("PARAM1", config.PARAM1) #value1
    print("PARAM2", config.PARAM2) # value2
    print("API_KEY", config.API_KEY) # None


    I am not sure I am doing wrong ?

    Continue reading...

Compartilhe esta Página