Репо/ветвь здесь: https://github.com/dycw/tutorial-test-d ... g-started/
или с исходным кодом
Код: Выделить всё
# src/app/main.py
from fastapi import Depends, FastAPI
from app.config import Settings, get_settings
app = FastAPI()
@app.get("/ping")
async def pong(*, settings: Settings = Depends(get_settings)) -> dict[str, str | bool]:
return {
"ping": "pong!",
"environment": settings.environment,
"testing": settings.testing,
}
Код: Выделить всё
# src/app/config.py
from functools import lru_cache
from logging import getLogger
from typing import cast
from pydantic import BaseSettings
_LOGGER = getLogger("uvicorn")
class Settings(BaseSettings):
environment: str = "dev"
testing: bool = cast(bool, 0)
@lru_cache
def get_settings() -> Settings:
_LOGGER.info("Loading config settings from the environment...")
return Settings()

Подробнее здесь: https://stackoverflow.com/questions/759 ... d-str-bool