Skip to content

Keep an emptied dotted key inside an inline table - #664

Open
cig13zs wants to merge 2 commits into
python-poetry:masterfrom
cig13zs:fix-inline-dotted-delete
Open

cig13zs wants to merge 2 commits into
python-poetry:masterfrom
cig13zs:fix-inline-dotted-delete

Conversation

@cig13zs

@cig13zs cig13zs commented Oct 15, 2026

Copy link
Copy Markdown

Fixes #662. Inside an inline table a dotted-key table is rendered as one prefix.child = value pair per child, so once every child is deleted it renders nothing and the separators around it leave , , (or a stray leading/trailing comma). The fix renders the emptied table as b = {}, which matches the data and follows how #567 handles an emptied array of tables.

Deleting the last key under a dotted prefix in an inline table left an
empty slot between two commas:

    >>> doc = parse('t = {a = "", b.c = 1, d = 1}\n')
    >>> del doc["t"]["b"]["c"]
    >>> doc.as_string()
    't = {a = "", , d = 1}\n'

_render_dotted renders one "prefix.child = value" pair per child, so an
emptied table produced no pairs at all while the separators around it
were still emitted. Render it as "b = {}" instead, which keeps the empty
table the document still holds and gives valid TOML wherever the key
sits in the inline table.
@carlitogwapito

Copy link
Copy Markdown

Here's a small optional test-only commit, based directly on your current head.

It checks both "b.c" and 'b.c' as literal key names, empties their child table, then adds a child back. tomllib checks the resulting data independently, alongside TOMLKit's own reader.

Two guards worth keeping: the dot stays part of the quoted key, and the empty table remains usable when refilled.

On Python 3.14.4, both new cases pass your source and hit the expected empty-state decoding error on the base. The existing dotted/nested append tests pass on both. Ruff 0.15.11 lint/format passes; I haven't run the configured 0.17.0 hooks, mypy, full corpus/suite, build or CI matrix. The oracle test explicitly skips below Python 3.11.

To run the added file in your tree: python -m pytest tests/test_empty_quoted_dotted_key.py -q.

@Wkhanex Wkhanex left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI-assisted validation at 6ada78613109 on Python 3.12.14: I tried emptying two dotted subtrees in the same inline table, both at one level (a={b.c=1, d.e=2}) and nested (a={x=0,b.c.d=1,d.e.f=2,y=3}). After deleting the leaves, both tomllib and TOMLKit recovered the expected empty dictionaries; adding a new sibling under b also round-tripped correctly. The seven focused dotted-inline-table tests passed.

This adds a slightly different boundary from the quoted-key/refill example already in the discussion: adjacent emptied subtrees must each keep their own rendered slot without introducing an empty comma-separated entry. That case appears worth keeping as a regression.

@cig13zs

cig13zs commented Oct 18, 2026

Copy link
Copy Markdown
Author

Thanks, I ran it: both cases pass on this branch and fail on master. If the maintainers want the quoted-key case covered I'd rather add it as another row in the existing parametrized test than a new file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Deleting the last key under a dotted key in an inline table produces invalid TOML

3 participants