Skip to content

gh-149046: fix: correctly handle str subclasses in StringIO#149047

Draft
KowalskiThomas wants to merge 3 commits intopython:mainfrom
KowalskiThomas:kowalski/fix-correctly-handle-str-subclasses-in-stringio
Draft

gh-149046: fix: correctly handle str subclasses in StringIO#149047
KowalskiThomas wants to merge 3 commits intopython:mainfrom
KowalskiThomas:kowalski/fix-correctly-handle-str-subclasses-in-stringio

Conversation

@KowalskiThomas
Copy link
Copy Markdown
Contributor

@KowalskiThomas KowalskiThomas commented Apr 27, 2026

What is this PR?

This PR is, in a way, a follow-up from #148241.

This other PR fixed a breaking change in behaviour where classes inheriting str would have __str__ called on them when instead of using the "underlying" str itself. The PR fixed the problem for JSON serialisation, but seemingly missed some other call sites, including StringIO. This seems to affect Python 3.14+ (but works as expected on 3.13).

This is a reproducer.

import io

class StrSubclass(str):
    def __str__(self):
        return "WRONG_VALUE"

obj = StrSubclass("correct_value")

buf = io.StringIO()
buf.write(obj)
value = buf.getvalue()
assert value == "correct_value", f"got {value!r}"

... gives AssertionError: got 'WRONG_VALUE'

@KowalskiThomas KowalskiThomas force-pushed the kowalski/fix-correctly-handle-str-subclasses-in-stringio branch from 9451278 to 47788ec Compare April 27, 2026 09:19
@KowalskiThomas KowalskiThomas force-pushed the kowalski/fix-correctly-handle-str-subclasses-in-stringio branch from 47788ec to 4f0649e Compare April 27, 2026 09:19
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