Skip to content

StringIO breaking change for str subclasses #149046

@KowalskiThomas

Description

@KowalskiThomas

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.14bugs and security fixes3.15new features, bugs and security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)topic-IOtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions