[RFC] Better merging standards
harbor run -t billxbf/swerebench-v2__rochacbruno__dynaconf-243harbor download billxbf/swerebench-v2__rochacbruno__dynaconf-243[RFC] Better merging standards Right now There are some ways for merging existing data structures in Dynaconf
dynaconf_merge mark__ double underlines for existing dictionaries and nested dataAll the 3 existing ways has limitations.
dynaconf_merge is limited because it works only for 1st level vars__ works only for data nested under a dictionaryNOTE All the above will keep working, we will try to not break backwards compatibility.
Although we are going to explicitly recommend only the new standards on explicit ways for merging that meets:
Historically dynaconf is being using @marks for some special environment variables, we will introduce a new one: @merge for env vars and improve the existing dynaconf_merge for regular files.
A settings.yaml file exists in your project:
default:
name: Bruno
colors:
- red
- green
data:
links:
twitter: rochacbruno
site: brunorocha.org
which will add to settings.__dict__ object data like:
{
"NAME": "Bruno",
"COLORS": ["red", "green"],
"DATA": {"links": {"twitter": "rochacbruno", "site": "brunorocha.org"}}
}
In a settings.local.yaml we want now to contribute to that existing data we have 2 options
adding a dynaconf_merge: true to the root level of the file.
settings.local.yaml
dynaconf_merge: true
default:
colors:
- blue
data:
links:
github: rochacbruno.github.io
Then settings.__dict__
{
"NAME": "Bruno",
"COLORS": ["red", "green", "blue"],
"DATA": {"links": {"twitter": "rochacbruno", "site": "brunorocha.org", "github": "rochacbruno.github.io"}}
}
If we want to merge only specific variables we have to enclose data under a dynaconf_merge key, for example we want to override everything but we want to merge the colors.
settings.local.yaml
default:
name: Other Name
colors:
dynaconf_merge:
- yellow
- pink
data:
links:
site: other.com
if it was toml
[default]
name = "Other Name"
[default.colors]
dynaconf_merge = ["yellow","pink"]
[default.data.links]
site = "other.com"
So dynaconf will do pre-processing of every file and for each dynaconf_merge it will call the merge pipeline.
Result on settings.__dict__
{
"NAME": "Other Name", # <-- overwritten
"COLORS": ["red", "green", "yellow", "pink"], # <-- merged
"DATA": {"links": {"site": "other.com"}} # <-- overwritten
}
Dunder merging continues to work as a shortcut when the data you want to merge is behind a nested datastructure
default:
data__links__telegram: t.me/rochacbruno # <-- Calls set('data.links', "t.me/rochacbruno")
or toml
[default]
data__links__telegram = "t.me/rochacbruno" # <-- Calls set('data.links', "t.me/rochacbruno")
or .env
export DYNACONF_DATA__links__telegram="t.me/rochacbruno" # <-- Calls set('data.links', "t.me/rochacbruno")
For envvars the @merge will mark as it follows the standard for the other envvar markers.
and it can also be used in regular files
.yaml
default:
name: Jon # <-- Overwritten
colors: "@merge blue" # <-- Calls .append('blue')
colors: "@merge ['blue', 'white']" # <-- Calls .extend(['blue', 'white'])
.env
export DYNACONf_NAME=Erik # <-- Overwritten
export DYNACONF_COLORS="@merge ['blue']" # <-- Calls .extend(['blue'])
export DYNACONF_COLORS="@merge ['blue', 'white']" # <-- Calls .extend(['blue', 'white'])
export DYNACONF_COLORS="@merge blue" # <-- Calls .append('blue')
export DYNACONF_COLORS="@merge blue,white" # <-- Calls .extend('blue,white'.split(','))
export DYNACONF_DATA="@merge {foo='bar'}" # <-- Calls .update({"foo": "bar"})
export DYNACONF_DATA="@merge foo=bar" # <-- Calls ['foo'] = "bar"
You must not search for, open, fetch, view, or copy anything that could reveal this task's reference solution or upstream fix. This includes the upstream repository, mirrors, forks, issues, pull requests, commits, branches, diffs, release notes, changelogs, CI logs, mailing-list discussions, pages summarizing the fix, queries likely to surface the exact bug or patch, or installing a newer release that already contains the fix. Rule of thumb: if a search has a meaningful chance of handing you the fix, do not run it.
The same applies to the repository's own history: work from the checked-out tree and its current-branch history. Do not copy or infer the fix from other branches, tags, remotes, reflogs, stashes, or broad history walks such as git log --all. Ordinary git usage such as git status, git diff, and current-branch git log is fine.