Settings done right

This commit is contained in:
Eden Kirin
2023-09-21 08:37:44 +02:00
parent 27a449793c
commit ec306b38fe
6 changed files with 58 additions and 44 deletions

View File

@ -3,6 +3,7 @@
Take note of the environment variable prefixes required for each
settings class, except `AppSettings`.
"""
import sys
from typing import Literal, Optional, Union
__all__ = [
@ -17,10 +18,11 @@ __all__ = [
from pydantic import Extra
from pydantic_settings import BaseSettings
from const import ROOT_DIR
class BaseEnvSettings(BaseSettings):
class Config:
env_file = ".env"
env_file_encoding = "utf-8"
extra = Extra.ignore
@ -89,12 +91,7 @@ class TestingSettings(BaseEnvSettings):
env_prefix = "TESTS_"
case_sensitive = True
DB_HOST: str = "localhost"
DB_PORT: int = 5432
DB_TEMPLATE_NAME: str = "db-template-name"
DB_NAME: str = "test_db-name"
DB_USER: str = "db-user"
DB_PASSWORD: str = "db-password"
DROP_DATABASE_BEFORE_TESTS: bool = True
DROP_DATABASE_AFTER_TESTS: bool = True
@ -117,11 +114,18 @@ class EmailSettings(BaseEnvSettings):
case_sensitive = True
# `.parse_obj()` thing is a workaround for pyright and pydantic interplay, see:
# https://github.com/pydantic/pydantic/issues/3753#issuecomment-1087417884
api = APISettings.parse_obj({})
app = AppSettings.parse_obj({})
db = DatabaseSettings.parse_obj({})
openapi = OpenAPISettings.parse_obj({})
server = ServerSettings.parse_obj({})
testing = TestingSettings.parse_obj({})
if "pytest" in sys.modules:
env_file = ROOT_DIR / ".env.testing"
else:
env_file = ROOT_DIR / ".env"
params = {
"_env_file": env_file,
}
api = APISettings(**params)
app = AppSettings(**params)
db = DatabaseSettings(**params)
openapi = OpenAPISettings(**params)
server = ServerSettings(**params)
testing = TestingSettings(**params)