KowalskiThomas · GitHub

Bug report

Bug description:

This 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 and this is a fix PR: #149047.

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}"

... gives AssertionError: got 'WRONG_VALUE' on 3.14 and a locally built 3.15/dev.

CPython versions tested on:

3.14

Operating systems tested on:

macOS

Linked PRs

Read the original on github.com ↗