Skip to content
Merged
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
6 changes: 6 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,12 @@ def test_mapping_types_can_be_dumped() -> None:
assert dumps(x) == 'foo = "bar"\n'


def test_parsed_document_can_be_dumped_with_sorted_keys() -> None:
doc = loads('zzz = 1\naaa = "foo"')

assert dumps(doc, sort_keys=True) == 'aaa = "foo"\nzzz = 1\n'


def test_dumps_weird_object() -> None:
with pytest.raises(TypeError):
dumps(object()) # type: ignore[arg-type]
Expand Down
7 changes: 5 additions & 2 deletions tomlkit/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,12 @@ def dumps(data: Mapping[str, Any], sort_keys: bool = False) -> str:
Dumps a TOMLDocument into a string.
"""
if isinstance(data, (Table, InlineTable, Container)):
return data.as_string()
if not sort_keys:
return data.as_string()

table: Table = item(dict(data), _sort_keys=sort_keys)
return item(data, _sort_keys=True).as_string()

table = item(dict(data), _sort_keys=sort_keys)
return table.as_string()


Expand Down
Loading