Skip to content

fix: write the underlying characters of str subclasses in dumps() - #726

Open
haigou-web wants to merge 1 commit into
python-poetry:masterfrom
haigou-web:fix/725-str-subclass-dumps
Open

haigou-web wants to merge 1 commit into
python-poetry:masterfrom
haigou-web:fix/725-str-subclass-dumps

Conversation

@haigou-web

Copy link
Copy Markdown

Summary

escape_string() ended with return str(s). For a str subclass that dispatches to the subclass's __str__, so whatever that returns is what ends up inside the quotes. A str-based Enum member gives its repr-like form, and a custom wrapper gives anything at all:

class Color(str, Enum):
    RED = "red"

class Quoted(str):
    def __str__(self):
        return f'"{super().__str__()}"'

doc = tomlkit.document()
doc["color"] = Color.RED
doc["k"] = Quoted("shown")

tomlkit.dumps(doc)                    # color = "Color.RED" / k = ""shown""
tomlkit.parse(tomlkit.dumps(doc))     # UnexpectedCharError

0.15.2 wrote color = "red" and k = "shown", so this is a regression from the single-pass escaping rewrite in 0.15.3.

str.__str__(s) unboxes to a plain str without invoking the subclass implementation, which is what the escaping logic wants. The escaping branch already returns a plain str via re.sub, so both paths now agree.

Fixes #725

Duplicate Check

  • I searched the open PRs and found none that already fixes this issue.

Checked all open PRs and the escape/str subclass/enum/dumps title searches. The nearest ones are #653 (re-escaping across quote styles) and #694 (escape=False multiline strings); neither touches str subclasses in escape_string().

Notes

  • String is unaffected: its __str__ is str.__str__, so the non-escaping fast path stays as fast as before.
  • Four tests cover it: plain str, subclass without sequences, subclass with sequences, and dumps() round-trip through parse() for both a str-based Enum and a custom wrapper.
  • Full suite locally: 809 passed. ruff check and ruff format --check clean.
  • CHANGELOG entry added under [Unreleased] / Fixed.

Agent Drafting Metadata

  • Agent: pi (earendil-works/pi-coding-agent)
  • Model: Claude
  • Notes: The agent reproduced the bug on 0.15.3, wrote the fix and tests, and ran the full suite and linters locally before opening this PR.

escape_string() called str(s), which dispatches to an overridden __str__ on a str subclass. str-based Enum members were therefore serialized as their repr-like Enum form and custom wrappers produced invalid TOML that could not be parsed back. This restores the 0.15.2 behavior lost in the single-pass escaping rewrite.

String itself is unaffected: its __str__ is str.__str__, so the fast path stays as fast as before.
@human-attestation

human-attestation Bot commented Nov 28, 2026 •

Copy link
Copy Markdown

✅ Human Attestation: @haigou-web attested this PR at commit 4928ee8 (2026-11-29T07:55:09.528Z).

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

Later pushes to this PR stay attested. Attestation page

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.

0.15.3: str-based Enum values are written as "Color.RED" instead of their value

1 participant