Repository navigation
Fix invalid TOML when adding a dotted key to an inline table - #692
Open
LindseyZ1205 wants to merge 1 commit into
Open
LindseyZ1205 wants to merge 1 commit into
LindseyZ1205 wants to merge 1 commit into
Conversation
A dotted key made with key() keeps the default " = " separator on every
part, while the parser leaves it empty on all parts but the last one.
An inline table renders those parts with their separator after the dot,
so adding such a key rendered {c. = d = 2}. Leave their separator empty
in _handle_dotted_key(), and put the space before the new entry on the
table that the dotted key adds instead of on its value, which comes
after the prefix.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adding a dotted key made with
key()to an inline table writes invalid TOML:tomllibrejects it withInvalid initial character for a key part, and tomlkit's own parser withEmpty key.append()and item assignment with the key do the same, and so does a new inline table ({c. = d = 2}). On a document or a table, the same key is written asc.d = 2.The parser leaves the separator empty on every part of a dotted key but the last one, but a key made with
key()keeps the default" = "on all of them.InlineTable._render_dotted()writes the separator of those parts after the dot, so it ends up asc. =. This change leaves their separator empty inContainer._handle_dotted_key(), so the key has the same shape as a parsed one and is written asc.d = 2.InlineTable.append()also puts the space before a new entry on the value. For a dotted key the value comes after the prefix, which gave{b = 1,c. d = 2}with only the change above, so the space now goes on the table that the dotted key adds.test_add_dotted_key_to_inline_tableandtest_add_dotted_key_to_parsed_inline_table, next totest_add_dotted_key, cover a new and a parsed inline table, including a key under an existing dotted key and a key with three parts. Both fail onmaster. The full test suite passes with Python 3.14 and 3.12, and so doruff checkandruff format --check. I found this with a script that makes random edits through the API and reads the output back withtomllib.Duplicate Check
key()is still written asc. = d = 2. This PR doesn't touch_render_dotted()and merges cleanly with both.Agent Drafting Metadata