From 992ebe5cbb6500af5ea5d333edb4e5cb4fc431eb Mon Sep 17 00:00:00 2001 From: Hans Keeler Date: Sun, 17 Mar 2024 14:37:56 -0400 Subject: [PATCH] fix _most_ of the type checker issues in `config.py` The `PostgresDsn.build()` type checker issue is a head-scratcher --- src/regtech_user_fi_management/config.py | 15 +++++++-------- .../entities/engine/engine.py | 4 ++-- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/src/regtech_user_fi_management/config.py b/src/regtech_user_fi_management/config.py index 6b8cae6..4f29811 100644 --- a/src/regtech_user_fi_management/config.py +++ b/src/regtech_user_fi_management/config.py @@ -1,9 +1,8 @@ import os +from pathlib import Path from urllib import parse -from typing import Any -from pydantic import field_validator, ValidationInfo -from pydantic.networks import PostgresDsn +from pydantic import field_validator, PostgresDsn, ValidationInfo from pydantic_settings import BaseSettings, SettingsConfigDict from regtech_api_commons.oauth2.config import KeycloakSettings @@ -11,7 +10,7 @@ JWT_OPTS_PREFIX = "jwt_opts_" -env_files_to_load = [".env"] +env_files_to_load: list[Path|str] = [".env"] if os.getenv("ENV", "LOCAL") == "LOCAL": env_files_to_load.append(".env.local") @@ -23,22 +22,22 @@ class Settings(BaseSettings): inst_db_pwd: str inst_db_host: str inst_db_scheme: str = "postgresql+asyncpg" - inst_conn: PostgresDsn | None = None + inst_conn: str | None = None def __init__(self, **data): super().__init__(**data) @field_validator("inst_conn", mode="before") @classmethod - def build_postgres_dsn(cls, postgres_dsn, info: ValidationInfo) -> Any: + def build_postgres_dsn(cls, field_value, info: ValidationInfo) -> str: postgres_dsn = PostgresDsn.build( scheme=info.data.get("inst_db_scheme"), username=info.data.get("inst_db_user"), - password=parse.quote(info.data.get("inst_db_pwd"), safe=""), + password=parse.quote(str(info.data.get("inst_db_pwd")), safe=""), host=info.data.get("inst_db_host"), path=info.data.get("inst_db_name"), ) - return str(postgres_dsn) + return postgres_dsn.unicode_string() model_config = SettingsConfigDict(env_file=env_files_to_load, extra="allow") diff --git a/src/regtech_user_fi_management/entities/engine/engine.py b/src/regtech_user_fi_management/entities/engine/engine.py index 9a43682..b1cdff2 100644 --- a/src/regtech_user_fi_management/entities/engine/engine.py +++ b/src/regtech_user_fi_management/entities/engine/engine.py @@ -4,9 +4,9 @@ async_scoped_session, ) from asyncio import current_task -from config import settings +from regtech_user_fi_management.config import settings -engine = create_async_engine(settings.inst_conn.unicode_string(), echo=True).execution_options( +engine = create_async_engine(str(settings.inst_conn), echo=True).execution_options( schema_translate_map={None: settings.inst_db_schema} ) SessionLocal = async_scoped_session(async_sessionmaker(engine, expire_on_commit=False), current_task)