Skip to content

Commit

Permalink
fix: keep the nested out of order table
Browse files Browse the repository at this point in the history
Signed-off-by: Frost Ming <[email protected]>
  • Loading branch information
frostming committed Jul 10, 2024
1 parent f12ece3 commit 5a51a15
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

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


## [0.12.5] - 2024-05-08
Expand Down
25 changes: 25 additions & 0 deletions tests/test_toml_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,31 @@ def test_remove_from_out_of_order_table():
assert json.dumps(document) == '{"a": {"x": 1}, "c": {"z": 3}}'


def test_update_nested_out_of_order_table():
doc = parse("""\
[root1.root2.a.b.c]
value = 2
[WALRUS]
goo = "gjob"
[root1.root2.x]
value = 4
""")
doc["root1"]["root2"]["a"].add("tmp", "hi")
assert (
doc.as_string()
== """\
[root1.root2.a]
tmp = "hi"
[root1.root2.a.b.c]
value = 2
[WALRUS]
goo = "gjob"
[root1.root2.x]
value = 4
"""
)


def test_updating_nested_value_keeps_correct_indent():
content = """
[Key1]
Expand Down
36 changes: 33 additions & 3 deletions tomlkit/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def _validate_out_of_order_table(self, key: SingleKey | None = None) -> None:
return
if key not in self._map or not isinstance(self._map[key], tuple):
return
OutOfOrderTableProxy(self, self._map[key])
OutOfOrderTableProxy.validate(self, self._map[key])

def append(
self, key: Key | str | None, item: Item, validate: bool = True
Expand Down Expand Up @@ -785,8 +785,38 @@ def _previous_item(self, idx: int | None = None, ignore=(Null,)) -> Item | None:
return prev[-1] if prev else None


def _validate_out_of_order_table(
container: Container, indices: tuple[int, ...]
) -> None:
"""Validate out of order tables in the given container"""
# Append all items to a temp container to see if there is any error
temp_container = Container(True)
for i in indices:
_, item = container._body[i]

if isinstance(item, Table):
for k, v in item.value.body:
temp_container.append(k, v, validate=False)

temp_container._validate_out_of_order_table()


class OutOfOrderTableProxy(_CustomDict):
def __init__(self, container: Container, indices: tuple[int]) -> None:
@staticmethod
def validate(container: Container, indices: tuple[int, ...]) -> None:
"""Validate out of order tables in the given container"""
# Append all items to a temp container to see if there is any error
temp_container = Container(True)
for i in indices:
_, item = container._body[i]

if isinstance(item, Table):
for k, v in item.value.body:
temp_container.append(k, v, validate=False)

temp_container._validate_out_of_order_table()

def __init__(self, container: Container, indices: tuple[int, ...]) -> None:
self._container = container
self._internal_container = Container(True)
self._tables = []
Expand All @@ -799,7 +829,7 @@ def __init__(self, container: Container, indices: tuple[int]) -> None:
self._tables.append(item)
table_idx = len(self._tables) - 1
for k, v in item.value.body:
self._internal_container.append(k, v, validate=False)
self._internal_container._raw_append(k, v)
self._tables_map[k] = table_idx
if k is not None:
dict.__setitem__(self, k.key, v)
Expand Down

0 comments on commit 5a51a15

Please sign in to comment.