Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
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
12 changes: 12 additions & 0 deletions crates/vm/src/builtins/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,18 @@ impl Constructor for PyStr {
}

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

// CPython parity: when cls is exactly str, return the __str__ / __repr__
// result as-is so any str subclass type the user returned is preserved
// (matches unicode_new_impl which only invokes unicode_subtype_new when
// type != &PyUnicode_Type).
if cls.is(vm.ctx.types.str_type)
&& args.encoding.is_missing()
&& let OptionalArg::Present(input) = &args.object
{
return Ok(input.str(vm)?.into());
}

let payload = Self::py_new(&cls, args, vm)?;
payload.into_ref_with_type(vm, cls).map(Into::into)
}
Expand Down
Loading