Reject trailing characters in key_value() - #594
Open
dylanpulver wants to merge 1 commit into
Open
Conversation
tomlkit.key_value() parsed a single key/value pair and returned it without
checking that the input was consumed, so anything after the pair was silently
discarded:
>>> tomlkit.key_value("a = 1 b = 2 c = 3")
(<Key a >, 1)
TOML 1.0.0 requires a newline or EOF after a key/value pair, and parse()
already enforces that -- tests/examples/invalid/key_value_with_trailing_chars.toml
exists for exactly this rule and is asserted against parse() alone.
Parse the comment trail (as the document parser does for a top-level pair) and
then require end of input, mirroring the guard value() already has. Trailing
whitespace, a comment and a final newline stay valid; real trailing input now
raises UnexpectedCharError.
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
tomlkit.key_value()returned the first pair it could parse and threw awaywhatever followed:
TOML 1.0.0 (Key/Value Pair) requires
a newline or EOF after a pair, and
parse()already enforces it — each of theinputs in the new test raises
UnexpectedCharErrorthere.tests/examples/invalid/key_value_with_trailing_chars.tomlexists for this exactrule and its own text spells it out, but it was only ever asserted against
parse().The fix mirrors what's already next door:
value()(api.py:283-287) guards withif not parser.end(): raise ..., and the document parser reads a top-level pairwith
_parse_key_value(True)so the comment trail is consumed.key_value()nowdoes both. Trailing whitespace, a trailing comment and a final newline stay
valid; real trailing input raises.
This follows the same call the project already made in #527, "Raise on malformed
array element instead of dropping it".
Behaviour change: input that was silently truncated now raises. I found no
open PR or issue touching
key_value(checked the changed files of all 22 openPRs; only #294 touches
api.py, forDecimalcodecs).Verification
python -m pytest tests/ -q, same command and environment either side:api.pyat HEAD)5 failed, 1057 passed1062 passedI also checked the naive version of this fix — the
end()guard alone, withoutparse_comment=True. It fails 3 of the new cases ("foo = 12\n","foo = 12 ","foo = 12 # comment\n"), which is why those are in the test.Agent Drafting Metadata
claude-opus-5)api.pyagainst each other —key_value()was the only one with a single test reference, and the siblingvalue()had the end-of-input guard it was missing. The diff, tests and thisdescription were drafted with the agent and reviewed before submission.