Skip to content

Commit

Permalink
fix _most_ of the type checker issues in config.py
Browse files Browse the repository at this point in the history
The `PostgresDsn.build()` type checker issue is a head-scratcher
  • Loading branch information
hkeeler committed Mar 17, 2024
1 parent 806887e commit 992ebe5
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
15 changes: 7 additions & 8 deletions src/regtech_user_fi_management/config.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
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


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")

Expand All @@ -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")

Expand Down
4 changes: 2 additions & 2 deletions src/regtech_user_fi_management/entities/engine/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 992ebe5

Please sign in to comment.