refactor: read settings directly instead of getattr with a second default

About 85 call sites read a setting as getattr(settings, "NAME", fallback),
each carrying its own copy of the default. Every one of those names is a
field with a default on the model, so the fallback could never apply to
the real settings object; it only masked drift. Two had drifted:

- OPENAI_PROMPT_CACHE_KEY defaults to True on the model but the reader
  fell back to False, and two test stubs relied on that.
- SharePoint's MICROSOFT_AUTHORITY fallback to
  https://login.microsoftonline.com/<tenant> never fired, because the
  attribute always exists (as None), so MSAL got authority=None. The
  connector now derives the tenant authority when the setting is unset,
  as its test always assumed.

Four places read EMBEDDINGS_KEY straight from os.environ, skipping the
"None"/"" normalisation the model applies; they read the setting now.
Test stubs that replaced a module's settings with a SimpleNamespace list
every setting the code under test reads.
This commit is contained in:
arc53-machine committed 2026-09-17 11:14:34 +01:00
1 parent d70644743b
commit f882ef49a7
46 files changed
+111 -113

No files matched your search

+3
View File
@@ -29,6 +29,9 @@ def _make_llm(monkeypatch, capabilities=None, store_responses=False):
AZURE_DEPLOYMENT_NAME="dep",
OPENAI_RESPONSES_STORE=store_responses,
OPENAI_REASONING_SUMMARY="auto",
OPENAI_RESPONSES_TRUNCATION_AUTO=False,
OPENAI_PROMPT_CACHE_KEY=False,
OPENAI_PROMPT_CACHE_RETENTION=None,
),
)
from docsgpt.llm.openai import OpenAILLM
+14 -13
View File
@@ -24,18 +24,19 @@ def _make_llm(monkeypatch, store_responses=True, **extra_settings):
"docsgpt.llm.openai.StorageCreator",
types.SimpleNamespace(get_storage=lambda: None),
)
monkeypatch.setattr(
"docsgpt.llm.openai.settings",
types.SimpleNamespace(
OPENAI_API_KEY="k",
API_KEY="k",
OPENAI_BASE_URL="",
AZURE_DEPLOYMENT_NAME="dep",
OPENAI_RESPONSES_STORE=store_responses,
OPENAI_REASONING_SUMMARY="auto",
**extra_settings,
),
)
# Every setting the Responses path reads, with the hints off; tests opt in per case.
stub = {
"OPENAI_API_KEY": "k",
"API_KEY": "k",
"OPENAI_BASE_URL": "",
"AZURE_DEPLOYMENT_NAME": "dep",
"OPENAI_RESPONSES_STORE": store_responses,
"OPENAI_REASONING_SUMMARY": "auto",
"OPENAI_RESPONSES_TRUNCATION_AUTO": False,
"OPENAI_PROMPT_CACHE_KEY": False,
"OPENAI_PROMPT_CACHE_RETENTION": None,
}
monkeypatch.setattr("docsgpt.llm.openai.settings", types.SimpleNamespace(**{**stub, **extra_settings}))
from docsgpt.llm.openai import OpenAILLM
llm = OpenAILLM(api_key="k")
@@ -142,7 +143,7 @@ def _params(llm, **kwargs):
@pytest.mark.unit
def test_build_responses_params_defaults_omit_truncation_and_cache_hints(monkeypatch):
def test_build_responses_params_omits_truncation_and_cache_hints_when_off(monkeypatch):
llm = _make_llm(monkeypatch)
llm._prompt_cache_key = "conv-123"
params = _params(llm)
@@ -14,8 +14,8 @@ def mock_settings():
s.MICROSOFT_TENANT_ID = "tenant-id-123"
s.CONNECTOR_REDIRECT_BASE_URI = "https://redirect.example.com/callback"
s.MONGO_DB_NAME = "test_db"
# Delete MICROSOFT_AUTHORITY so getattr falls back to default
del s.MICROSOFT_AUTHORITY
# Unset, as in a real Settings object, so the tenant-derived authority is used.
s.MICROSOFT_AUTHORITY = None
return s