Skip to content

Commit 85aaf7a

Browse files
authored
fix: keep the nested out of order table (#366)
1 parent f12ece3 commit 85aaf7a

File tree

3 files changed

+43
-3
lines changed

3 files changed

+43
-3
lines changed

CHANGELOG.md

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

1212
- Fix the incompatiblity with 3.13 because of the `datetime.replace()` change. ([#333](https://github.com/python-poetry/tomlkit/issues/333))
1313
- Revert the change of parsing out-of-order tables. ([#347](https://github.com/python-poetry/tomlkit/issues/347))
14+
- Keep the nested out-of-order table. ([#361](https://github.com/python-poetry/tomlkit/issues/361))
1415

1516

1617
## [0.12.5] - 2024-05-08

tests/test_toml_document.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,31 @@ def test_remove_from_out_of_order_table():
676676
assert json.dumps(document) == '{"a": {"x": 1}, "c": {"z": 3}}'
677677

678678

679+
def test_update_nested_out_of_order_table():
680+
doc = parse("""\
681+
[root1.root2.a.b.c]
682+
value = 2
683+
[WALRUS]
684+
goo = "gjob"
685+
[root1.root2.x]
686+
value = 4
687+
""")
688+
doc["root1"]["root2"]["a"].add("tmp", "hi")
689+
assert (
690+
doc.as_string()
691+
== """\
692+
[root1.root2.a]
693+
tmp = "hi"
694+
[root1.root2.a.b.c]
695+
value = 2
696+
[WALRUS]
697+
goo = "gjob"
698+
[root1.root2.x]
699+
value = 4
700+
"""
701+
)
702+
703+
679704
def test_updating_nested_value_keeps_correct_indent():
680705
content = """
681706
[Key1]

tomlkit/container.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ def _validate_out_of_order_table(self, key: SingleKey | None = None) -> None:
155155
return
156156
if key not in self._map or not isinstance(self._map[key], tuple):
157157
return
158-
OutOfOrderTableProxy(self, self._map[key])
158+
OutOfOrderTableProxy.validate(self, self._map[key])
159159

160160
def append(
161161
self, key: Key | str | None, item: Item, validate: bool = True
@@ -786,7 +786,21 @@ def _previous_item(self, idx: int | None = None, ignore=(Null,)) -> Item | None:
786786

787787

788788
class OutOfOrderTableProxy(_CustomDict):
789-
def __init__(self, container: Container, indices: tuple[int]) -> None:
789+
@staticmethod
790+
def validate(container: Container, indices: tuple[int, ...]) -> None:
791+
"""Validate out of order tables in the given container"""
792+
# Append all items to a temp container to see if there is any error
793+
temp_container = Container(True)
794+
for i in indices:
795+
_, item = container._body[i]
796+
797+
if isinstance(item, Table):
798+
for k, v in item.value.body:
799+
temp_container.append(k, v, validate=False)
800+
801+
temp_container._validate_out_of_order_table()
802+
803+
def __init__(self, container: Container, indices: tuple[int, ...]) -> None:
790804
self._container = container
791805
self._internal_container = Container(True)
792806
self._tables = []
@@ -799,7 +813,7 @@ def __init__(self, container: Container, indices: tuple[int]) -> None:
799813
self._tables.append(item)
800814
table_idx = len(self._tables) - 1
801815
for k, v in item.value.body:
802-
self._internal_container.append(k, v, validate=False)
816+
self._internal_container._raw_append(k, v)
803817
self._tables_map[k] = table_idx
804818
if k is not None:
805819
dict.__setitem__(self, k.key, v)

0 commit comments

Comments
 (0)