Skip to content

Commit

Permalink
fix: #1088 fresh vars definition case insensitive (#1091)
Browse files Browse the repository at this point in the history
  • Loading branch information
rochacbruno committed Apr 19, 2024
1 parent 353b1c1 commit 4dadf0e
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 2 deletions.
9 changes: 7 additions & 2 deletions dynaconf/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from dynaconf.utils import BANNER
from dynaconf.utils import compat_kwargs
from dynaconf.utils import ensure_a_list
from dynaconf.utils import ensure_upperfied_list
from dynaconf.utils import missing
from dynaconf.utils import object_merge
from dynaconf.utils import recursively_evaluate_lazy_format
Expand Down Expand Up @@ -138,7 +139,8 @@ def __getattr__(self, name):
name.isupper()
and (
self._wrapped._fresh
or name in self._wrapped.FRESH_VARS_FOR_DYNACONF
or name
in ensure_upperfied_list(self._wrapped.FRESH_VARS_FOR_DYNACONF)
)
and name not in UPPER_DEFAULT_SETTINGS
):
Expand Down Expand Up @@ -525,7 +527,10 @@ def get(
if (
fresh
or self._fresh
or key in getattr(self, "FRESH_VARS_FOR_DYNACONF", ())
or key
in ensure_upperfied_list(
getattr(self, "FRESH_VARS_FOR_DYNACONF", ())
)
) and key not in UPPER_DEFAULT_SETTINGS:
self.unset(key)
self.execute_loaders(key=key)
Expand Down
5 changes: 5 additions & 0 deletions dynaconf/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,11 @@ def ensure_a_list(data: T | list[T]) -> list[T]:
return [data]


def ensure_upperfied_list(data: list) -> list:
"""Ensure list of strings contains upperfied items."""
return [upperfy(item) if isinstance(item, str) else item for item in data]


def build_env_list(obj: Settings | LazySettings, env: str | None) -> list[str]:
"""Build env list for loaders to iterate.
Expand Down
5 changes: 5 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from dynaconf.loaders.json_loader import DynaconfEncoder
from dynaconf.utils import build_env_list
from dynaconf.utils import ensure_a_list
from dynaconf.utils import ensure_upperfied_list
from dynaconf.utils import extract_json_objects
from dynaconf.utils import isnamedtupleinstance
from dynaconf.utils import Missing
Expand Down Expand Up @@ -363,6 +364,10 @@ def test_ensure_a_list():
assert ensure_a_list(1) == [1]


def test_ensure_upperfied_list():
assert ensure_upperfied_list([1, "a", "A__b"]) == [1, "A", "A__b"]


def test_get_local_filename():
settings_path = os.path.join("foo", "b", "conf.toml")
local_path = os.path.join("foo", "b", "conf.local.toml")
Expand Down
44 changes: 44 additions & 0 deletions tests_functional/issues/1088_freshvars/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from __future__ import annotations

import os
from pathlib import Path

from dynaconf import Dynaconf

HERE = Path(__file__).parent

os.environ["DYNACONF_NAME"] = "stay fresh"
settings = Dynaconf(
fresh_vars=["name", "AGE"], settings_files=["settings.toml"]
)


def do_the_sets(age: int = 10):
with open(HERE / "settings.toml", "w") as sf:
sf.write(f"age = {age}")

settings.name = "not so fresh" # NO EFFECT
settings.NAME = "not so fresh"
settings.NaMe = "not so fresh"
settings.set("name", "not so fresh")
settings["name"] = "not so fresh"

print(settings.name)
print(settings.age)


do_the_sets()
assert settings["NAME"] == "stay fresh"
assert settings["AGE"] == 10
do_the_sets(20)
assert settings.name == "stay fresh"
assert settings["AGE"] == 20
do_the_sets(30)
assert settings.NAME == "stay fresh"
assert settings["AGE"] == 30
do_the_sets(40)
assert settings["NAME"] == "stay fresh"
assert settings["AGE"] == 40
do_the_sets(50)
assert settings("NAME") == "stay fresh"
assert settings["AGE"] == 50
1 change: 1 addition & 0 deletions tests_functional/issues/1088_freshvars/settings.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
age = 50

0 comments on commit 4dadf0e

Please sign in to comment.