Validate and serialize YAML config files with Pydantic, with support for environment variables.
This package uses uv for project management. To get started, ensure that uv is installed on your machine and updated to the 0.5.6
version. Detailed installation instructions for uv can be found here.
uv add pyamldantic
config.yaml
database:
host: $DATABASE_HOST
name: $DATABASE_NAME
password: $DATABASE_PASSWORD
port: $DATABASE_PORT
user: $DATABASE_USER
timeout: $DATABASE_TIMEOUT?
environment: development
is_debug: true
config.py
from pyamldantic import YAMLConfig
from pydantic import BaseModel, SecretStr
class DatabaseSchema(BaseModel):
host: str
name: str
password: SecretStr
port: int
user: str
ssl: bool = False
timeout: int | None = None
class Schema(BaseModel):
database: DatabaseSchema
environment: str
is_debug: bool
config = YAMLConfig.load("config.yaml", schema=Schema)
main.py
from .config import config
if __name__ == "__main__":
print(f"Initializing {config.environment} environment...")
...
uv sync --frozen --group=development
uv run --frozen pre-commit install --install-hooks
uv run --frozen pre-commit install --hook-type=commit-msg
uv sync --frozen --group=testing
uv run --frozen pytest
This project was inspired by envyaml.
Contributions are welcome! To get started, please refer to our contribution guidelines.
If you encounter any problems while using this package, please open a new issue here.