{{ message }}
Preserve str subclass type returned by __str__ / __repr__ - #7701
Merged
youknowone merged 1 commit intoApr 28, 2026
Merged
Conversation
Closes RustPython#7450. CPython's unicode_new_impl returns the PyObject_Str result as-is when type == &PyUnicode_Type, only invoking unicode_subtype_new for actual str subclasses. RustPython's PyStr::Constructor stripped the result via Self::from(s.as_wtf8().to_owned()) and re-materialized through into_ref_with_type, dropping the subclass type even when cls is exactly str. Add a slot_new branch that returns input.str(vm)? directly when cls is str_type with no encoding. Subtype construction and the bytes-decoding path are unchanged. Unmasks test_str.StrTest.test_conversion (11 assertTypedEqual cases).
Contributor
Contributor
📦 Library DependenciesThe following Lib/ modules were modified. Here are their dependencies: [x] test: cpython/Lib/test/test_str.py (TODO: 14) dependencies: dependent tests: (no tests depend on str) Legend:
|
ShaharNaveh
approved these changes
Apr 27, 2026
ShaharNaveh
left a comment
Contributor
There was a problem hiding this comment.
Great!
ty for finding these:)
Contributor
Author
youknowone
approved these changes
Apr 28, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Closes #7450.
Background
CPython's
unicode_new_impl(Objects/unicodeobject.c#L15575-L15596) only invokesunicode_subtype_newwhen the requested type is astrsubclass. Whentype == &PyUnicode_Type, it returns the result ofPyObject_Str(x)as-is, preserving anystrsubclass type that__str__or__repr__returned.RustPython's
PyStr::Constructor::py_newextracted the raw bytes viaSelf::from(s.as_wtf8().to_owned()), thenslot_newre-materialized throughinto_ref_with_type(vm, cls). This stripped the subclass type even whencls is str.Repro
Fix
Add a branch in
slot_newthat returnsinput.str(vm)?directly whencls is str_type(no subtype conversion, no encoding). Subtype construction (StrSubclass(...)) and thestr(bytes, encoding)decoding path are unchanged.Tests unmasked
test_str.StrTest.test_conversion(11assertTypedEqualcases coveringstr(WithStr/StrWithStr/WithRepr(...))×StrSubclass/OtherStrSubclasspermutations)Verification
str(),str('hi'),str(MyStr('x')),__str__/__repr__returning subclass,MyStr('z'),OtherStr(MyStr('w')),str(b'abc', 'utf-8'))test_str,test_descr,test_class,test_typing,test_userstring,test_format,test_pickle,test_io,test_collections,test_dict,test_set,test_exceptions,test_fstring,test_genericaliasSummary by CodeRabbit