Files
DocsGPT/docsgpt/core/settings/_shared.py
T
arc53-machine 95d0799494 refactor(settings): tighten types on closed choices, containers and bounds
Enum-like settings whose allowed values were only listed in a comment are
now Literal types, so a typo fails at startup with a message naming the
allowed values instead of falling through to a default with a warning
(or, for VECTOR_STORE, failing on first use):

  AUTH_TYPE, VECTOR_STORE, STORAGE_TYPE, URL_STRATEGY, OCR_BACKEND,
  OCR_ENGINE, SANDBOX_BACKEND, DOC_PARSER_ENGINE, TTS_PROVIDER, STT_PROVIDER

Each keeps a before-validator that strips and lower-cases the value, since
the registries that consume them already lower-cased at the use site, and
AUTH_TYPE maps the "None"/"none"/"" spellings a .env file carries to None
(it was the string "None" before, which only worked because nothing
compared against it). An empty TTS/STT provider still means "off".
LLM_PROVIDER stays a plain str because providers are plugin-extensible.

Containers are typed (dict[str, int], list[str], dict[str, Any]) instead
of bare dict/list, six fields that were Optional with a non-None default
are plain, and integer settings whose description already states a range
carry it as a constraint (ge=0 for "0 disables", ge=1 for counts that
cannot be zero, 0 < threshold <= 1).
2026-09-17 11:08:50 +01:00

45 lines
1.4 KiB
Python

"""Building blocks shared by the settings groups.
Every group in this package is a :class:`SettingsGroup`: a ``BaseSettings``
subclass that owns one domain's fields. ``docsgpt.core.settings.Settings``
inherits from all of them, so the composed class keeps the flat
``settings.NAME`` attributes the rest of the codebase reads while each
domain's definitions live in their own module.
"""
from __future__ import annotations
from typing import Any, Optional
from pydantic_settings import BaseSettings, SettingsConfigDict
class SettingsGroup(BaseSettings):
"""Base for one domain's settings; groups are composed into ``Settings``."""
model_config = SettingsConfigDict(extra="ignore")
def normalize_choice(value: Any) -> Any:
"""Case-fold a closed-choice setting so ``PGVector`` and ``pgvector`` are the same choice."""
if isinstance(value, str):
return value.strip().lower()
return value
def normalize_secret(value: Optional[str]) -> Optional[str]:
"""Map the ways an unset secret reaches us from ``.env`` to ``None``.
``.env`` files carry ``KEY=None`` and ``KEY=`` for "not set", and pydantic
would otherwise keep those as the strings ``"None"`` and ``""``. Whitespace
around a real value is stripped.
"""
if value is None:
return None
if not isinstance(value, str):
return value
stripped = value.strip()
if stripped == "" or stripped.lower() == "none":
return None
return stripped