Repository navigation
repr() does not raise TypeError when __repr__ returns a non-string #18941
Description
Activity
Thanks for sharing.
Do you have a real world example where this difference is problematic, or is your concern the different behavior compared to Cpython?@Josverl Thanks for your question.
My concern is the behavioral difference compared to CPython, and whether this difference is intentional.
This was discovered via fuzzing rather than a real-world case.I’m interested in MicroPython–CPython compatibility and opened this issue to help improve it.
Thanks for the background info.
MicroPython's goal is not to be identical to COython, but to balance Micro with Python
(Also see MicroPython design values in the repro's readme.md)
So yes, there is a desire to match cpython where possible, but it also should provide practical value.However such differences to Cpython should be documented, and there are actually a seperate set of
cpydifftest to do just that.So there is a choice between implementation, and the assumed associated increase in firmware size, and documenting the differences.
So yes, there is a desire to match cpython where possible, but it also should provide practical value.
Yes, that's correct.
In this case I don't think this difference matters in real applications. I doubt there's any code relying on
repr()raising an exception for non-string return values.So unless this can be fixed at zero-cost (no change in code size) then I think it should be left as-is.
(I don't even thing it really needs documenting as a difference because it's such a minor thing and we don't want to clutter the list of differences with arguably minor things like this.)
I agree with MicroPython's design values — matching CPython blindly isn't the goal, and I agree with @Josverl and @dpgeorge that keeping the
repr()implementation as-is is reasonable given it's unlikely to matter in practice.However, the same root cause also affects
str():class Foo: def __str__(self): return True print(str(Foo())) # MicroPython: True / CPython: TypeError
This feels more significant — overriding
__str__is something Python developers do regularly, andstr()is a core builtin. I've opened PR #18955 to document this difference. Would appreciate a review.- added a commit that references this issue
on May 14, 2026 Documented in 3ef656a
- added a commit that references this issue
on Sep 21, 2026
Port, board and/or hardware
Unix port, Linux
MicroPython version
v1.27.0
Reproduction
Expected behaviour
CPython v3.11.15:
Observed behaviour
MicroPython v1.27.0:
Additional Information
repr()does not raiseTypeErrorwhen a user-defined__repr__method returns a non-string value. Instead, MicroPython silently converts and returns the value as a string. This is a CPython compatibility issue.Root Cause
instance_printinobjtype.ccalls the user-defined__repr__method viamp_call_function_1and passes the result directly tomp_obj_print_helperwithout validating that the return value is a string.Fix
Add a
mp_obj_is_str(r)check after calling the__repr__method ininstance_print, and raiseTypeErrorif the return value is not a string.Code of Conduct
Yes, I agree