Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Preserve str subclass returned by __repr__
Co-authored-by: youknowone <69878+youknowone@users.noreply.github.com>
  • Loading branch information
Copilot and youknowone committed Mar 28, 2026
commit ed2fd1e16fcd2bc6e61fc2289c46d1eb0ad773b4
1 change: 0 additions & 1 deletion Lib/test/test_str.py
Original file line number Diff line number Diff line change
Expand Up @@ -2414,7 +2414,6 @@ def test_ucs4(self):
else:
self.fail("Should have raised UnicodeDecodeError")

@unittest.expectedFailure # TODO: RUSTPYTHON; AssertionError: <class 'str'> is not <class 'test.test_str.StrSubclass'>
def test_conversion(self):
# Make sure __str__() works properly
class StrWithStr(str):
Expand Down
5 changes: 2 additions & 3 deletions crates/vm/src/builtins/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,13 +394,12 @@ impl Constructor for PyStr {
type Args = StrArgs;

fn slot_new(cls: PyTypeRef, func_args: FuncArgs, vm: &VirtualMachine) -> PyResult {
// Optimization: return exact str as-is (only when no encoding/errors provided)
// Optimization: for exact str, return PyObject_Str result as-is
if cls.is(vm.ctx.types.str_type)
&& func_args.args.len() == 1
&& func_args.kwargs.is_empty()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[auto-format] reported by reviewdog 🐶

Suggested change
if cls.is(vm.ctx.types.str_type)
&& func_args.args.len() == 1
&& func_args.kwargs.is_empty()
if cls.is(vm.ctx.types.str_type) && func_args.args.len() == 1 && func_args.kwargs.is_empty()

&& func_args.args[0].class().is(vm.ctx.types.str_type)
{
return Ok(func_args.args[0].clone());
return func_args.args[0].str(vm).map(Into::into);
}

let args: Self::Args = func_args.bind(vm)?;
Expand Down