Render a dotted key's sub-table inside an inline table as an inline table

FE
1mo ago
+57-3

Summary

Adding a sub-table to a dotted key inside an inline table produces TOML that cannot be parsed back:

>>> from tomlkit import parse, dumps
>>> doc = parse("a = {b.c = 1}\n")
>>> doc["a"]["b"]["e"] = {"f": 3}
>>> print(dumps(doc))
a = {b.c = 1, b.e. = f = 3}

b.e. is a key followed by a stray . and then =, which is not valid TOML in any version — the stdlib tomllib answers Invalid initial character for a key part (at line 1, column 20), and so does tomlkit's own parser, so the edit yields a document you cannot read back. After this change the same edit serializes as a = {b.c = 1, b.e = {f = 3}}, which tomllib accepts and which round-trips stably to three levels of nesting.

The cause is in InlineTable._render_dotted (tomlkit/items.py:2176), which rendered every Table child with the prefix.child = body shape. That is the right shape for a child that is itself a dotted key — which is what the parser materializes from b.c = 1 — but a child added afterwards is a real sub-table, and its body is not a value. So the fix recurses only for a child whose own key is dotted, and renders a real sub-table as an inline table, converting nested tables the same way.

The state is only reachable by a programmatic edit, which is why no round-trip test covered it: the parser never produces a non-dotted child under a dotted key inside an inline table. The two existing dotted-key tests in tests/test_items.py pin that adding keys there works and round-trips; this is the same API with a dict instead of a scalar.

Original implementation-time validation (not rerun on 2026-10-14): pytest tests/test_items.py -k dotted_key_inside_inline_table fails on 8c959b5 with tomlkit.exceptions.EmptyKeyError: Empty key at line 1 col 19 when the output is re-parsed, and passes here. The suite is 380 passed with --ignore=tests/test_toml_tests.py — that module needs the tests/toml-test submodule, which is not checked out here, so it errors at collection both before and after this change. ruff check and ruff format --check are clean on both files; mypy reports 2 pre-existing errors in tomlkit/source.py, which this change does not touch.

On 2026-10-14, I reran only the example above with Python 3.13.3, loading each checkout's tomlkit source. Original base 8c959b5 and current upstream 19edc23 both emitted a = {b.c = 1, b.e. = f = 3}; tomllib rejected it at line 1, column 20 and tomlkit raised EmptyKeyError. PR head ea1a82f emitted a = {b.c = 1, b.e = {f = 3}}, and both parsers read back {"a": {"b": {"c": 1, "e": {"f": 3}}}}. This also corrects the previously mismatched example output in this description. No full test suite, Ruff or mypy rerun accompanied this description-only correction.

I have not added a CHANGELOG entry — the file is organised per release and every entry cites a PR number, so tell me if you would like one and I will add it under a new section referencing this PR.

Agent Drafting Metadata

  • Agent: opencode (autonomous OSS contribution agent for this workspace)
  • Model: Space Bunny Free (opencode space-bunny-free)
  • Notes: the agent read InlineTable._render_dotted and the dotted-key rendering path, wrote the fix and the two regression tests, and ran the checks quoted above. I reviewed the code and the local evidence before pushing; a human review is requested before merge.

AI-assisted development. Human review requested before merge.