Skip to content

Commit bd31dbd

Browse files
authored
fix: Dumping a subelement of a parsed toml fails starting on 4rd level of nesting (#419)
Fixes #411 Signed-off-by: Frost Ming <[email protected]>
1 parent 62a6708 commit bd31dbd

File tree

3 files changed

+11
-2
lines changed

3 files changed

+11
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
- Fix repeated whitespace when removing an array item. ([#405](https://github.com/python-poetry/tomlkit/issues/405))
1212
- Fix invalid serialization after removing array item if the comma is on its own line. ([#408](https://github.com/python-poetry/tomlkit/issues/408))
13+
- Fix serialization of a nested dotted key table. ([#411](https://github.com/python-poetry/tomlkit/issues/411))
1314
- Refine the error message when use non-string as single key. ([#412](https://github.com/python-poetry/tomlkit/issues/412))
1415
- Fix invalid serialization after overwriting a key of a out-of-order table. ([#414](https://github.com/python-poetry/tomlkit/issues/414))
1516

tests/test_api.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,12 @@ def test_dump_to_file_object():
181181
assert fp.getvalue() == 'foo = "bar"\n'
182182

183183

184+
def test_dump_nested_dotted_table():
185+
a = tomlkit.parse("a.b.c.d='e'")["a"]
186+
assert a == {"b": {"c": {"d": "e"}}}
187+
assert dumps(a) == "b.c.d='e'"
188+
189+
184190
def test_integer():
185191
i = tomlkit.integer("34")
186192

tomlkit/api.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,17 @@ def dumps(data: Mapping, sort_keys: bool = False) -> str:
5050
"""
5151
Dumps a TOMLDocument into a string.
5252
"""
53-
if not isinstance(data, Container) and isinstance(data, Mapping):
53+
if not isinstance(data, (Table, InlineTable, Container)) and isinstance(
54+
data, Mapping
55+
):
5456
data = item(dict(data), _sort_keys=sort_keys)
5557

5658
try:
5759
# data should be a `Container` (and therefore implement `as_string`)
5860
# for all type safe invocations of this function
5961
return data.as_string() # type: ignore[attr-defined]
6062
except AttributeError as ex:
61-
msg = f"Expecting Mapping or TOML Container, {type(data)} given"
63+
msg = f"Expecting Mapping or TOML Table or Container, {type(data)} given"
6264
raise TypeError(msg) from ex
6365

6466

0 commit comments

Comments
 (0)