Skip to content

Preserve literal quotes when replacing table strings - #627

Open
guhou-hvi wants to merge 1 commit into
python-poetry:masterfrom
guhou-hvi:fix/preserve-literal-quotes
Open

guhou-hvi wants to merge 1 commit into
python-poetry:masterfrom
guhou-hvi:fix/preserve-literal-quotes

Conversation

@guhou-hvi

Copy link
Copy Markdown

Summary

Related to #325.

When editing a parsed TOML table, assigning a plain Python string currently replaces single quotes with double quotes: bar = 'baz' becomes bar = "quux" after assigning "quux". This adds formatting changes to an otherwise simple configuration edit.

Preserve single-line literal quoting for replacements that can be represented that way. Values containing an apostrophe, newline, or forbidden control character still use basic strings, which provide the necessary escaping. Explicit tomlkit.string(...) values keep the caller's quote choice. New fields, multiline strings, and array element assignments keep their existing behavior.

The conversion is shared by document, table, and inline-table assignment. This is needed because tables convert Python strings to TOML items before passing them to the underlying container; checking only the final replacement method would miss the original issue's table example.

Regression tests check root, ordinary, inline, and out-of-order tables, preserving comments, spacing, key order and line endings. They also check the actual values after serialization and reparsing, explicit quote choices, and values requiring escaping. These are additional checks for this change, alongside the project's existing suite.

On Windows with Python 3.12.2, all 1,088 tests pass, including 20 new regression cases (10 fail on the baseline). All six pre-commit hooks pass. Mypy reports the same 29 errors on the baseline and the fix. Alternating local measurements found about 0.98–2.50 microseconds of extra cost per string assignment (6.0–15.8%); parsing 30 fields differed by 0.25%.

Agent Drafting Metadata

  • Agent: OpenAI Codex
  • Model: GPT-6; exact deployment identifier is not exposed in this session.
  • Notes: Codex implemented the code and regression tests, drafted the documentation and description, and ran local checks. A separate Codex agent reviewed the diff and exercised additional input cases. I have reviewed and understand the changes.

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.

1 participant