From 0144b0d1c944434585bd348bcd25fb96a772b86a Mon Sep 17 00:00:00 2001 From: Max Zuev Date: Fri, 3 Nov 2023 22:53:57 +0100 Subject: [PATCH] BI-5002: allow to use field name as a default fallback cfg key (#69) --- .../dl_configs/settings_loaders/loader_env.py | 12 ++++++---- .../unit/test_settings_loader_env.py | 23 +++++++++++++++++++ 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/lib/dl_configs/dl_configs/settings_loaders/loader_env.py b/lib/dl_configs/dl_configs/settings_loaders/loader_env.py index ec2e6b226..904d632b2 100644 --- a/lib/dl_configs/dl_configs/settings_loaders/loader_env.py +++ b/lib/dl_configs/dl_configs/settings_loaders/loader_env.py @@ -488,11 +488,15 @@ def build_composite_extractor( else: raise SettingsLoadingException(f"Unexpected signature of fallback factory for {field}") - elif cls.is_sub_settings_field(field) and default is not None and default is not NOT_SET: - field_default = getattr(default, field_name) - else: - field_default = NOT_SET + field_default = getattr(fallback_cfg, field_s_meta.name, NOT_SET) + if ( + field_default is NOT_SET + and cls.is_sub_settings_field(field) + and default is not None + and default is not NOT_SET + ): + field_default = getattr(default, field_name) child_extractor: SDictExtractor diff --git a/lib/dl_configs/dl_configs_tests/unit/test_settings_loader_env.py b/lib/dl_configs/dl_configs_tests/unit/test_settings_loader_env.py index 23653a235..0506a5975 100644 --- a/lib/dl_configs/dl_configs_tests/unit/test_settings_loader_env.py +++ b/lib/dl_configs/dl_configs_tests/unit/test_settings_loader_env.py @@ -812,3 +812,26 @@ class TypedSettings(SettingsBase): b=b_value, ), ) + + +def test_default_cfg_key_name(temp_file_factory): + @attr.s(frozen=True) + class Settings: + TEST_KEY: str = s_attrib("TEST_KEY", missing="default_value") # type: ignore + + class Fallback: + TEST_KEY = "test_value" + + perform_loader_env_check( + {}, + fallback_env=Fallback, + expected_settings=Settings( + TEST_KEY="test_value", + ), + ) + perform_loader_env_check( + {}, + expected_settings=Settings( + TEST_KEY="default_value", + ), + )