From 2efb7a7c06a8e51516cc9b93e5dbcdea69f562fa Mon Sep 17 00:00:00 2001 From: Alex Sokolov Date: Wed, 14 May 2025 14:42:10 +0300 Subject: [PATCH] fix(settings): fix nested settings load via environment variables (#1551) Signed-off-by: Alexander Sokolov --- docling/datamodel/settings.py | 10 ++++++---- tests/test_settings_load.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 4 deletions(-) create mode 100644 tests/test_settings_load.py diff --git a/docling/datamodel/settings.py b/docling/datamodel/settings.py index fee871a..6cfc953 100644 --- a/docling/datamodel/settings.py +++ b/docling/datamodel/settings.py @@ -56,13 +56,15 @@ class DebugSettings(BaseModel): class AppSettings(BaseSettings): - model_config = SettingsConfigDict(env_prefix="DOCLING_", env_nested_delimiter="_") + model_config = SettingsConfigDict( + env_prefix="DOCLING_", env_nested_delimiter="_", env_nested_max_split=1 + ) - perf: BatchConcurrencySettings - debug: DebugSettings + perf: BatchConcurrencySettings = BatchConcurrencySettings() + debug: DebugSettings = DebugSettings() cache_dir: Path = Path.home() / ".cache" / "docling" artifacts_path: Optional[Path] = None -settings = AppSettings(perf=BatchConcurrencySettings(), debug=DebugSettings()) +settings = AppSettings() diff --git a/tests/test_settings_load.py b/tests/test_settings_load.py new file mode 100644 index 0000000..a36015e --- /dev/null +++ b/tests/test_settings_load.py @@ -0,0 +1,29 @@ +import os + + +def _setup_env(): + os.environ["DOCLING_PERF_PAGE_BATCH_SIZE"] = "12" + os.environ["DOCLING_DEBUG_VISUALIZE_RAW_LAYOUT"] = "True" + os.environ["DOCLING_ARTIFACTS_PATH"] = "/path/to/artifacts" + + +def test_settings(): + _setup_env() + + import importlib + + import docling.datamodel.settings as m + + # Reinitialize settings module + importlib.reload(m) + + # Check top level setting + assert str(m.settings.artifacts_path) == "/path/to/artifacts" + + # Check nested set via environment variables + assert m.settings.perf.page_batch_size == 12 + assert m.settings.debug.visualize_raw_layout is True + + # Check nested defaults + assert m.settings.perf.doc_batch_size == 2 + assert m.settings.debug.visualize_ocr is False