Skip to content

Override config file from env variable using nested path #658

@harscoet

Description

@harscoet

Describe the bug
Not sure if it's working as expected but there is a difference between uppercase and lowercase when we try to override the config from a file settings.yaml when there is at least three nested levels

To Reproduce

  1. Having the following folder structure
Project structure
.
├── requirements.txt
├── settings.yaml
└── test_config.py
  1. Having the following requirements.txt:
Project structure
python==3.9.6
dynaconf==3.1.5
pytest==6
  1. Having the following config files:
Config files

./settings.yaml

a:
  b: foo
  c:
    d: hello
  1. Having the following app code:
Code

./test_config.py

from dynaconf import Dynaconf

def new_settings():
  settings = Dynaconf(
    envvar_prefix="DYNACONF",
    settings_files=['settings.yaml'],
  )
  print(f"{settings.a=}")

  return settings

def test_nested_one_uppercase(monkeypatch):
    monkeypatch.setenv("DYNACONF_A__B", "OK")
    assert new_settings().a.b == "OK"

def test_nested_two_lowercase(monkeypatch):
    monkeypatch.setenv("DYNACONF_a__b__c", "OK")
    assert new_settings().a.b.c == "OK"

def test_nested_two_uppercase(monkeypatch):
    monkeypatch.setenv("DYNACONF_A__B__C", "OK")
    assert new_settings().A.B.C == "OK"
    assert new_settings().a.b.c == "OK"
  1. Executing under the following environment
Execution
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
pytest

I used pytest with monkeypatch fixture to play with the lib but the issue is still there outside tests. I'm using MacOS but I also tried to run the script in a linux docker image
The last test is not working because the path is in uppercase and there are many nested levels

============================================================================================================================= test session starts ==============================================================================================================================
platform darwin -- Python 3.9.6, pytest-6.0.0, py-1.10.0, pluggy-0.13.1
collected 3 items

test_config.py ..F                                                                                                                                                                                                                                                       [100%]

=================================================================================================================================== FAILURES ===================================================================================================================================
__________________________________________________________________________________________________________________________ test_nested_two_uppercase ___________________________________________________________________________________________________________________________

monkeypatch = <_pytest.monkeypatch.MonkeyPatch object at 0x102334a00>

    def test_nested_two_uppercase(monkeypatch):
        monkeypatch.setenv("DYNACONF_A__B__C", "OK")
        assert new_settings().A.B.C == "OK"
>       assert new_settings().a.b.c == "OK"
E       AttributeError: 'str' object has no attribute 'c'

test_config.py:23: AttributeError
----------------------------------------------------------------------------------------------------------------------------- Captured stdout call -----------------------------------------------------------------------------------------------------------------------------
settings.a=<Box: {'B': {'C': 'OK'}, 'b': 'foo', 'c': {'d': 'hello'}}>
settings.a=<Box: {'B': {'C': 'OK'}, 'b': 'foo', 'c': {'d': 'hello'}}>
=========================================================================================================================== short test summary info ============================================================================================================================
FAILED test_config.py::test_nested_two_uppercase - AttributeError: 'str' object has no attribute 'c'
========================================================================================================================= 1 failed, 2 passed in 0.33s ==========================================================================================================================

Metadata

Metadata

Assignees

No one assigned

    Labels

    LazyIssueRelated to lazy evaluation such as @format etc...bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions