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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
### Fixed

- Arithmetic and `replace()` results for `Integer`, `Float`, `String`, `DateTime`, `Date` and `Time` no longer share the original item's `Trivia`, so changing the result's comment or indentation no longer rewrites the item still stored in the document.
- Fix an `AttributeError` when a table is defined out of order under an indented header nested four or more levels deep, e.g. ` [a.b.c.d]` then `[a.b.c]`. ([#663](https://github.com/python-poetry/tomlkit/pull/663))

## [0.15.2] - 2026-10-16

Expand Down
19 changes: 19 additions & 0 deletions tests/test_toml_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -743,6 +743,25 @@ def test_valid_out_of_order_super_table_extended_by_dotted_key() -> None:
assert doc.as_string() == "[a.b.c]\n[a]\nb.z = 1\n"


@pytest.mark.parametrize(
("content", "expected"),
[
(" [a.b.c.d]\n[a.b.c]\n", {"a": {"b": {"c": {"d": {}}}}}),
(" [[a.b.c.d]]\n[a.b.c]\n", {"a": {"b": {"c": {"d": [{}]}}}}),
(" [a.b.c.d]\n[x]\n[a.b.c]\n", {"a": {"b": {"c": {"d": {}}}}, "x": {}}),
],
)
def test_out_of_order_table_after_indented_header(
content: str, expected: dict[str, Any]
) -> None:
# https://github.com/python-poetry/tomlkit/issues/263 covered `[a.b]` after
# an indented `[[a.b.c]]`. One level deeper, the out-of-order table lands
# under an intermediate table that carries the header's indent.
doc = parse(content)
assert doc.unwrap() == expected
assert doc.as_string() == content


def test_set_value_on_out_of_order_table_with_empty_concrete_part() -> None:
# A super table defined after its sub-table (the "defining a super-table
# afterward is ok" spec example) leaves an empty concrete `[x]` part.
Expand Down
4 changes: 3 additions & 1 deletion tomlkit/items.py
Original file line number Diff line number Diff line change
Expand Up @@ -1969,7 +1969,9 @@ def append(self, key: Key | str | None, _item: Any) -> Table:

indent = m.group(1)

if not isinstance(_item, Whitespace):
# A table split out of order is looked up as an OutOfOrderTableProxy,
# which has no trivia; its parts keep their own indentation.
if isinstance(_item, Item) and not isinstance(_item, Whitespace):
m = re.match("(?s)^([^ ]*)(.*)$", _item.trivia.indent)
if not m:
_item.trivia.indent = indent
Expand Down
Loading