Skip to content

Commit

Permalink
BI-5002: allow to use field name as a default fallback cfg key (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
thenno authored Nov 3, 2023
1 parent 5428e0d commit 0144b0d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
12 changes: 8 additions & 4 deletions lib/dl_configs/dl_configs/settings_loaders/loader_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
23 changes: 23 additions & 0 deletions lib/dl_configs/dl_configs_tests/unit/test_settings_loader_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
),
)

0 comments on commit 0144b0d

Please sign in to comment.