diff --git a/CHANGES.md b/CHANGES.md index 2a796d1..92be4f2 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -4,7 +4,7 @@ Changes development (main) ------------------ -- +- Fix dump functions failing to write nested `Configuration` instances in TOML format. 0.17.1 (2025-10-15) ------------------- diff --git a/confidence/formats.py b/confidence/formats.py index 9e74266..7ecdda8 100644 --- a/confidence/formats.py +++ b/confidence/formats.py @@ -84,6 +84,7 @@ def loads(self, string: str) -> typing.Any: return tomlkit.value(string) def dumps(self, value: typing.Any) -> str: + value = unwrap(value) try: # attempt to dump the value as TOML document return tomlkit.dumps(value) diff --git a/tests/test_formats.py b/tests/test_formats.py index 8ce8586..cec608e 100644 --- a/tests/test_formats.py +++ b/tests/test_formats.py @@ -2,6 +2,7 @@ import pytest +from confidence import Configuration, unwrap from confidence.formats import JSON, TOML, YAML @@ -16,12 +17,24 @@ def test_singular_value_roundtrip(format, value): @pytest.mark.parametrize('format', (JSON, TOML, YAML, YAML(suffix='.conf', encoding='utf-32'))) -@pytest.mark.parametrize('value', ([], {}, [1, 2, 'a'], {'a': 1, 'b': 42.0, 'c': {'d': 'str'}, 'e': [{'g': True}]})) +@pytest.mark.parametrize( + 'value', + ( + [], + {}, + [1, 2, 'a'], + {'a': 1, 'b': 42.0, 'c': {'d': 'str'}, 'e': [{'g': True}]}, + # nested configuration object should get unwrapped before serialization + Configuration({'a.b.c': 42}), + # serialization should not be trying to resolve references, this one would cause a recursion error if it does + Configuration({'a.b': [1, 2, '${a}']}), + ), +) def test_multiple_values_roundtrip(format, value, tmp_path): fname = tmp_path / f'config{format.suffix}' format.dumpf(value, fname) - assert format.loadf(fname) == value + assert format.loadf(fname) == unwrap(value) def test_edit_format():