Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion confuse/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def build_dict(
save_to = save_to[child_key]

# Save
if key in save_to:
if isinstance(save_to.get(key), dict) and isinstance(result, dict):
save_to[key].update(result)
else:
save_to[key] = result
Expand Down
9 changes: 6 additions & 3 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@ Unreleased
New features
~~~~~~~~~~~~

..
Bug fixes
~~~~~~~~~
Bug fixes
~~~~~~~~~

- A more specific nested key (for example ``APP_DATABASE__PORT`` or a dotted
``set_args`` key) no longer raises when it overrides a non-mapping value
inside an existing mapping; the more specific value now wins.

..
For plugin developers
Expand Down
6 changes: 6 additions & 0 deletions test/test_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,12 @@ def test_parse_yaml_docs_scalar(self):
config = _root(confuse.EnvSource("TEST_", parse_yaml_docs=True))
assert config["foo"].get() == "a"

def test_nested_env_var_overrides_yaml_mapping_leaf(self):
os.environ["TEST_DATABASE"] = "{host: localhost, port: 5432}"
os.environ["TEST_DATABASE__PORT"] = "5433"
config = _root(confuse.EnvSource("TEST_", parse_yaml_docs=True))
assert {"host": "localhost", "port": 5433} == config["database"].get(dict)

def test_parse_yaml_docs_list(self):
os.environ["TEST_FOO"] = "[a, b]"
config = _root(confuse.EnvSource("TEST_", parse_yaml_docs=True))
Expand Down
13 changes: 13 additions & 0 deletions test/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,19 @@ def test_dot_sep_keys_no_clobber(self):
assert 2 == result["foo"]["far"]
assert 4 == result["foo"]["zar"]["dar"]

def test_nested_key_overrides_scalar_in_existing_mapping(self):
config = {
"database": {"host": "localhost", "port": 5432},
"database.port": 5433,
}
result = confuse.util.build_dict(config, sep=".")
assert {"host": "localhost", "port": 5433} == result["database"]

def test_nested_key_overrides_list_in_existing_mapping(self):
config = {"foo": {"bar": [1, 2]}, "foo.bar": 3}
result = confuse.util.build_dict(config, sep=".")
assert 3 == result["foo"]["bar"]

def test_adjacent_underscores_sep_keys(self):
config: dict[str, Any] = {"foo__bar_baz": 1}
result = confuse.util.build_dict(config.copy())
Expand Down
Loading