Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support regex to load config that falls under the same roof #416

Open
MarcBresson opened this issue Sep 20, 2024 · 1 comment
Open

support regex to load config that falls under the same roof #416

MarcBresson opened this issue Sep 20, 2024 · 1 comment
Assignees

Comments

@MarcBresson
Copy link

Hello ! It's me again :)

l'm using fsspec to implement a general interface, and I would like our end users to be able to use any storage they want and supply the required connection parameters through env variables.

Because I don't really want to specify all the different models for all the different storage solutions, I thought it would be quite cool to have one BaseSettings model that would capture any env variable that matches a regex. In my case, it would give something like:

from pydantic_settings import BaseSettings, SettingsConfigDict

class A(BaseSettings):
    model_config = SettingsConfigDict(match="STORAGE_*")

    name: str

of course it means that this model is also configured with extra="allow"

@MarcBresson
Copy link
Author

maybe it's too similar to env_nested_delimiter option, i'll let you be the hudge of that :)

The workaround is to have one nested model like the following:

from pydantic import BaseModel, ConfigDict
from pydantic_settings import BaseSettings, SettingsConfigDict

class StorageParameters(BaseModel):
    model_config = ConfigDict(extra="allow")


class StorageConfig(BaseSettings):
    model_config = SettingsConfigDict(env_nested_delimiter="__")

    storage_name: str
    storage: StorageParameters
import os

os.environ["STORAGE_NAME"] = "s3"
os.environ["STORAGE__USERNAME"] = "michel"
os.environ["STORAGE__PASSWORD"] = "1234"
StorageConfig()
#> StorageConfig(storage_name='s3', storage=StorageParameters(username='michel', password='1234'))

this is not it, but it is close enough

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants