Skip to content

Keep valid TOML when appending to a String - #733

Open
Harp404 wants to merge 2 commits into
python-poetry:masterfrom
Harp404:fix-string-add-quoting
Open

Harp404 wants to merge 2 commits into
python-poetry:masterfrom
Harp404:fix-string-add-quoting

Conversation

@Harp404

@Harp404 Harp404 commented Dec 4, 2026

Copy link
Copy Markdown

String.__add__ pastes the other operand into the left string's TOML source as-is, so the result can be invalid TOML or read back as a different value:

  • string("lit", literal=True) + "'q" writes 'lit'q' (a literal string cannot escape ')
  • string("a") + string('b"c', literal=True) writes "ab"c"
  • string("a", literal=True) + string("b\nc") writes 'ab\nc', which reads back with a literal backslash
  • string('a""', multiline=True) + '"' writes """a""""""
  • string("", multiline=True) + "\nb" loses the newline (the parser drops a newline right after the opening """)

Now a literal string that can't hold the result becomes a basic string of the same kind (the same fallback as #627), an operand with a different quote style is added by value instead of by its source text, and the multiline join cases are handled. The string's trivia/comment is kept.

Tests: test_string_add_keeps_valid_toml (12 cases) and test_string_add_literal_fallback_keeps_comment fail on master and pass with this change; the full suite passes. I also fuzzed random additions (180k across 6 seeds) and checked every result with tomllib and tomlkit's own parser: none were invalid or changed value.

Duplicate Check

Agent Drafting Metadata

  • Agent: Claude Code
  • Model: Claude Opus 5.5
  • Notes: The agent investigated the bug, wrote the fix and tests, and drafted this description.

Copilot AI balanced review requested due to automatic review settings December 4, 2026 07:59
@human-attestation

human-attestation Bot commented Dec 4, 2026 •

Copy link
Copy Markdown

✅ Human Attestation: @Harp404 attested this PR at commit 131d3f6 (2026-12-04T07:59:46.457Z).

I have read and understood all agent-written code in this PR. / 我已阅读并理解此 PR 中 Agent 编写的全部代码。

Later pushes to this PR stay attested. Attestation page

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

@Harp404
Harp404 force-pushed the fix-string-add-quoting branch from 131d3f6 to dfa751c Compare December 4, 2026 08:00

@Giacomo117 Giacomo117 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.

Thanks for this! I merged it with today's master: no conflicts, all 1529 tests pass, and the 13 new tests fail without the fix.

The 5 cases in the description give invalid TOML or a different value on master and are all correct with this PR. += on k = 'it' # comment now writes valid TOML and keeps the comment. I also tried 30,000 random additions across the four string types and none produced invalid TOML.

The only problem I found is the one in the inline comment. It's not your fault: #726 was merged after you opened this PR. A test with a str enum might be worth adding.

Comment thread tomlkit/items.py Outdated
)
# The raw text of a String of another type is spelled for its own
# delimiters: append its value instead.
appended = str(other)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Merged with today's master, this line brings back the bug fixed in #726. With Color a str-based enum, string("a") + Color.RED writes "aColor.RED", while master writes "ared". Same with += and with all four string types.

Using str.__str__(other), like #726 does, fixes it and the full suite still passes (1529 tests).

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thankyou for testing it so thoroughly, I fixed the enum case in a267932 the same way #726 does, and I have added the str enum test you suggested.

String.__add__ spliced the appended text into the raw TOML of the left
string. That wrote invalid TOML or a different value when a literal
string could not hold the result, when the right operand was a String
of another quoting style, and at the edges of multiline strings (a
leading newline, adjacent quotes, whitespace after a line ending
backslash).

Fall back to a basic string when a literal string cannot hold the
result, append a String of another style by value, and handle the
multiline edge cases.
@Harp404
Harp404 force-pushed the fix-string-add-quoting branch from dfa751c to a267932 Compare December 4, 2026 17:16
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.

3 participants