Configurator

About

Module with BaseConfig. That config is the main element of bc_configs. It provides to receive values from the environment variables on the validation step of pydantic model. For use, create child class of BaseConfig and declare needed fields with typehint.

class MyConfig(BaseConfig):
    my_field: str


my_config = MyConfig()  # type: ignore[call-arg]

Note

my_config.my_field will contain the value of the environment variable MY_MY_FIELD.

Building the environment variable name

  • First part of the environment variable name is the class name in SCREAMING_SNAKE_CASE without the Config suffix.

  • Second part of the environment variable name is the field name in SCREAMING_SNAKE_CASE.

Note

If class name is FooBarConfig and field name is del_bar then the environment variable name will be FOO_BAR_DEL_BAR.

Customizing the environment variable name

If you want to customize the environment variable name, you can define it in field→json_schema_extra→env_name.

from pydantic import Field


class FooConfig(BaseConfig):
    bar: str = Field(json_schema_extra={"env_name": "CUSTOM_ENV_NAME"})

Casting the environment variable value

If you want to cast the environment variable value, you can define field typehint.

class FooConfig(BaseConfig):
    bar: str
    baz: int
    foo: bool


foo_config = FooConfig()

assert isinstance(foo_config.bar, str)  # True
assert isinstance(foo_config.baz, int)  # True
assert isinstance(foo_config.foo, bool)  # True

Optional fields

If you want to make the field optional, you can define field typehint with | None.

import os


class FooConfig(BaseConfig):
    bar: str | None


foo_config = FooConfig()

assert os.getenv("FOO_BAR") is None  # True
assert foo_config.bar is None  # True

Warning

By default, all fields are required. If the environment variable is missing an exception will follow.

Default values

If you want to define default values for the fields, you can define Field with default value argument.

import os
from pydantic import Field


class FooConfig(BaseConfig):
    bar: str = Field(default="default value")

assert os.getenv("FOO_BAR") is None  # True
assert foo_config.bar == "default value"  # True
pydantic model bc_configs.configurator.BaseConfig[source]

Bases: BaseModel, ABC

Provides to receive values from the environment variables on the validation step of pydantic model.

Show JSON schema
{
   "title": "BaseConfig",
   "description": "Provides to receive values from the environment variables on the validation step of\npydantic model.",
   "type": "object",
   "properties": {}
}

Validators:
  • _populate_from_env » all fields

BaseConfig

pydantic model bc_configs.configurator.BaseConfig.BaseConfig[source]

Bases: BaseModel, ABC

Provides to receive values from the environment variables on the validation step of pydantic model.

Show JSON schema
{
   "title": "BaseConfig",
   "description": "Provides to receive values from the environment variables on the validation step of\npydantic model.",
   "type": "object",
   "properties": {}
}

Validators:
  • _populate_from_env » all fields