Skip to content

Commit 6b5f079

Browse files
Render an emptied array of tables as an empty array (#567)
* Render an emptied array of tables as an empty array _render_aot looped over the AoT body to emit [[key]] headers, so an array of tables with no elements left rendered as nothing at all and the key was dropped from the output. Fall back to the inline `key = []` form, matching what an empty array built through the API already renders as. TOML only reads bare key/value pairs before the first table header, so these keys are hoisted there; left in body order an emptied array of tables sitting after a table would be parsed back as a key of that table. Non-empty arrays of tables are unaffected and still render in place. * Render an emptied nested array of tables with its bare key The inline `key = []` fallback is a bare key/value pair emitted inside the scope its `[[header]]` named, so applying the render prefix nested the key under itself: an emptied `[[t.a]]` rendered as `[t]` / `t.a = []`, which reads back as `t.t.a`. Drop the prefix for the fallback. Bare pairs are also read into whichever table the closest preceding header opened, so the existing root-level hoisting has to happen in every table body, not just the document body. Factor it into `_hoist_empty_aots` and apply it in `_render_table` and `_render_aot_table` too. Five parametrized regressions assert `parse(doc.as_string()).unwrap() == doc.unwrap()` across nesting under a table, either side of a sibling sub-table, two levels down, and inside an array-of-tables element. * Render an emptied array of tables relative to its nearest header An emptied array of tables falls back to the inline `key = []` form, which is a bare key/value pair: TOML reads it into whichever table the closest preceding header opened. The fallback was written with a bare key and left in body order, so it only landed in the right table when its parent had an explicit header of its own and nothing else came first. Collect the fallbacks of a scope up front instead, walking through the super tables that emit no header, and key each one relative to the scope that will read it. `[[t.a]]` with no `[t]` now renders `t.a = []` rather than moving the key to the root, and `[[t.u.a]]` under `[t]` renders `u.a = []` rather than `t.u.a = []`, which reparsed as `t.t.u.a`.
1 parent 229ee86 commit 6b5f079

2 files changed

Lines changed: 195 additions & 20 deletions

File tree

‎tests/test_toml_document.py‎

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1698,3 +1698,109 @@ def test_scalar_is_not_captured_by_table_rendered_from_dotted_key() -> None:
16981698
doc["z"] = 2
16991699

17001700
assert doc.as_string() == "a.b = 1\nz = 2\n"
1701+
1702+
1703+
def test_emptied_array_of_tables_renders_as_empty_array() -> None:
1704+
# https://github.com/python-poetry/tomlkit/issues/553
1705+
# An array of tables with no elements left has no `[[key]]` header to
1706+
# render, so it must fall back to the inline `key = []` form. Rendering
1707+
# nothing dropped the key entirely.
1708+
doc = parse("[[a]]\nx = 1\n")
1709+
doc["a"].pop()
1710+
1711+
assert doc.as_string() == "a = []\n"
1712+
assert parse(doc.as_string()) == {"a": []}
1713+
1714+
1715+
def test_emptied_array_of_tables_is_hoisted_above_table_headers() -> None:
1716+
# https://github.com/python-poetry/tomlkit/issues/553
1717+
# TOML only reads bare key/value pairs before the first table header, so
1718+
# the inline fallback has to be emitted there rather than in body order.
1719+
# Left in place it would be parsed back as a key of the preceding table.
1720+
doc = parse("[t]\nq = 2\n\n[[a]]\nx = 1\n")
1721+
doc["a"].pop()
1722+
1723+
assert parse(doc.as_string()) == {"t": {"q": 2}, "a": []}
1724+
assert doc.as_string().index("a = []") < doc.as_string().index("[t]")
1725+
1726+
1727+
def test_emptied_array_of_tables_keeps_preceding_scalars() -> None:
1728+
# https://github.com/python-poetry/tomlkit/issues/553
1729+
doc = parse("v = 9\n\n[[a]]\nx = 1\n")
1730+
doc["a"].pop()
1731+
1732+
assert parse(doc.as_string()) == {"v": 9, "a": []}
1733+
1734+
1735+
def test_non_empty_array_of_tables_is_not_hoisted() -> None:
1736+
# https://github.com/python-poetry/tomlkit/issues/553
1737+
# Only emptied arrays of tables change form; ordinary ones must round-trip
1738+
# byte for byte.
1739+
content = "[t]\nq = 2\n\n[[a]]\nx = 1\n"
1740+
1741+
assert parse(content).as_string() == content
1742+
1743+
1744+
@pytest.mark.parametrize(
1745+
("content", "empty"),
1746+
[
1747+
# Nested directly under a table.
1748+
("[t]\nq = 2\n\n[[t.a]]\nx = 1\n", lambda doc: doc["t"]["a"]),
1749+
# Nested under a table that also has a sibling sub-table after it.
1750+
("[t]\n\n[[t.a]]\nx = 1\n\n[t.b]\ny = 3\n", lambda doc: doc["t"]["a"]),
1751+
# ... and before it, so the fallback has to move.
1752+
("[t]\n\n[t.b]\ny = 3\n\n[[t.a]]\nx = 1\n", lambda doc: doc["t"]["a"]),
1753+
# Two levels down.
1754+
("[t]\n\n[t.u]\n\n[[t.u.a]]\nx = 1\n", lambda doc: doc["t"]["u"]["a"]),
1755+
# Inside an element of another array of tables.
1756+
("[[e]]\nn = 1\n\n[[e.a]]\nx = 1\n", lambda doc: doc["e"][0]["a"]),
1757+
# Parent table left implicit: there is no `[t]` header to make `a`
1758+
# bare, so the fallback has to keep the `t.` prefix.
1759+
("[[t.a]]\nx = 1\n", lambda doc: doc["t"]["a"]),
1760+
# ... two levels of implicit parent.
1761+
("[[t.u.a]]\nx = 1\n", lambda doc: doc["t"]["u"]["a"]),
1762+
# ... with an unrelated header before it, which the fallback must not
1763+
# be read into.
1764+
("[x]\nq = 1\n\n[[t.a]]\nx = 1\n", lambda doc: doc["t"]["a"]),
1765+
# ... and with a sibling sub-table, so the implicit parent survives
1766+
# into the rendered output as a header of its own.
1767+
("[x]\nq = 1\n\n[[t.a]]\nx = 1\n\n[t.b]\ny = 3\n", lambda doc: doc["t"]["a"]),
1768+
# Explicit grandparent, implicit parent: the fallback is relative to
1769+
# the nearest header, so it is `u.a`, not `t.u.a`.
1770+
("[t]\n\n[[t.u.a]]\nx = 1\n", lambda doc: doc["t"]["u"]["a"]),
1771+
("[t]\n\n[[t.u.a]]\nx = 1\n\n[t.u.b]\ny = 3\n", lambda doc: doc["t"]["u"]["a"]),
1772+
# Implicit parent inside an array-of-tables element.
1773+
("[[e]]\nn = 1\n\n[[e.s.a]]\nx = 1\n", lambda doc: doc["e"][0]["s"]["a"]),
1774+
],
1775+
)
1776+
def test_emptied_nested_array_of_tables_round_trips(content, empty) -> None:
1777+
# https://github.com/python-poetry/tomlkit/issues/553
1778+
# The inline fallback is a bare key/value pair emitted inside the scope its
1779+
# header named, so it must be written with the bare key. Carrying the
1780+
# header prefix over emitted `t.a = []` under `[t]`, which reads back as
1781+
# `t.t.a`.
1782+
doc = parse(content)
1783+
empty(doc).pop()
1784+
1785+
assert parse(doc.as_string()).unwrap() == doc.unwrap()
1786+
1787+
1788+
def test_emptied_array_of_tables_under_implicit_parent_keeps_its_path() -> None:
1789+
# https://github.com/python-poetry/tomlkit/issues/553
1790+
# A super table emits no header, so it opens no scope for the fallback to
1791+
# be bare in. Dropping the prefix here moved the key to the root.
1792+
doc = parse("[[t.a]]\nx = 1\n")
1793+
doc["t"]["a"].pop()
1794+
1795+
assert doc.as_string() == "t.a = []\n"
1796+
1797+
1798+
def test_emptied_array_of_tables_is_relative_to_the_nearest_header() -> None:
1799+
# https://github.com/python-poetry/tomlkit/issues/553
1800+
# `[t]` is emitted, `t.u` is not, so the fallback is written relative to
1801+
# `[t]`. Spelling it in full emitted `t.u.a = []` under `[t]`, which reads
1802+
# back as `t.t.u.a`.
1803+
doc = parse("[t]\n[[t.u.a]]\nx = 1\n")
1804+
doc["t"]["u"]["a"].pop()
1805+
1806+
assert doc.as_string() == "[t]\nu.a = []\n"

‎tomlkit/container.py‎

Lines changed: 89 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,76 @@
3232
_NOT_SET = object()
3333

3434

35+
def _is_empty_aot(item: Item) -> bool:
36+
"""Whether ``item`` is an array of tables with no elements left."""
37+
return isinstance(item, AoT) and not item.body
38+
39+
40+
def _emits_header(key: Key, table: Table) -> bool:
41+
"""Whether rendering ``table`` under ``key`` emits a ``[header]`` line.
42+
43+
A super table normally renders as nothing but the path prefix of its
44+
children, so it opens no scope of its own.
45+
"""
46+
return (
47+
not table.is_super_table()
48+
or (
49+
any(
50+
not isinstance(v, (Table, AoT, Whitespace, Null))
51+
for _, v in table.value.body
52+
)
53+
and not key.is_dotted()
54+
)
55+
or (
56+
any(
57+
k is not None and k.is_dotted()
58+
for k, v in table.value.body
59+
if isinstance(v, Table)
60+
)
61+
and not key.is_dotted()
62+
)
63+
)
64+
65+
66+
def _header_less_aots(
67+
body: list[tuple[Key | None, Item]], prefix: str = ""
68+
) -> list[tuple[str, AoT]]:
69+
"""Every emptied array of tables reachable without crossing a header.
70+
71+
An emptied array of tables has no ``[[key]]`` header left to render, so it
72+
falls back to the inline ``key = []`` form -- and TOML reads a bare
73+
key/value pair into whichever table the closest preceding header opened.
74+
So the fallbacks of a scope have to be written at the top of that scope,
75+
with a key relative to it, and that includes the ones sitting inside super
76+
tables, which emit no header to separate them.
77+
78+
Returns ``(dotted key, aot)`` pairs, keyed relative to the scope ``body``
79+
belongs to.
80+
"""
81+
found: list[tuple[str, AoT]] = []
82+
for k, v in body:
83+
if k is None:
84+
continue
85+
path = f"{prefix}.{k.as_string()}" if prefix else k.as_string()
86+
if _is_empty_aot(v):
87+
found.append((path, v))
88+
elif isinstance(v, Table) and not _emits_header(k, v):
89+
found.extend(_header_less_aots(v.value.body, path))
90+
return found
91+
92+
93+
def _render_empty_aot(key: str, aot: AoT) -> str:
94+
"""Render an emptied array of tables as an inline empty array."""
95+
return (
96+
f"{aot.trivia.indent}"
97+
f"{decode(key)}"
98+
f" = []"
99+
f"{aot.trivia.comment_ws}"
100+
f"{decode(aot.trivia.comment)}"
101+
f"{aot.trivia.trail or chr(10)}"
102+
)
103+
104+
35105
class Container(_CustomDict): # type: ignore[type-arg]
36106
"""
37107
A container for items within a TOMLDocument.
@@ -639,8 +709,12 @@ def last_item(self) -> Item | None:
639709
def as_string(self) -> str:
640710
"""Render as TOML string."""
641711
s = ""
712+
for path, aot in _header_less_aots(self._body):
713+
s += _render_empty_aot(path, aot)
642714
for k, v in self._body:
643715
if k is not None:
716+
if _is_empty_aot(v):
717+
continue
644718
if isinstance(v, Table):
645719
if (
646720
s.strip(" ")
@@ -675,24 +749,8 @@ def _render_table(self, key: Key, table: Table, prefix: str | None = None) -> st
675749
if prefix is not None:
676750
_key = prefix + "." + _key
677751

678-
if (
679-
not table.is_super_table()
680-
or (
681-
any(
682-
not isinstance(v, (Table, AoT, Whitespace, Null))
683-
for _, v in table.value.body
684-
)
685-
and not key.is_dotted()
686-
)
687-
or (
688-
any(
689-
k is not None and k.is_dotted()
690-
for k, v in table.value.body
691-
if isinstance(v, Table)
692-
)
693-
and not key.is_dotted()
694-
)
695-
):
752+
header_emitted = _emits_header(key, table)
753+
if header_emitted:
696754
open_, close = "[", "]"
697755
if table.is_aot_element():
698756
open_, close = "[[", "]]"
@@ -713,8 +771,15 @@ def _render_table(self, key: Key, table: Table, prefix: str | None = None) -> st
713771
elif table.trivia.indent == "\n":
714772
cur += table.trivia.indent
715773

774+
if header_emitted:
775+
# A super table opens no scope, so its fallbacks belong to -- and
776+
# were already written by -- the nearest enclosing header instead.
777+
for path, aot in _header_less_aots(table.value.body):
778+
cur += _render_empty_aot(path, aot)
716779
for k, v in table.value.body:
717-
if isinstance(v, Table):
780+
if k is not None and _is_empty_aot(v):
781+
continue
782+
elif isinstance(v, Table):
718783
if (
719784
cur.strip(" ")
720785
and not cur.strip(" ").endswith("\n")
@@ -773,8 +838,12 @@ def _render_aot_table(self, table: Table, prefix: str | None = None) -> str:
773838
f"{table.trivia.trail}"
774839
)
775840

841+
for path, aot in _header_less_aots(table.value.body):
842+
cur += _render_empty_aot(path, aot)
776843
for k, v in table.value.body:
777-
if isinstance(v, Table):
844+
if k is not None and _is_empty_aot(v):
845+
continue
846+
elif isinstance(v, Table):
778847
assert k is not None
779848
if v.is_super_table():
780849
if k.is_dotted():

0 commit comments

Comments
 (0)