Skip to content

[bug] @format sometimes does not evaluate correctly when using environments #666

@ZianVW

Description

@ZianVW

Describe the bug
The @Format dynamic variable does not produce the expected result with overlaid environments. It seems that the unexpected behavior is triggered when there is another key on the same level of the settings tree.

To Reproduce
Steps to reproduce the behavior:

  1. Having the following folder structure

The unexpected behavior occurs regardless of the folder structure. I've attached a zip with source code necessary to reproduce the error, which can be unzipped into a single folder.

Project structure
.env
.gitignore
.secrets.yaml
config.py
main.py
requirements.in
requirements.txt
settings.yaml
  1. Having the following config files:
Config files

/path/.env

ENV_FOR_DYNACONF=env1

and

/path/settings.yaml

default:
    a_level1:
        prefix: default
        level2:
            value: "@format {this.a_level1.prefix}-xxx"
    b_level1:
        prefix: default
        level2:
            some_key: True
            value: "@format {this.b_level1.prefix}-xxx"

env1:
    dynaconf_merge: True
    a_level1:
        prefix: env1

    b_level1:
        prefix: env1
        level2:
            some_key: False
  1. Having the following app code:
Code

/path/src/main.py

from config import settings

print(f"a_level1.level2.value: {settings.a_level1.level2.value}")
print(f"b_level1.level2.value: {settings.b_level1.level2.value}  #<--- Evaluated to wrong value.  Expected: env1-xxx")
  1. Executing under the following environment
Execution

Run using Python 3.8 in a virtual environment on Windows, with only dynaconf 3.1.7 installed.

$ python /path/src/main.py

PS C:\Users\dj36sv\Develop\scratch\dynaconf> python main.py
a_level1.level2.value: env1-xxx
b_level1.level2.value: default-xxx  <--- Evaluated to wrong value.  Expected: env1-xxx

Expected behavior
There are two cases in the provided settings file, under "a_level1" and "b_level1" respectively.

The case under "a_level1" works as expected.

In the case under "b_level1", I expect that setting b_level1.level2.value should use the prefix value that is associated with its environment (env1) when evaluating the @Format string, and should yield "env1-xxx". Instead, it yields "default-xxx", which comes from the "default" environment settings. This seems to be triggered by the presence of the sibling "some_key" setting under the same parent. When I omit that setting, as seen under the "a_level1" parent, it works as expected.

Environment (please complete the following information):

  • OS: Windows 10 Enterprise/21H1
  • Dynaconf Version: 3.1.7
  • Frameworks in use: None

Additional context
Add any other context about the problem here.

dynaconf.zip

Metadata

Metadata

Assignees

No one assigned

    Labels

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

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions