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
27 changes: 27 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,33 @@ def test_dumps_with_sorted_keys_recurses_into_a_parsed_document() -> None:
assert dumps(doc) == original


def test_dumps_with_sorted_keys_keeps_inline_values_before_aot_sections() -> None:
doc = loads("[t]\nz = 1\n[[t.a]]\nx = 2\n")
original = dumps(doc)
sorted_output = dumps(doc, sort_keys=True)
assert sorted_output == "[t]\nz = 1\n[[t.a]]\nx = 2\n"
assert loads(sorted_output).unwrap() == doc.unwrap()
assert dumps(doc) == original

nested_aot = loads("[[items]]\nz = 1\n[[items.a]]\nx = 2\n")
assert dumps(nested_aot, sort_keys=True) == "[[items]]\nz = 1\n[[items.a]]\nx = 2\n"

mixed_sections = loads("[t]\nz = 1\n[t.b]\ny = 3\n[[t.a]]\nx = 2\n")
assert dumps(mixed_sections, sort_keys=True) == (
"[t]\nz = 1\n[[t.a]]\nx = 2\n[t.b]\ny = 3\n"
)

inline_and_dotted = loads("[t]\nz = { b = 2, a = 1 }\ny.k = 3\n[[t.a]]\nx = 4\n")
assert dumps(inline_and_dotted, sort_keys=True) == (
"[t]\ny.k = 3\nz = { a = 1, b = 2 }\n[[t.a]]\nx = 4\n"
)

inline_before_subtable = loads("[t]\nz = { x = 1 }\n[t.a]\ny = 2\n")
assert dumps(inline_before_subtable, sort_keys=True) == (
"[t]\nz = { x = 1 }\n[t.a]\ny = 2\n"
)


def test_dumps_with_sorted_keys_sorts_inline_tables_inside_arrays() -> None:
# sort_keys must reach inline tables nested inside an ordinary array, not
# just tables, inline tables and arrays of tables, while keeping the
Expand Down
17 changes: 13 additions & 4 deletions tomlkit/items.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,15 @@ def item(value: ItemT, _parent: Item | None = ..., _sort_keys: bool = ...) -> It
def item(value: object, _parent: Item | None = ..., _sort_keys: bool = ...) -> Item: ...


def _opens_container_section(container: Container, key: Key, value: Item) -> bool:
"""Whether rendering *value* under *key* opens a ``[table]`` or ``[[aot]]`` header."""
if isinstance(value, AoT):
return True
if isinstance(value, Table):
return not key.is_dotted() or container._renders_table_header(value)
return False


def _sort_container_body(container: Container, inline: bool = False) -> None:
"""Reorder *container*'s entries by key, in place, and recurse.

Expand Down Expand Up @@ -153,10 +162,10 @@ def _sort_container_body(container: Container, inline: bool = False) -> None:
pending.append((key, value))
else:
# Same order as the plain-dict branch of ``item``: a value that
# renders as its own table section sorts after the inline
# values, so a key never ends up under a preceding section
# header.
sort_key = (isinstance(value, dict), key.key)
# renders as its own table or array-of-tables section sorts after
# inline values, so a key never ends up under a preceding
# section header.
sort_key = (_opens_container_section(container, key, value), key.key)
groups.append((sort_key, [*pending, (key, value)]))
pending = []

Expand Down
Loading